Skip to content
This repository has been archived by the owner on Apr 13, 2022. It is now read-only.

Commit

Permalink
Browse files Browse the repository at this point in the history
fix($cookies): update $cookies to prevent duplicate cookie writes and…
… play nice with external code

Update the ngCookies service to prevent repetitive writes via $browser.cookies()
Also it is possible for $cookies to get confused about cookies modified outside of $cookies and
see those changes as user changes via the $cookies service which would then be set again. This
unnecessary setting of cookies can duplicate or overwrite depending on the original cookie's
domain. This update prevents that scenario.

Closes #11490
Closes #11515
  • Loading branch information
chrisakers authored and IgorMinar committed Jun 5, 2015
1 parent c4ae7d2 commit 706a93a
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/ngCookies/cookies.js
Expand Up @@ -86,6 +86,7 @@ angular.module('ngCookies', ['ng']).
for (name in lastCookies) {
if (isUndefined(cookies[name])) {
$browser.cookies(name, undefined);
delete lastCookies[name];
}
}

Expand All @@ -98,24 +99,24 @@ angular.module('ngCookies', ['ng']).
}
if (value !== lastCookies[name]) {
$browser.cookies(name, value);
lastCookies[name] = value;
updated = true;
}
}

//verify what was actually stored
if (updated) {
updated = false;
browserCookies = $browser.cookies();

for (name in cookies) {
if (cookies[name] !== browserCookies[name]) {
//delete or reset all cookies that the browser dropped from $cookies
if (isUndefined(browserCookies[name])) {
delete cookies[name];
delete lastCookies[name];
} else {
cookies[name] = browserCookies[name];
cookies[name] = lastCookies[name] = browserCookies[name];
}
updated = true;
}
}
}
Expand Down
45 changes: 45 additions & 0 deletions test/ngCookies/cookiesSpec.js
Expand Up @@ -45,6 +45,51 @@ describe('$cookies', function() {
}));


it('should only set the browser cookie once per cookie change',
inject(function($cookies, $browser, $rootScope) {
function hasArgs(call) {
return call.args.length > 0;
}

spyOn($browser, 'cookies').andCallThrough();

$cookies.oatmealCookie = 'nom nom';
$rootScope.$digest();

expect($browser.cookies.calls.filter(hasArgs).length).toEqual(1);
}));


it('should only delete the browser cookie once per cookie delete',
inject(function($cookies, $browser, $rootScope) {
function hasArgs(call) {
return call.args.length > 0;
}

spyOn($browser, 'cookies').andCallThrough();

delete $cookies.preexisting;
$rootScope.$digest();

expect($browser.cookies.calls.filter(hasArgs).length).toEqual(1);
}));


it('should allow cookies to be set outside the service without overwriting/duplicating',
inject(function($cookies, $browser, $rootScope) {
var browserCookieSpy = spyOn($browser, 'cookies').andCallThrough();

function hasArgs(call) {
return call.args.length > 0;
}

$browser.cookieHash['preexisting'] = 'vanilla';
$cookies.oatmealCookie = 'nom nom';
$rootScope.$digest();
expect(browserCookieSpy.calls.filter(hasArgs).length).toEqual(1);
}));


it('should convert non-string values to string',
inject(function($cookies, $browser, $rootScope) {
$cookies.nonString = [1, 2, 3];
Expand Down

0 comments on commit 706a93a

Please sign in to comment.