-
Notifications
You must be signed in to change notification settings - Fork 0
/
cookies.spec.js
280 lines (215 loc) · 8.55 KB
/
cookies.spec.js
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
describe("cookies", function() {
"use strict";
var cookies = require("../src/cookies");
var utils = require("../src/utils");
function clearCookies() {
var cookies = document.cookie.split(";");
for (var index = 0; index < cookies.length; index++) {
var cookie = cookies[index];
deleteCookie(cookie.split("=")[0]);
}
function deleteCookie(name) {
var pathSplit = location.pathname.split('/');
var pathCurrent = ' path=';
document.cookie = name + '=; expires=Thu, 01-Jan-1970 00:00:01 GMT;';
for (var index = 0; index < pathSplit.length; index++) {
var pathPart = pathSplit[index];
pathCurrent += ((pathCurrent.substr(-1) != '/') ? '/' : '') + pathPart;
document.cookie = name + '=; expires=Thu, 01-Jan-1970 00:00:01 GMT;' + pathCurrent + ';';
}
}
}
beforeEach(function() {
clearCookies();
expect(document.cookie).toEqual("");
});
// Could add last spec to do this but make sure we keep the cookies cleared after running
// all specs
afterEach(function() {
clearCookies();
});
describe("get", function() {
it("should return for existing key", function(){
document.cookie = "a=b";
expect(cookies.get("a")).toEqual("b");
});
it("should return null for unknown key", function() {
document.cookie = "c=b";
expect(cookies.get("a")).toEqual(null);
});
it("should return null for null key", function() {
expect(cookies.get(null)).toEqual(null);
});
it("should return null for undefined key", function() {
expect(cookies.get()).toEqual(null);
});
it("should return empty value", function() {
document.cookie = "a=";
expect(cookies.get("a")).toEqual("");
});
it("should unescape", function() {
document.cookie = "a=" + encodeURIComponent("$");
expect(cookies.get("a")).toEqual("$");
});
it("should handle quoted", function() {
document.cookie = 'a="b@c.com\\"\\\\\\""';
expect(cookies.get("a")).toEqual('b@c.com"\\"');
});
it("should parse json", function() {
document.cookie = "a=" + utils.toJson({b:"c"});
expect(cookies.get("a")).toEqual({b: "c"});
});
it("should return raw for malformed json", function(){
document.cookie = "a={b:";
expect(cookies.get("a")).toEqual("{b:");
});
it("should preserve leading and trailing spaces", function() {
document.cookie = "a=" + encodeURIComponent(" b ");
expect(cookies.get("a")).toEqual(" b ");
});
});
describe("set", function() {
it("should save", function() {
cookies.set("a", "b");
expect(cookies.get("a")).toEqual("b");
});
it("should save number", function() {
cookies.set("a", 1);
expect(cookies.get("a")).toEqual(1);
});
it("should save quoted string", function() {
cookies.set("a", 'b\"');
expect(cookies.get("a")).toEqual('b"');
});
it("should save object", function() {
cookies.set("a", {b: "c"});
expect(cookies.get("a")).toEqual({b: "c"});
});
it("should save special characters", function() {
cookies.set("a", "$");
expect(cookies.get("a")).toEqual("$");
});
it("should save empty value", function() {
cookies.set("a", "");
expect(cookies.get("a")).toEqual("");
});
it("should save null", function() {
cookies.set("a", null);
expect(cookies.get("a")).toEqual(null);
});
it("should save undefined as string", function() {
cookies.set("a", undefined);
expect(cookies.get("a")).toEqual("undefined");
});
it("should save leading and trailing spaces", function() {
cookies.set(" a ", " b ");
expect(cookies.get(" a ")).toEqual(" b ");
});
it("should save with expires days from now", function() {
var oneDayFromNow = new Date();
oneDayFromNow.setDate(oneDayFromNow.getDate() + 1);
var expected = "a=b; expires=" + oneDayFromNow.toUTCString();
expect(cookies.set("a", "b", {expires:1})).toEqual(expected);
});
it("should save with expires as Date instance", function() {
var date = new Date();
var expected = "a=b; expires=" + date.toUTCString();
expect(cookies.set("a", "b", {expires:date})).toEqual(expected);
});
it("should save with domain", function() {
expect(cookies.set("a", "b", {domain:".test.com"})).toEqual("a=b; domain=.test.com");
});
it("should save with path", function() {
expect(cookies.set("a", "b", {path:"/test"})).toEqual("a=b; path=/test");
});
it("should save with secure", function() {
expect(cookies.set("a", "b", {secure: true})).toEqual("a=b; secure");
});
});
describe("remove", function() {
it("should remove", function() {
cookies.set("a", "b");
cookies.remove("a");
expect(document.cookie).toEqual("");
});
it("should return true if exists", function(){
cookies.set("a", "b");
expect(cookies.remove("a")).toEqual(true);
});
it("should return false if doesn't exist", function() {
expect(cookies.remove("a")).toEqual(false);
});
it("should not throw error for null key", function(){
cookies.remove(null);
});
it("should not throw error for undefined key", function(){
cookies.remove(undefined);
});
// can't fully test domain or path, but lets do what we can...
it("should remove with same path specified", function() {
cookies.set("a", "b", {path: "/"});
cookies.remove("a", {path: "/"});
expect(document.cookie).toEqual("");
});
it("should not remove if different path", function(){
cookies.set("a", "b", {path: "/"});
cookies.remove("a", {path: "/a"});
expect(document.cookie).toEqual("a=b");
});
it("should not remove if different domain", function() {
cookies.set("a", "b");
cookies.remove("a", {domain: ".test.com"});
expect(document.cookie).toEqual("a=b");
});
});
describe("exists", function() {
it("should return true if exists", function() {
cookies.set("a", "b");
expect(cookies.exists("a")).toEqual(true);
});
it("should return false if only exists for different path", function() {
cookies.set("a", "b", {path: "/a"});
expect(cookies.exists("a")).toEqual(false);
});
it("should return false if only exists for different domain", function() {
cookies.set("a", "b", {domain: ".test.com"});
expect(cookies.exists("a")).toEqual(false);
});
it("should return false if doesn't exist", function() {
expect(cookies.exists("a")).toEqual(false);
});
it("should return false for null key", function() {
expect(cookies.exists(null)).toEqual(false);
});
it("should return false for undefined key", function() {
expect(cookies.exists(undefined)).toEqual(false);
});
});
it("should clear all cookies", function(){
cookies.set("a", 1);
cookies.set("b", 2);
cookies.set("c", 3);
cookies.clear();
expect(document.cookie).toEqual("");
});
describe("forEach", function() {
it("should iterate all cookies", function() {
cookies.set("a", 1);
cookies.set("b", 2);
var expected = [["a", 1], ["b", 2]];
var index = 0;
cookies.forEach(function(value, name) {
expect(expected[index]).toEqual([name, value]);
index++;
});
});
it("should call iterator with context", function() {
cookies.set("a", 1);
var context = null;
cookies.forEach(function() {
context = this;
}, this);
expect(context).toBe(this);
});
});
});