forked from angular/angular.js
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcookieReaderSpec.js
More file actions
104 lines (77 loc) · 3.28 KB
/
cookieReaderSpec.js
File metadata and controls
104 lines (77 loc) · 3.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
'use strict';
describe('$$cookieReader', function() {
var $$cookieReader;
function deleteAllCookies() {
var cookies = document.cookie.split(";");
var path = location.pathname;
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;
var parts = path.split('/');
while (parts.length) {
document.cookie = name + "=;path=" + (parts.join('/') || '/') + ";expires=Thu, 01 Jan 1970 00:00:00 GMT";
parts.pop();
}
}
}
beforeEach(function() {
deleteAllCookies();
expect(document.cookie).toEqual('');
inject(function(_$$cookieReader_) {
$$cookieReader = _$$cookieReader_;
});
});
afterEach(function() {
deleteAllCookies();
expect(document.cookie).toEqual('');
});
describe('get via $$cookieReader()[cookieName]', function() {
it('should return undefined for nonexistent cookie', function() {
expect($$cookieReader().nonexistent).not.toBeDefined();
});
it('should return a value for an existing cookie', function() {
document.cookie = "foo=bar=baz;path=/";
expect($$cookieReader().foo).toEqual('bar=baz');
});
it('should return the the first value provided for a cookie', function() {
// For a cookie that has different values that differ by path, the
// value for the most specific path appears first. $$cookieReader()
// should provide that value for the cookie.
document.cookie = 'foo="first"; foo="second"';
expect($$cookieReader()['foo']).toBe('"first"');
});
it('should decode cookie values that were encoded by puts', function() {
document.cookie = "cookie2%3Dbar%3Bbaz=val%3Due;path=/";
expect($$cookieReader()['cookie2=bar;baz']).toEqual('val=ue');
});
it('should preserve leading & trailing spaces in names and values', function() {
document.cookie = '%20cookie%20name%20=%20cookie%20value%20';
expect($$cookieReader()[' cookie name ']).toEqual(' cookie value ');
expect($$cookieReader()['cookie name']).not.toBeDefined();
});
it('should decode special characters in cookie values', function() {
document.cookie = 'cookie_name=cookie_value_%E2%82%AC';
expect($$cookieReader()['cookie_name']).toEqual('cookie_value_€');
});
it('should not decode cookie values that do not appear to be encoded', function() {
// see #9211 - sometimes cookies contain a value that causes decodeURIComponent to throw
document.cookie = 'cookie_name=cookie_value_%XX';
expect($$cookieReader()['cookie_name']).toEqual('cookie_value_%XX');
});
});
describe('getAll via $$cookieReader()', function() {
it('should return cookies as hash', function() {
document.cookie = "foo1=bar1;path=/";
document.cookie = "foo2=bar2;path=/";
expect($$cookieReader()).toEqual({'foo1':'bar1', 'foo2':'bar2'});
});
it('should return empty hash if no cookies exist', function() {
expect($$cookieReader()).toEqual({});
});
});
it('should initialize cookie cache with existing cookies', function() {
document.cookie = "existingCookie=existingValue;path=/";
expect($$cookieReader()).toEqual({'existingCookie':'existingValue'});
});
});