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

fix($cookies): update $cookies to prevent duplicate cookie writes #11515

Closed
wants to merge 1 commit 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
7 changes: 4 additions & 3 deletions src/ngCookies/cookies.js
Original file line number Diff line number Diff line change
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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated is not evaluated after line 106.

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];
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated is not evaluated after line 106.

}
updated = true;
}
}
}
Expand Down
45 changes: 45 additions & 0 deletions test/ngCookies/cookiesSpec.js
Original file line number Diff line number Diff line change
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