Skip to content
This repository has been archived by the owner on Feb 22, 2018. It is now read-only.

Commit

Permalink
feat(cookies): Basic Cookies service/wrapper over BrowserCookies
Browse files Browse the repository at this point in the history
  • Loading branch information
kurman authored and mhevery committed Dec 13, 2013
1 parent 0559068 commit 6efde83
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 27 deletions.
31 changes: 30 additions & 1 deletion lib/core_dom/cookies.dart
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ part of angular.core.dom;
* It is not meant to be used directly by applications. Instead
* use the Cookies service.
*
* NOTE the Cookies service is not yet implemented.
*/
@NgInjectableService()
class BrowserCookies {
Expand Down Expand Up @@ -102,3 +101,33 @@ class BrowserCookies {

get all => _updateLastCookies();
}

/**
* Cookies service
*/
class Cookies {

BrowserCookies _browserCookies;

Cookies(BrowserCookies this._browserCookies);

/**
* Returns the value of given cookie key
*/
operator[](name) => this._browserCookies[name];

/**
* Sets a value for given cookie key
*/
operator[]=(name, value) {
this._browserCookies[name] = value;
}

/**
* Remove given cookie
*/
remove(name) {
this._browserCookies[name] = null;
}
}

1 change: 1 addition & 0 deletions lib/core_dom/module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class NgCoreDomModule extends Module {
type(BlockCache);
type(GetterSetter);
type(BrowserCookies);
type(Cookies);
type(LocationWrapper);
}
}
Expand Down
82 changes: 56 additions & 26 deletions test/core_dom/cookies_spec.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,32 @@ library cookies_spec;
import '../_specs.dart';
import 'package:angular/core_dom/module.dart';

main() => describe('browser cookies', () {
var cookies;

deleteAllCookies() {
var cookies = document.cookie.split(";");
var path = window.location.pathname;

for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
var eqPos = cookie.indexOf("=");
var name = eqPos > -1 ? cookie.substring(0, eqPos) : '';
var parts = path.split('/');
while (!parts.isEmpty) {
var joinedParts = parts.join('/');
document.cookie = name + "=;path=" + (joinedParts.isEmpty ? '/': joinedParts) +
";expires=Thu, 01 Jan 1970 00:00:00 GMT";
parts.removeLast();
}
main() => describe('cookies', () {
deleteAllCookies() {
var cookies = document.cookie.split(";");
var path = window.location.pathname;

for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
var eqPos = cookie.indexOf("=");
var name = eqPos > -1 ? cookie.substring(0, eqPos) : '';
var parts = path.split('/');
while (!parts.isEmpty) {
var joinedParts = parts.join('/');
document.cookie = name + "=;path=" + (joinedParts.isEmpty ? '/': joinedParts) +
";expires=Thu, 01 Jan 1970 00:00:00 GMT";
parts.removeLast();
}
}
}

afterEach(() {
deleteAllCookies();
expect(document.cookie).toEqual('');
});

describe('browser cookies', () {
var cookies;

beforeEach(inject((BrowserCookies iCookies) {
deleteAllCookies();
Expand All @@ -31,13 +37,6 @@ main() => describe('browser cookies', () {
cookies = iCookies;
}));


afterEach(() {
deleteAllCookies();
expect(document.cookie).toEqual('');
});


describe('remove via cookies(cookieName, null)', () {

it('should remove a cookie when it is present', () {
Expand Down Expand Up @@ -114,7 +113,7 @@ main() => describe('browser cookies', () {

if (document.cookie != cookieStr) {
throw "browser didn't drop long cookie when it was expected. make the " +
"cookie in this test longer";
"cookie in this test longer";
}

expect(cookies['x']).toEqual('shortVal');
Expand Down Expand Up @@ -196,4 +195,35 @@ main() => describe('browser cookies', () {
document.cookie = "existingCookie=existingValue;path=/";
expect(cookies.all).toEqual({'existingCookie':'existingValue'});
});
});

describe('cookies service', () {
var cookiesService;
beforeEach(inject((Cookies iCookies) {
cookiesService = iCookies;
document.cookie = 'oatmealCookie=fresh;path=/';
}));

it('should read cookie', () {
expect(cookiesService["oatmealCookie"]).toEqual("fresh");
});

describe("set cookie", () {
it('should set new key value pair', () {
cookiesService["oven"] = "hot";
expect(document.cookie).toContain("oven=hot");
});

it('should override existing value', () {
cookiesService["oatmealCookie"] = "stale";
expect(document.cookie).toContain("oatmealCookie=stale");
});
});

it('should remove cookie', () {
cookiesService.remove("oatmealCookie");
expect(document.cookie).not.toContain("oatmealCookie");
});
});
});

0 comments on commit 6efde83

Please sign in to comment.