mirror of
https://github.com/zhigang1992/angular.js.git
synced 2026-04-17 22:34:43 +08:00
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.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
10
test/angular-mocks.js
vendored
10
test/angular-mocks.js
vendored
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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'});
|
||||
});
|
||||
});
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user