aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
Diffstat (limited to 'test')
-rw-r--r--test/BrowserSpecs.js56
-rw-r--r--test/angular-mocks.js4
-rw-r--r--test/servicesSpec.js24
3 files changed, 69 insertions, 15 deletions
diff --git a/test/BrowserSpecs.js b/test/BrowserSpecs.js
index abd761eb..148fbca4 100644
--- a/test/BrowserSpecs.js
+++ b/test/BrowserSpecs.js
@@ -72,11 +72,16 @@ describe('browser', function(){
}
}
- var browser;
+ var browser, log, logs;
beforeEach(function() {
deleteAllCookies();
- browser = new Browser({}, jqLite(document));
+ logs = {log:[], warn:[], info:[], error:[]}
+ log = {log: function() {logs.log.push(Array.prototype.slice.call(arguments))},
+ warn: function() {logs.warn.push(Array.prototype.slice.call(arguments))},
+ info: function() {logs.info.push(Array.prototype.slice.call(arguments))},
+ error: function() {logs.error.push(Array.prototype.slice.call(arguments))}};
+ browser = new Browser({}, jqLite(document), undefined, XHR, log);
expect(document.cookie).toEqual('');
});
@@ -121,7 +126,7 @@ describe('browser', function(){
it('should create and store a cookie', function() {
browser.cookies('cookieName', 'cookieValue');
- expect(document.cookie).toEqual('cookieName=cookieValue');
+ expect(document.cookie).toMatch(/cookieName=cookieValue;? ?/);
expect(browser.cookies()).toEqual({'cookieName':'cookieValue'});
});
@@ -145,6 +150,47 @@ describe('browser', function(){
expect(rawCookies).toContain('cookie1%3D=val%3Bue');
expect(rawCookies).toContain('cookie2%3Dbar%3Bbaz=val%3Due');
});
+
+ it('should log warnings when 4kb per cookie storage limit is reached', function() {
+ var i, longVal = '', cookieString;
+
+ for(i=0; i<4092; i++) {
+ longVal += '+';
+ }
+
+ cookieString = document.cookie;
+ browser.cookies('x', longVal); //total size 4094-4096, so it should go through
+ expect(document.cookie).not.toEqual(cookieString);
+ 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
+ expect(logs.warn).toEqual(
+ [[ "Cookie 'x' possibly not set or overflowed because it was too large (4097 > 4096 " +
+ "bytes)!" ]]);
+ });
+
+ it('should log warnings when 20 cookies per domain storage limit is reached', function() {
+ var i, str;
+
+ for (i=0; i<20; i++) {
+ str = '' + i;
+ browser.cookies(str, str);
+ }
+
+ i=0;
+ for (str in browser.cookies()) {
+ i++;
+ }
+ expect(i).toEqual(20);
+ expect(logs.warn).toEqual([]);
+
+ browser.cookies('one', 'more');
+ //browser behavior is undefined, so we test for existance of warning logs only
+ expect(logs.warn).toEqual([]);
+ });
+
});
@@ -157,14 +203,12 @@ describe('browser', function(){
it ('should return a value for an existing cookie', function() {
document.cookie = "foo=bar";
- browser.cookies(true);
expect(browser.cookies().foo).toEqual('bar');
});
it ('should unescape cookie values that were escaped by puts', function() {
document.cookie = "cookie2%3Dbar%3Bbaz=val%3Due";
- browser.cookies(true);
expect(browser.cookies()['cookie2=bar;baz']).toEqual('val=ue');
});
@@ -197,7 +241,6 @@ describe('browser', function(){
expect(browser.cookies()).toEqual({'oatmealCookie':'drool'});
document.cookie = 'oatmealCookie=changed';
- browser.cookies(true);
expect(browser.cookies().oatmealCookie).toEqual('changed');
});
@@ -233,4 +276,3 @@ describe('browser', function(){
});
});
});
-
diff --git a/test/angular-mocks.js b/test/angular-mocks.js
index a0d25042..5b5c9863 100644
--- a/test/angular-mocks.js
+++ b/test/angular-mocks.js
@@ -102,7 +102,9 @@ MockBrowser.prototype = {
if (value == undefined) {
delete this.cookieHash[name];
} else {
- this.cookieHash[name] = ""+value;
+ if (isString(value)) {
+ this.cookieHash[name] = value;
+ }
}
} else {
return copy(this.cookieHash);
diff --git a/test/servicesSpec.js b/test/servicesSpec.js
index 28bde598..3416f0ea 100644
--- a/test/servicesSpec.js
+++ b/test/servicesSpec.js
@@ -373,13 +373,22 @@ describe("service", function(){
describe('$cookies', function() {
- it('should provide access to existing cookies via object properties', function(){
+ it('should provide access to existing cookies via object properties and keep them in sync',
+ function(){
expect(scope.$cookies).toEqual({});
scope.$browser.cookies('brandNew', 'cookie');
scope.$browser.poll();
expect(scope.$cookies).toEqual({'brandNew':'cookie'});
+
+ scope.$browser.cookies('brandNew', 'cookie2');
+ scope.$browser.poll();
+ expect(scope.$cookies).toEqual({'brandNew':'cookie2'});
+
+ scope.$browser.cookies('brandNew', undefined);
+ scope.$browser.poll();
+ expect(scope.$cookies).toEqual({});
});
@@ -396,10 +405,11 @@ describe("service", function(){
});
- it('should turn non-string into String when creating a cookie', function() {
+ it('should ignore non-string values when asked to create a cookie', function() {
scope.$cookies.nonString = [1, 2, 3];
scope.$eval();
- expect(scope.$browser.cookies()).toEqual({'nonString':'1,2,3'});
+ expect(scope.$browser.cookies()).toEqual({});
+ expect(scope.$cookies).toEqual({});
});
@@ -425,10 +435,10 @@ describe("service", function(){
});
- describe('$sessionStore', function() {
+ describe('$cookieStore', function() {
it('should serialize objects to json', function() {
- scope.$sessionStore.put('objectCookie', {id: 123, name: 'blah'});
+ scope.$cookieStore.put('objectCookie', {id: 123, name: 'blah'});
scope.$eval(); //force eval in test
expect(scope.$browser.cookies()).toEqual({'objectCookie': '{"id":123,"name":"blah"}'});
});
@@ -437,12 +447,12 @@ describe("service", function(){
it('should deserialize json to object', function() {
scope.$browser.cookies('objectCookie', '{"id":123,"name":"blah"}');
scope.$browser.poll();
- expect(scope.$sessionStore.get('objectCookie')).toEqual({id: 123, name: 'blah'});
+ expect(scope.$cookieStore.get('objectCookie')).toEqual({id: 123, name: 'blah'});
});
it('should delete objects from the store when remove is called', function() {
- scope.$sessionStore.put('gonner', { "I'll":"Be Back"});
+ scope.$cookieStore.put('gonner', { "I'll":"Be Back"});
scope.$eval(); //force eval in test
expect(scope.$browser.cookies()).toEqual({'gonner': '{"I\'ll":"Be Back"}'});
});