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

Commit acbcfba

Browse files
IgorMinarmhevery
authored andcommitted
$cookies service refactoring
- remove obsolete code in tests - add warning logs when maximum cookie limits (as specified via RFC 2965) were reached - non-string values will now get dropped - after each update $cookies hash will reflect the actual state of browser cookies this means that if browser drops some cookies due to cookie overflow, $cookies will reflect that - $sessionStore got renamed to $cookieStore to avoid name conflicts with html5's sessionStore
1 parent a8931c9 commit acbcfba

File tree

6 files changed

+147
-37
lines changed

6 files changed

+147
-37
lines changed

src/AngularPublic.js

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
var browserSingleton;
2-
angularService('$browser', function browserFactory(){
2+
angularService('$browser', function($log){
33
if (!browserSingleton) {
44
browserSingleton = new Browser(
55
window.location,
66
jqLite(window.document),
77
jqLite(window.document.getElementsByTagName('head')[0]),
8-
XHR);
8+
XHR,
9+
$log);
910
browserSingleton.startPoller(50, function(delay, fn){setTimeout(delay,fn);});
1011
browserSingleton.bind();
1112
}
1213
return browserSingleton;
13-
});
14+
}, {inject:['$log']});
1415

1516
extend(angular, {
1617
'element': jqLite,

src/Browser.js

+32-10
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ var XHR = window.XMLHttpRequest || function () {
88
throw new Error("This browser does not support XMLHttpRequest.");
99
};
1010

11-
function Browser(location, document, head, XHR) {
11+
function Browser(location, document, head, XHR, $log) {
1212
var self = this;
1313
self.isMock = false;
1414

@@ -106,28 +106,50 @@ function Browser(location, document, head, XHR) {
106106
var rawDocument = document[0];
107107
var lastCookies = {};
108108
var lastCookieString = '';
109+
109110
/**
110-
* cookies() -> hash of all cookies
111-
* cookies(name, value) -> set name to value
112-
* if value is undefined delete it
113-
* cookies(name) -> should get value, but deletes (no one calls it right now that way)
111+
* The cookies method provides a 'private' low level access to browser cookies. It is not meant to
112+
* be used directly, use the $cookie service instead.
113+
*
114+
* The return values vary depending on the arguments that the method was called with as follows:
115+
* <ul><li>
116+
* cookies() -> hash of all cookies, this is NOT a copy of the internal state, so do not modify it
117+
* </li><li>
118+
* cookies(name, value) -> set name to value, if value is undefined delete the cookie
119+
* </li><li>
120+
* cookies(name) -> the same as (name, undefined) == DELETES (no one calls it right now that way)
121+
* </li></ul>
114122
*/
115-
self.cookies = function (name, value){
123+
self.cookies = function (/**string*/name, /**string*/value){
124+
var cookieLength, cookieArray, i, keyValue;
125+
116126
if (name) {
117127
if (value === _undefined) {
118128
delete lastCookies[name];
119129
rawDocument.cookie = escape(name) + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
120130
} else {
121-
rawDocument.cookie = escape(name) + '=' + escape(lastCookies[name] = ''+value);
131+
if (isString(value)) {
132+
rawDocument.cookie = escape(name) + '=' + escape(lastCookies[name] = value);
133+
134+
cookieLength = name.length + value.length + 1;
135+
if (cookieLength > 4096) {
136+
$log.warn("Cookie '"+ name +"' possibly not set or overflowed because it was too large ("+
137+
cookieLength + " > 4096 bytes)!");
138+
}
139+
if (lastCookies.length > 20) {
140+
$log.warn("Cookie '"+ name +"' possibly not set or overflowed because too many cookies " +
141+
"were already set (" + lastCookies.length + " > 20 )");
142+
}
143+
}
122144
}
123145
} else {
124146
if (rawDocument.cookie !== lastCookieString) {
125147
lastCookieString = rawDocument.cookie;
126-
var cookieArray = lastCookieString.split("; ");
148+
cookieArray = lastCookieString.split("; ");
127149
lastCookies = {};
128150

129-
for (var i = 0; i < cookieArray.length; i++) {
130-
var keyValue = cookieArray[i].split("=");
151+
for (i = 0; i < cookieArray.length; i++) {
152+
keyValue = cookieArray[i].split("=");
131153
if (keyValue.length === 2) { //ignore nameless cookies
132154
lastCookies[unescape(keyValue[0])] = unescape(keyValue[1]);
133155
}

src/services.js

+42-9
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,18 @@ angularService('$resource', function($xhr){
397397
}, {inject: ['$xhr.cache']});
398398

399399

400+
/**
401+
* $cookies service provides read/write access to the browser cookies. Currently only session
402+
* cookies are supported.
403+
*
404+
* Only a simple Object is exposed and by adding or removing properties to/from this object, new
405+
* cookies are created or deleted from the browser at the end of the current eval.
406+
*/
400407
angularService('$cookies', function($browser) {
401-
var cookies = {}, rootScope = this, lastCookies;
408+
var cookies = {},
409+
rootScope = this,
410+
lastCookies;
411+
402412
$browser.addPollFn(function(){
403413
var currentCookies = $browser.cookies();
404414
if (lastCookies != currentCookies) {
@@ -407,38 +417,61 @@ angularService('$cookies', function($browser) {
407417
rootScope.$eval();
408418
}
409419
});
420+
410421
this.$onEval(PRIORITY_FIRST, update);
411422
this.$onEval(PRIORITY_LAST, update);
423+
412424
return cookies;
413425

414426
function update(){
415-
var name, browserCookies = $browser.cookies();
427+
var name,
428+
browserCookies = $browser.cookies();
429+
430+
//$cookies -> $browser
416431
for(name in cookies) {
417-
if (browserCookies[name] !== cookies[name]) {
418-
$browser.cookies(name, browserCookies[name] = cookies[name]);
432+
if (cookies[name] !== browserCookies[name]) {
433+
$browser.cookies(name, cookies[name]);
419434
}
420435
}
436+
437+
//get what was actually stored in the browser
438+
browserCookies = $browser.cookies();
439+
440+
//$browser -> $cookies
421441
for(name in browserCookies) {
422-
if (browserCookies[name] !== cookies[name]) {
442+
if (isUndefined(cookies[name])) {
423443
$browser.cookies(name, _undefined);
444+
} else {
445+
cookies[name] = browserCookies[name];
446+
}
447+
}
448+
449+
//drop cookies in $cookies for cookies that $browser or real browser dropped
450+
for (name in cookies) {
451+
if (isUndefined(browserCookies[name])) {
452+
delete cookies[name];
424453
}
425454
}
426455
}
427456
}, {inject: ['$browser']});
428457

429458

430-
angularService('$sessionStore', function($store) {
459+
/**
460+
* $cookieStore provides a key-value (string-object) storage that is backed by session cookies.
461+
* Objects put or retrieved from this storage are automatically serialized or deserialized.
462+
*/
463+
angularService('$cookieStore', function($store) {
431464

432465
return {
433-
get: function(key) {
466+
get: function(/**string*/key) {
434467
return fromJson($store[key]);
435468
},
436469

437-
put: function(key, value) {
470+
put: function(/**string*/key, /**Object*/value) {
438471
$store[key] = toJson(value);
439472
},
440473

441-
remove: function(key) {
474+
remove: function(/**string*/key) {
442475
delete $store[key];
443476
}
444477
};

test/BrowserSpecs.js

+49-7
Original file line numberDiff line numberDiff line change
@@ -72,11 +72,16 @@ describe('browser', function(){
7272
}
7373
}
7474

75-
var browser;
75+
var browser, log, logs;
7676

7777
beforeEach(function() {
7878
deleteAllCookies();
79-
browser = new Browser({}, jqLite(document));
79+
logs = {log:[], warn:[], info:[], error:[]}
80+
log = {log: function() {logs.log.push(Array.prototype.slice.call(arguments))},
81+
warn: function() {logs.warn.push(Array.prototype.slice.call(arguments))},
82+
info: function() {logs.info.push(Array.prototype.slice.call(arguments))},
83+
error: function() {logs.error.push(Array.prototype.slice.call(arguments))}};
84+
browser = new Browser({}, jqLite(document), undefined, XHR, log);
8085
expect(document.cookie).toEqual('');
8186
});
8287

@@ -121,7 +126,7 @@ describe('browser', function(){
121126

122127
it('should create and store a cookie', function() {
123128
browser.cookies('cookieName', 'cookieValue');
124-
expect(document.cookie).toEqual('cookieName=cookieValue');
129+
expect(document.cookie).toMatch(/cookieName=cookieValue;? ?/);
125130
expect(browser.cookies()).toEqual({'cookieName':'cookieValue'});
126131
});
127132

@@ -145,6 +150,47 @@ describe('browser', function(){
145150
expect(rawCookies).toContain('cookie1%3D=val%3Bue');
146151
expect(rawCookies).toContain('cookie2%3Dbar%3Bbaz=val%3Due');
147152
});
153+
154+
it('should log warnings when 4kb per cookie storage limit is reached', function() {
155+
var i, longVal = '', cookieString;
156+
157+
for(i=0; i<4092; i++) {
158+
longVal += '+';
159+
}
160+
161+
cookieString = document.cookie;
162+
browser.cookies('x', longVal); //total size 4094-4096, so it should go through
163+
expect(document.cookie).not.toEqual(cookieString);
164+
expect(browser.cookies()['x']).toEqual(longVal);
165+
expect(logs.warn).toEqual([]);
166+
167+
browser.cookies('x', longVal + 'xxx') //total size 4097-4099, a warning should be logged
168+
//browser behavior is undefined, so we test for existance of warning logs only
169+
expect(logs.warn).toEqual(
170+
[[ "Cookie 'x' possibly not set or overflowed because it was too large (4097 > 4096 " +
171+
"bytes)!" ]]);
172+
});
173+
174+
it('should log warnings when 20 cookies per domain storage limit is reached', function() {
175+
var i, str;
176+
177+
for (i=0; i<20; i++) {
178+
str = '' + i;
179+
browser.cookies(str, str);
180+
}
181+
182+
i=0;
183+
for (str in browser.cookies()) {
184+
i++;
185+
}
186+
expect(i).toEqual(20);
187+
expect(logs.warn).toEqual([]);
188+
189+
browser.cookies('one', 'more');
190+
//browser behavior is undefined, so we test for existance of warning logs only
191+
expect(logs.warn).toEqual([]);
192+
});
193+
148194
});
149195

150196

@@ -157,14 +203,12 @@ describe('browser', function(){
157203

158204
it ('should return a value for an existing cookie', function() {
159205
document.cookie = "foo=bar";
160-
browser.cookies(true);
161206
expect(browser.cookies().foo).toEqual('bar');
162207
});
163208

164209

165210
it ('should unescape cookie values that were escaped by puts', function() {
166211
document.cookie = "cookie2%3Dbar%3Bbaz=val%3Due";
167-
browser.cookies(true);
168212
expect(browser.cookies()['cookie2=bar;baz']).toEqual('val=ue');
169213
});
170214

@@ -197,7 +241,6 @@ describe('browser', function(){
197241
expect(browser.cookies()).toEqual({'oatmealCookie':'drool'});
198242

199243
document.cookie = 'oatmealCookie=changed';
200-
browser.cookies(true);
201244
expect(browser.cookies().oatmealCookie).toEqual('changed');
202245
});
203246

@@ -233,4 +276,3 @@ describe('browser', function(){
233276
});
234277
});
235278
});
236-

test/angular-mocks.js

+3-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,9 @@ MockBrowser.prototype = {
102102
if (value == undefined) {
103103
delete this.cookieHash[name];
104104
} else {
105-
this.cookieHash[name] = ""+value;
105+
if (isString(value)) {
106+
this.cookieHash[name] = value;
107+
}
106108
}
107109
} else {
108110
return copy(this.cookieHash);

test/servicesSpec.js

+17-7
Original file line numberDiff line numberDiff line change
@@ -373,13 +373,22 @@ describe("service", function(){
373373

374374
describe('$cookies', function() {
375375

376-
it('should provide access to existing cookies via object properties', function(){
376+
it('should provide access to existing cookies via object properties and keep them in sync',
377+
function(){
377378
expect(scope.$cookies).toEqual({});
378379

379380
scope.$browser.cookies('brandNew', 'cookie');
380381
scope.$browser.poll();
381382

382383
expect(scope.$cookies).toEqual({'brandNew':'cookie'});
384+
385+
scope.$browser.cookies('brandNew', 'cookie2');
386+
scope.$browser.poll();
387+
expect(scope.$cookies).toEqual({'brandNew':'cookie2'});
388+
389+
scope.$browser.cookies('brandNew', undefined);
390+
scope.$browser.poll();
391+
expect(scope.$cookies).toEqual({});
383392
});
384393

385394

@@ -396,10 +405,11 @@ describe("service", function(){
396405
});
397406

398407

399-
it('should turn non-string into String when creating a cookie', function() {
408+
it('should ignore non-string values when asked to create a cookie', function() {
400409
scope.$cookies.nonString = [1, 2, 3];
401410
scope.$eval();
402-
expect(scope.$browser.cookies()).toEqual({'nonString':'1,2,3'});
411+
expect(scope.$browser.cookies()).toEqual({});
412+
expect(scope.$cookies).toEqual({});
403413
});
404414

405415

@@ -425,10 +435,10 @@ describe("service", function(){
425435
});
426436

427437

428-
describe('$sessionStore', function() {
438+
describe('$cookieStore', function() {
429439

430440
it('should serialize objects to json', function() {
431-
scope.$sessionStore.put('objectCookie', {id: 123, name: 'blah'});
441+
scope.$cookieStore.put('objectCookie', {id: 123, name: 'blah'});
432442
scope.$eval(); //force eval in test
433443
expect(scope.$browser.cookies()).toEqual({'objectCookie': '{"id":123,"name":"blah"}'});
434444
});
@@ -437,12 +447,12 @@ describe("service", function(){
437447
it('should deserialize json to object', function() {
438448
scope.$browser.cookies('objectCookie', '{"id":123,"name":"blah"}');
439449
scope.$browser.poll();
440-
expect(scope.$sessionStore.get('objectCookie')).toEqual({id: 123, name: 'blah'});
450+
expect(scope.$cookieStore.get('objectCookie')).toEqual({id: 123, name: 'blah'});
441451
});
442452

443453

444454
it('should delete objects from the store when remove is called', function() {
445-
scope.$sessionStore.put('gonner', { "I'll":"Be Back"});
455+
scope.$cookieStore.put('gonner', { "I'll":"Be Back"});
446456
scope.$eval(); //force eval in test
447457
expect(scope.$browser.cookies()).toEqual({'gonner': '{"I\'ll":"Be Back"}'});
448458
});

0 commit comments

Comments
 (0)