Adding cookie service

- Browser.cookies()
  - MockBrowser
  - $cookie service
  - $sessionStore
This commit is contained in:
Misko Hevery 2010-09-14 22:51:01 +02:00
parent 07699b1a70
commit e3f760fbad
6 changed files with 438 additions and 4 deletions

View file

@ -6,6 +6,7 @@ angularService('$browser', function browserFactory(){
jqLite(window.document),
jqLite(window.document.getElementsByTagName('head')[0]));
browserSingleton.startUrlWatcher();
browserSingleton.startCookieWatcher();
browserSingleton.bind();
}
return browserSingleton;

View file

@ -22,8 +22,50 @@ function Browser(location, document, head) {
this.location = location;
this.document = document;
var rawDocument = document[0];
this.head = head;
this.idCounter = 0;
this.cookies = cookies;
this.watchCookies = function(fn){ cookieListeners.push(fn); };
// functions
var lastCookies = {};
var lastCookieString = '';
var cookieListeners = [];
/**
* cookies() -> hash of all cookies
* cookies(name, value) -> set name to value
* if value is undefined delete it
* cookies(name) -> should get value, but deletes (no one calls it right now that way)
*/
function cookies(name, value){
if (name) {
if (value === _undefined) {
delete lastCookies[name];
rawDocument.cookie = escape(name) + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
} else {
rawDocument.cookie = escape(name) + '=' + escape(lastCookies[name] = ''+value);
}
} else {
if (rawDocument.cookie !== lastCookieString) {
lastCookieString = rawDocument.cookie;
var cookieArray = lastCookieString.split("; ");
lastCookies = {};
for (var i = 0; i < cookieArray.length; i++) {
var keyValue = cookieArray[i].split("=");
if (keyValue.length === 2) { //ignore nameless cookies
lastCookies[unescape(keyValue[0])] = unescape(keyValue[1]);
}
}
foreach(cookieListeners, function(fn){
fn(lastCookies);
});
}
return lastCookies;
}
}
}
Browser.prototype = {
@ -132,6 +174,14 @@ Browser.prototype = {
})();
},
startCookieWatcher: function() {
var self = this;
(function poll() {
self.cookies();
self.setTimeout(poll, self.delay);
})();
},
setUrl: function(url) {
var existingURL = this.location.href;
if (!existingURL.match(/#/)) existingURL += '#';

View file

@ -392,3 +392,67 @@ angularService('$resource', function($xhr){
var resource = new ResourceFactory($xhr);
return bind(resource, resource.route);
}, {inject: ['$xhr.cache']});
angularService('$cookies', function($browser) {
var cookies = {}, rootScope = this;
$browser.watchCookies(function(newCookies){
copy(newCookies, cookies);
rootScope.$eval();
});
this.$onEval(PRIORITY_FIRST, update);
this.$onEval(PRIORITY_LAST, update);
return cookies;
function update(){
var name, browserCookies = $browser.cookies();
for(name in cookies) {
if (browserCookies[name] !== cookies[name]) {
$browser.cookies(name, browserCookies[name] = cookies[name]);
}
}
for(name in browserCookies) {
if (browserCookies[name] !== cookies[name]) {
$browser.cookies(name, _undefined);
//TODO: write test for this delete
//delete cookies[name];
}
}
};
}, {inject: ['$browser']});
angularService('$sessionStore', function($store) {
function SessionStore() {}
SessionStore.prototype.get = function(key) {
return fromJson($store[key]);
};
SessionStore.prototype.getAll = function() {
var all = {},
key;
for (key in $store) {
if (!$store.hasOwnProperty(key)) continue;
all[key] = fromJson($store[key]);
}
return all;
};
SessionStore.prototype.put = function(key, value) {
$store[key] = toJson(value);
};
SessionStore.prototype.remove = function(key) {
delete $store[key];
};
return new SessionStore();
}, {inject: ['$cookies']});

View file

@ -27,6 +27,25 @@ describe('browser', function(){
browser.startUrlWatcher();
});
it('should contain cookie cruncher', function() {
expect(browser.cookies).toBeDefined();
});
it('should be able to start cookie watcher', function() {
browser.delay = 1;
expectAsserts(2);
browser.watchCookies(function(cookies){
assertEquals({'foo':'bar'}, cookies);
});
browser.setTimeout = function(fn, delay){
assertEquals(1, delay);
document.cookie = 'foo=bar';
browser.setTimeout = function(fn, delay) {};
fn();
};
browser.startCookieWatcher();
});
describe('outstading requests', function(){
it('should process callbacks immedietly with no outstanding requests', function(){
var callback = jasmine.createSpy('callback');
@ -67,4 +86,193 @@ describe('browser', function(){
});
});
describe('cookies', function() {
function deleteAllCookies() {
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
var eqPos = cookie.indexOf("=");
var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
};
var browser;
beforeEach(function() {
deleteAllCookies();
browser = new Browser({}, jqLite(document));
expect(document.cookie).toEqual('');
});
afterEach(function() {
deleteAllCookies();
expect(document.cookie).toEqual('');
});
describe('remove all via (null)', function() {
it('should do nothing when no cookies are set', function() {
browser.cookies(null);
expect(document.cookie).toEqual('');
expect(browser.cookies()).toEqual({});
});
});
describe('remove via (cookieName, undefined)', function() {
it('should remove a cookie when it is present', function() {
document.cookie = 'foo=bar';
browser.cookies('foo', undefined);
expect(document.cookie).toEqual('');
expect(browser.cookies()).toEqual({});
});
it('should do nothing when an nonexisting cookie is being removed', function() {
browser.cookies('doesntexist', undefined);
expect(document.cookie).toEqual('');
expect(browser.cookies()).toEqual({});
});
});
describe('put via (cookieName, string)', function() {
it('should create and store a cookie', function() {
browser.cookies('cookieName', 'cookieValue');
expect(document.cookie).toEqual('cookieName=cookieValue');
expect(browser.cookies()).toEqual({'cookieName':'cookieValue'});
});
it('should overwrite an existing unsynced cookie', function() {
document.cookie = "cookie=new";
var oldVal = browser.cookies('cookie', 'newer');
expect(document.cookie).toEqual('cookie=newer');
expect(browser.cookies()).toEqual({'cookie':'newer'});
expect(oldVal).not.toBeDefined();
});
it('should escape both name and value', function() {
browser.cookies('cookie1=', 'val;ue');
browser.cookies('cookie2=bar;baz', 'val=ue');
var rawCookies = document.cookie.split("; "); //order is not guaranteed, so we need to parse
expect(rawCookies.length).toEqual(2);
expect(rawCookies).toContain('cookie1%3D=val%3Bue');
expect(rawCookies).toContain('cookie2%3Dbar%3Bbaz=val%3Due');
});
});
describe('get via (cookieName)', function() {
it('should return undefined for nonexistent cookie', function() {
expect(browser.cookies('nonexistent')).not.toBeDefined();
});
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');
});
it('should preserve leading & trailing spaces in names and values', function() {
browser.cookies(' cookie name ', ' cookie value ');
expect(browser.cookies()[' cookie name ']).toEqual(' cookie value ');
expect(browser.cookies()['cookie name']).not.toBeDefined();
});
});
describe('getAll', function() {
it('should return cookies as hash', function() {
document.cookie = "foo1=bar1";
document.cookie = "foo2=bar2";
expect(browser.cookies()).toEqual({'foo1':'bar1', 'foo2':'bar2'});
});
it('should return empty hash if no cookies exist', function() {
expect(browser.cookies()).toEqual({});
});
});
describe('watch', function() {
it('should allow listeners to be registered', function() {
expectAsserts(1);
browser.watchCookies(function(cookies) {
assertEquals({'aaa':'bbb'}, cookies);
});
browser.cookies('aaa','bbb');
browser.cookies();
});
it('should fire listeners when cookie changes are discovered', function() {
expectAsserts(1);
browser.watchCookies(function(cookies) {
assertEquals({'foo':'bar'}, cookies);
});
document.cookie = 'foo=bar';
browser.cookies();
});
it('should not fire listeners when no cookies were changed', function() {
expectAsserts(0);
browser.cookies(function(cookies) {
assertEquals({'shouldnt':'fire'}, cookies);
});
browser.cookies(true);
});
});
it('should pick up external changes made to browser cookies', function() {
browser.cookies('oatmealCookie', 'drool');
expect(browser.cookies()).toEqual({'oatmealCookie':'drool'});
document.cookie = 'oatmealCookie=changed';
browser.cookies(true);
expect(browser.cookies().oatmealCookie).toEqual('changed');
});
it('should initialize cookie cache with existing cookies', function() {
document.cookie = "existingCookie=existingValue";
expect(browser.cookies()).toEqual({'existingCookie':'existingValue'});
});
});
});

20
test/angular-mocks.js vendored
View file

@ -26,6 +26,7 @@ function MockBrowser() {
var self = this,
expectations = {},
requests = [];
this.isMock = true;
self.url = "http://server";
self.watches = [];
@ -72,6 +73,8 @@ function MockBrowser() {
requests.pop()();
}
};
self.cookieHash = {};
}
MockBrowser.prototype = {
@ -90,11 +93,28 @@ MockBrowser.prototype = {
this.watches.push(fn);
},
watchCookies: function(fn) {
this.watches.push(fn);
},
fireUrlWatchers: function() {
for(var i=0; i<this.watches.length; i++) {
this.watches[i](this.url);
}
},
cookies: function(name, value) {
if (name) {
if (value == undefined) {
delete this.cookieHash[name];
} else {
this.cookieHash[name] = ""+value;
}
} else {
return copy(this.cookieHash);
}
}
};
angular.service('$browser', function(){

View file

@ -38,7 +38,7 @@ describe("service", function(){
function warn(){ logger+= 'warn;'; };
function info(){ logger+= 'info;'; };
function error(){ logger+= 'error;'; };
var scope = createScope(null, angularService, {$window: {console:{log:log, warn:warn, info:info, error:error}}, $document:[{}]});
var scope = createScope(null, angularService, {$window: {console:{log:log, warn:warn, info:info, error:error}}, $document:[{cookie:''}]});
scope.$log.log();
scope.$log.warn();
scope.$log.info();
@ -49,7 +49,7 @@ describe("service", function(){
it('should use console.log if other not present', function(){
var logger = "";
function log(){ logger+= 'log;'; };
var scope = createScope(null, angularService, {$window: {console:{log:log}}, $document:[{}]});
var scope = createScope(null, angularService, {$window: {console:{log:log}}, $document:[{cookie:''}]});
scope.$log.log();
scope.$log.warn();
scope.$log.info();
@ -58,7 +58,7 @@ describe("service", function(){
});
it('should use noop if no console', function(){
var scope = createScope(null, angularService, {$window: {}, $document:[{}]});
var scope = createScope(null, angularService, {$window: {}, $document:[{cookie:''}]});
scope.$log.log();
scope.$log.warn();
scope.$log.info();
@ -371,4 +371,95 @@ describe("service", function(){
});
});
describe('$cookies', function() {
it('should provide access to existing cookies via object properties', function(){
expect(scope.$cookies).toEqual({});
scope.$browser.cookies('brandNew', 'cookie');
//TODO: This is a hacky way of calling the watch function, once pooling is refactored, this will go away.
scope.$browser.watches[1](scope.$browser.cookies());
expect(scope.$cookies).toEqual({'brandNew':'cookie'});
});
it('should create or update a cookie when a value is assigned to a property', function() {
scope.$cookies.oatmealCookie = 'nom nom';
scope.$eval();
expect(scope.$browser.cookies()).toEqual({'oatmealCookie':'nom nom'});
scope.$cookies.oatmealCookie = 'gone';
scope.$eval();
expect(scope.$browser.cookies()).toEqual({'oatmealCookie':'gone'});
});
it('should turn non-string into String when creating a cookie', function() {
scope.$cookies.nonString = [1, 2, 3];
scope.$eval();
expect(scope.$browser.cookies()).toEqual({'nonString':'1,2,3'});
});
it('should drop any null or undefined properties', function() {
scope.$cookies.nullVal = null;
scope.$cookies.undefVal = undefined;
scope.$eval();
expect(scope.$browser.cookies()).toEqual({});
});
it('should remove a cookie when a $cookies property is deleted', function() {
scope.$cookies.oatmealCookie = 'nom nom';
scope.$eval();
expect(scope.$browser.cookies()).toEqual({'oatmealCookie':'nom nom'});
delete scope.$cookies.oatmealCookie;
scope.$eval();
expect(scope.$browser.cookies()).toEqual({});
});
});
describe('$sessionStore', function() {
it('should serialize objects to json', function() {
scope.$sessionStore.put('objectCookie', {id: 123, name: 'blah'});
scope.$eval();
expect(scope.$browser.cookies()).toEqual({'objectCookie': '{"id":123,"name":"blah"}'});
});
it('should return all persisted items as a has via getAll', function() {
expect(scope.$sessionStore.getAll()).toEqual({});
scope.$sessionStore.put('object1', {id:1,foo:'bar1'});
scope.$sessionStore.put('object2', {id:2,foo:'bar2'});
expect(scope.$sessionStore.getAll()).toEqual({'object1':{id:1,foo:'bar1'},
'object2':{id:2,foo:'bar2'}});
});
it('should deserialize json to object', function() {
scope.$browser.cookies('objectCookie', '{"id":123,"name":"blah"}');
//TODO: This is a hacky way of calling the watch function, once pooling is refactored, this will go away.
scope.$browser.watches[1](scope.$browser.cookies());
expect(scope.$sessionStore.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"});
// TODO: Is this $eval necessary (why was it not here before?)
scope.$eval();
expect(scope.$browser.cookies()).toEqual({'gonner': '{"I\'ll":"Be Back"}'});
});
});
});