Skip to content
This repository was archived by the owner on Apr 12, 2024. It is now read-only.

fix($browser): cookiePath now defaults to '' when basePath is undefined #1908

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/ng/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -245,7 +245,7 @@ function Browser(window, document, $log, $sniffer) {
//////////////////////////////////////////////////////////////
var lastCookies = {};
var lastCookieString = '';
var cookiePath = self.baseHref();
var cookiePath = self.baseHref() || '';

/**
* @name ng.$browser#cookies
Expand Down Expand Up @@ -276,7 +276,6 @@ function Browser(window, document, $log, $sniffer) {
} else {
if (isString(value)) {
cookieLength = (rawDocument.cookie = escape(name) + '=' + escape(value) + ';path=' + cookiePath).length + 1;

// per http://www.ietf.org/rfc/rfc2109.txt browser must allow at minimum:
// - 300 cookies
// - 20 cookies per unique domain
Expand Down
20 changes: 17 additions & 3 deletions src/ng/http.js
Original file line number Diff line number Diff line change
Expand Up @@ -521,7 +521,8 @@ function $HttpProvider() {
reqHeaders = extend({'X-XSRF-TOKEN': xsrfToken},
defHeaders.common, defHeaders[lowercase(config.method)], config.headers),
reqData = transformData(config.data, headersGetter(reqHeaders), reqTransformFn),
promise;
promise,
abortFn;

// strip content-type if data is undefined
if (isUndefined(config.data)) {
Expand All @@ -535,13 +536,17 @@ function $HttpProvider() {
// send request
promise = sendReq(config, reqData, reqHeaders);

// save a reference to the abort function
abortFn = promise.abort;

// transform future response
promise = promise.then(transformResponse, transformResponse);
promise.abort = abortFn;

// apply interceptors
forEach(responseInterceptors, function(interceptor) {
promise = interceptor(promise);
promise.abort = abortFn;
});

promise.success = function(fn) {
Expand Down Expand Up @@ -707,13 +712,20 @@ function $HttpProvider() {
function sendReq(config, reqData, reqHeaders) {
var deferred = $q.defer(),
promise = deferred.promise,
abortFn,
cache,
cachedResp,
url = buildUrl(config.url, config.params);

$http.pendingRequests.push(config);
promise.then(removePendingReq, removePendingReq);

promise.abort = function() {
if (isFunction(abortFn)) {
abortFn();
}
}


if (config.cache && config.method == 'GET') {
cache = isObject(config.cache) ? config.cache : defaultCache;
Expand Down Expand Up @@ -742,7 +754,7 @@ function $HttpProvider() {

// if we won't have the response in cache, send the request to the backend
if (!cachedResp) {
$httpBackend(config.method, url, reqData, done, reqHeaders, config.timeout,
abortFn = $httpBackend(config.method, url, reqData, done, reqHeaders, config.timeout,
config.withCredentials, config.responseType);
}

Expand All @@ -766,7 +778,9 @@ function $HttpProvider() {
}

resolvePromise(response, status, headersString);
$rootScope.$apply();
if(!$rootScope.$$phase) {
$rootScope.$apply();
}
}


Expand Down
14 changes: 9 additions & 5 deletions src/ng/httpBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,12 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument,
delete callbacks[callbackId];
});
} else {
var xhr = new XHR();
var xhr = new XHR(),
abortRequest = function () {
status = -1;
xhr.abort();
};

xhr.open(method, url, true);
forEach(headers, function(value, key) {
if (value) xhr.setRequestHeader(key, value);
Expand Down Expand Up @@ -81,11 +86,10 @@ function createHttpBackend($browser, XHR, $browserDefer, callbacks, rawDocument,
xhr.send(post || '');

if (timeout > 0) {
$browserDefer(function() {
status = -1;
xhr.abort();
}, timeout);
$browserDefer(abortRequest, timeout);
}

return abortRequest;
}


Expand Down
23 changes: 17 additions & 6 deletions src/ngResource/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,9 +290,10 @@ angular.module('ngResource', ['ng']).
replace((pctEncodeSpaces ? null : /%20/g), '+');
}

function Route(template, defaults) {
function Route(template, defaults, options) {
this.template = template = template + '#';
this.defaults = defaults || {};
this.options = extend({ encodeUri : true }, (options || {}));
var urlParams = this.urlParams = {};
forEach(template.split(/\W/), function(param){
if (param && template.match(new RegExp("[^\\\\]:" + param + "\\W"))) {
Expand All @@ -306,15 +307,14 @@ angular.module('ngResource', ['ng']).
url: function(params) {
var self = this,
url = this.template,
val,
encodedVal;
val;

params = params || {};
forEach(this.urlParams, function(_, urlParam){
val = params.hasOwnProperty(urlParam) ? params[urlParam] : self.defaults[urlParam];
if (angular.isDefined(val) && val !== null) {
encodedVal = encodeUriSegment(val);
url = url.replace(new RegExp(":" + urlParam + "(\\W)", "g"), encodedVal + "$1");
val = self.options.encodeUri ? encodeUriSegment(val) : val;
url = url.replace(new RegExp(":" + urlParam + "(\\W)", "g"), val + "$1");
} else {
url = url.replace(new RegExp("(\/?):" + urlParam + "(\\W)", "g"), function(match,
leadingSlashes, tail) {
Expand All @@ -341,7 +341,11 @@ angular.module('ngResource', ['ng']).


function ResourceFactory(url, paramDefaults, actions) {
var route = new Route(url);
var route = new Route(url, {}, (actions ? actions.options : undefined));

if (actions && actions.options) {
delete actions.options;
}

actions = extend({}, DEFAULT_ACTIONS, actions);

Expand Down Expand Up @@ -424,6 +428,8 @@ angular.module('ngResource', ['ng']).
promise.then(function(response) {
var data = response.data;
var q = value.$q, resolved = value.$resolved;
delete value.httpRequest;

if (data) {
if (action.isArray) {
value.length = 0;
Expand All @@ -439,6 +445,11 @@ angular.module('ngResource', ['ng']).
(success||noop)(value, response.headers);
}, error);


value.httpRequest = function () {
return promise;
};

return value;

};
Expand Down
24 changes: 23 additions & 1 deletion test/ngResource/resourceSpec.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,8 @@ describe("resource", function() {

it("should create resource", function() {
$httpBackend.expect('POST', '/CreditCard', '{"name":"misko"}').respond({id: 123, name: 'misko'});

var cc = CreditCard.save({name: 'misko'}, callback);

expect(cc).toEqualData({name: 'misko'});
expect(callback).not.toHaveBeenCalled();

Expand Down Expand Up @@ -264,10 +264,12 @@ describe("resource", function() {
$httpBackend.expect('GET', '/CreditCard?key=value').respond([{id: 1}, {id: 2}]);

var ccs = CreditCard.query({key: 'value'}, callback);

expect(ccs).toEqualData([]);
expect(callback).not.toHaveBeenCalled();

$httpBackend.flush();

expect(ccs).toEqualData([{id:1}, {id:2}]);
expect(callback.mostRecentCall.args[0]).toEqual(ccs);
expect(callback.mostRecentCall.args[1]()).toEqual({});
Expand Down Expand Up @@ -419,6 +421,26 @@ describe("resource", function() {
expect(person.name).toEqual('misko');
});

it("should cleanup and remove httpRequest when done", function() {
$httpBackend.expect('GET', '/CreditCard/123').respond({id: 123, number: '9876'});
var cc = CreditCard.get({id: 123}, callback);

expect(cc.httpRequest()).toBeTruthy();

$httpBackend.flush();
expect(cc.httpRequest).toBe(undefined);
});

it("should cleanup and remove httpRequest when done (with query)", function() {
$httpBackend.expect('GET', '/CreditCard/123').respond({id: 123, number: '9876'});
var cc = CreditCard.query({id: 123}, callback);

expect(cc.httpRequest()).toBeTruthy();

$httpBackend.flush();
expect(cc.httpRequest).toBe(undefined);
});

it("should have $q and $resolved properties for get", function () {
$httpBackend.expect('GET', '/CreditCard/123').respond({id: 123, number: '9876'});
var cc = CreditCard.get({id: 123}, callback);
Expand Down