aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorIgor Minar2010-09-26 23:45:05 -0700
committerIgor Minar2010-09-27 15:10:05 -0700
commit984acdc6270df1dee5796ed44efebfb9ff6706c7 (patch)
tree770d3f78e3db85740b0c5cf59edf32356060d505 /test
parent3eec8c1a517f8b93a5afd15b7f83b33c5df7e54b (diff)
downloadangular.js-984acdc6270df1dee5796ed44efebfb9ff6706c7.tar.bz2
Reworked the cookie synchronization between cookie service, $browser and document.cookie.
Now we finally correctly handle situations when browser refuses to set a cookie, due to storage quota or other (file:// protocol) limitations.
Diffstat (limited to 'test')
-rw-r--r--test/BrowserSpecs.js34
-rw-r--r--test/angular-mocks.js10
-rw-r--r--test/servicesSpec.js24
3 files changed, 58 insertions, 10 deletions
diff --git a/test/BrowserSpecs.js b/test/BrowserSpecs.js
index 1a4e8585..5f28b610 100644
--- a/test/BrowserSpecs.js
+++ b/test/BrowserSpecs.js
@@ -152,27 +152,39 @@ describe('browser', function(){
});
it('should log warnings when 4kb per cookie storage limit is reached', function() {
- var i, longVal = '', cookieString;
+ var i, longVal = '', cookieStr;
for(i=0; i<4092; i++) {
longVal += '+';
}
- cookieString = document.cookie;
+ cookieStr = document.cookie;
browser.cookies('x', longVal); //total size 4094-4096, so it should go through
- expect(document.cookie).not.toEqual(cookieString);
+ expect(document.cookie).not.toEqual(cookieStr);
expect(browser.cookies()['x']).toEqual(longVal);
expect(logs.warn).toEqual([]);
- browser.cookies('x', longVal + 'xxx') //total size 4097-4099, a warning should be logged
- //browser behavior is undefined, so we test for existance of warning logs only
+ browser.cookies('x', longVal + 'xxx'); //total size 4097-4099, a warning should be logged
expect(logs.warn).toEqual(
[[ "Cookie 'x' possibly not set or overflowed because it was too large (4097 > 4096 " +
"bytes)!" ]]);
+
+ //force browser to dropped a cookie and make sure that the cache is not out of sync
+ browser.cookies('x', 'shortVal');
+ expect(browser.cookies().x).toEqual('shortVal'); //needed to prime the cache
+ cookieStr = document.cookie;
+ browser.cookies('x', longVal + longVal + longVal); //should be too long for all browsers
+
+ if (document.cookie !== cookieStr) {
+ fail("browser didn't drop long cookie when it was expected. make the cookie in this " +
+ "test longer");
+ }
+
+ expect(browser.cookies().x).toEqual('shortVal');
});
it('should log warnings when 20 cookies per domain storage limit is reached', function() {
- var i, str;
+ var i, str, cookieStr;
for (i=0; i<20; i++) {
str = '' + i;
@@ -185,12 +197,18 @@ describe('browser', function(){
}
expect(i).toEqual(20);
expect(logs.warn).toEqual([]);
+ cookieStr = document.cookie;
browser.cookies('one', 'more');
- //browser behavior is undefined, so we test for existance of warning logs only
expect(logs.warn).toEqual([]);
- });
+ //if browser dropped a cookie (very likely), make sure that the cache is not out of sync
+ if (document.cookie === cookieStr) {
+ expect(size(browser.cookies())).toEqual(20);
+ } else {
+ expect(size(browser.cookies())).toEqual(21);
+ }
+ });
});
diff --git a/test/angular-mocks.js b/test/angular-mocks.js
index b02fabaf..1a73a542 100644
--- a/test/angular-mocks.js
+++ b/test/angular-mocks.js
@@ -75,6 +75,7 @@ function MockBrowser() {
};
self.cookieHash = {};
+ self.lastCookieHash = {};
}
MockBrowser.prototype = {
@@ -103,12 +104,17 @@ MockBrowser.prototype = {
if (value == undefined) {
delete this.cookieHash[name];
} else {
- if (isString(value)) {
+ if (isString(value) && //strings only
+ value.length <= 4096) { //strict cookie storage limits
this.cookieHash[name] = value;
}
}
} else {
- return copy(this.cookieHash);
+ if (!equals(this.cookieHash, this.lastCookieHash)) {
+ this.lastCookieHash = copy(this.cookieHash);
+ this.cookieHash = copy(this.cookieHash);
+ }
+ return this.cookieHash;
}
}
diff --git a/test/servicesSpec.js b/test/servicesSpec.js
index 258af46b..f40b8f5e 100644
--- a/test/servicesSpec.js
+++ b/test/servicesSpec.js
@@ -438,6 +438,7 @@ describe("service", function(){
it('should remove a cookie when a $cookies property is deleted', function() {
scope.$cookies.oatmealCookie = 'nom nom';
scope.$eval();
+ scope.$browser.poll();
expect(scope.$browser.cookies()).
toEqual({'preexisting': 'oldCookie', 'oatmealCookie':'nom nom'});
@@ -446,6 +447,29 @@ describe("service", function(){
expect(scope.$browser.cookies()).toEqual({'preexisting': 'oldCookie'});
});
+
+
+ it('should drop or reset cookies that browser refused to store', function() {
+ var i, longVal;
+
+ for (i=0; i<5000; i++) {
+ longVal += '*';
+ }
+
+ //drop if no previous value
+ scope.$cookies.longCookie = longVal;
+ scope.$eval();
+ expect(scope.$cookies).toEqual({'preexisting': 'oldCookie'});
+
+
+ //reset if previous value existed
+ scope.$cookies.longCookie = 'shortVal';
+ scope.$eval();
+ expect(scope.$cookies).toEqual({'preexisting': 'oldCookie', 'longCookie': 'shortVal'});
+ scope.$cookies.longCookie = longVal;
+ scope.$eval();
+ expect(scope.$cookies).toEqual({'preexisting': 'oldCookie', 'longCookie': 'shortVal'});
+ });
});