Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
81146 views
1
/*!
2
* Copyright (c) 2015, Salesforce.com, Inc.
3
* All rights reserved.
4
*
5
* Redistribution and use in source and binary forms, with or without
6
* modification, are permitted provided that the following conditions are met:
7
*
8
* 1. Redistributions of source code must retain the above copyright notice,
9
* this list of conditions and the following disclaimer.
10
*
11
* 2. Redistributions in binary form must reproduce the above copyright notice,
12
* this list of conditions and the following disclaimer in the documentation
13
* and/or other materials provided with the distribution.
14
*
15
* 3. Neither the name of Salesforce.com nor the names of its contributors may
16
* be used to endorse or promote products derived from this software without
17
* specific prior written permission.
18
*
19
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29
* POSSIBILITY OF SUCH DAMAGE.
30
*/
31
'use strict';
32
var vows = require('vows');
33
var assert = require('assert');
34
var async = require('async');
35
var tough = require('../lib/cookie');
36
var Cookie = tough.Cookie;
37
var CookieJar = tough.CookieJar;
38
39
40
var atNow = Date.now();
41
42
function at(offset) {
43
return {now: new Date(atNow + offset)};
44
}
45
46
vows
47
.describe('API')
48
.addBatch({
49
"All defined": function () {
50
assert.ok(Cookie);
51
assert.ok(CookieJar);
52
}
53
})
54
.addBatch({
55
"Constructor": {
56
topic: function () {
57
return new Cookie({
58
key: 'test',
59
value: 'b',
60
maxAge: 60
61
});
62
},
63
'check for key property': function (c) {
64
assert.ok(c);
65
assert.equal(c.key, 'test');
66
},
67
'check for value property': function (c) {
68
assert.equal(c.value, 'b');
69
},
70
'check for maxAge': function (c) {
71
assert.equal(c.maxAge, 60);
72
},
73
'check for default values for unspecified properties': function (c) {
74
assert.equal(c.expires, "Infinity");
75
assert.equal(c.secure, false);
76
assert.equal(c.httpOnly, false);
77
}
78
}
79
})
80
.addBatch({
81
"expiry option": {
82
topic: function () {
83
var cb = this.callback;
84
var cj = new CookieJar();
85
cj.setCookie('near=expiry; Domain=example.com; Path=/; Max-Age=1', 'http://www.example.com', at(-1), function (err, cookie) {
86
87
cb(err, {cj: cj, cookie: cookie});
88
});
89
},
90
"set the cookie": function (t) {
91
assert.ok(t.cookie, "didn't set?!");
92
assert.equal(t.cookie.key, 'near');
93
},
94
"then, retrieving": {
95
topic: function (t) {
96
var cb = this.callback;
97
setTimeout(function () {
98
t.cj.getCookies('http://www.example.com', {http: true, expire: false}, function (err, cookies) {
99
t.cookies = cookies;
100
cb(err, t);
101
});
102
}, 2000);
103
},
104
"got the cookie": function (t) {
105
assert.lengthOf(t.cookies, 1);
106
assert.equal(t.cookies[0].key, 'near');
107
}
108
}
109
}
110
})
111
.addBatch({
112
"allPaths option": {
113
topic: function () {
114
var cj = new CookieJar();
115
var tasks = [];
116
tasks.push(cj.setCookie.bind(cj, 'nopath_dom=qq; Path=/; Domain=example.com', 'http://example.com', {}));
117
tasks.push(cj.setCookie.bind(cj, 'path_dom=qq; Path=/foo; Domain=example.com', 'http://example.com', {}));
118
tasks.push(cj.setCookie.bind(cj, 'nopath_host=qq; Path=/', 'http://www.example.com', {}));
119
tasks.push(cj.setCookie.bind(cj, 'path_host=qq; Path=/foo', 'http://www.example.com', {}));
120
tasks.push(cj.setCookie.bind(cj, 'other=qq; Path=/', 'http://other.example.com/', {}));
121
tasks.push(cj.setCookie.bind(cj, 'other2=qq; Path=/foo', 'http://other.example.com/foo', {}));
122
var cb = this.callback;
123
async.parallel(tasks, function (err, results) {
124
cb(err, {cj: cj, cookies: results});
125
});
126
},
127
"all set": function (t) {
128
assert.equal(t.cookies.length, 6);
129
assert.ok(t.cookies.every(function (c) {
130
return !!c
131
}));
132
},
133
"getting without allPaths": {
134
topic: function (t) {
135
var cb = this.callback;
136
var cj = t.cj;
137
cj.getCookies('http://www.example.com/', {}, function (err, cookies) {
138
cb(err, {cj: cj, cookies: cookies});
139
});
140
},
141
"found just two cookies": function (t) {
142
assert.equal(t.cookies.length, 2);
143
},
144
"all are path=/": function (t) {
145
assert.ok(t.cookies.every(function (c) {
146
return c.path === '/'
147
}));
148
},
149
"no 'other' cookies": function (t) {
150
assert.ok(!t.cookies.some(function (c) {
151
return (/^other/).test(c.name)
152
}));
153
}
154
},
155
"getting without allPaths for /foo": {
156
topic: function (t) {
157
var cb = this.callback;
158
var cj = t.cj;
159
cj.getCookies('http://www.example.com/foo', {}, function (err, cookies) {
160
cb(err, {cj: cj, cookies: cookies});
161
});
162
},
163
"found four cookies": function (t) {
164
assert.equal(t.cookies.length, 4);
165
},
166
"no 'other' cookies": function (t) {
167
assert.ok(!t.cookies.some(function (c) {
168
return (/^other/).test(c.name)
169
}));
170
}
171
},
172
"getting with allPaths:true": {
173
topic: function (t) {
174
var cb = this.callback;
175
var cj = t.cj;
176
cj.getCookies('http://www.example.com/', {allPaths: true}, function (err, cookies) {
177
cb(err, {cj: cj, cookies: cookies});
178
});
179
},
180
"found four cookies": function (t) {
181
assert.equal(t.cookies.length, 4);
182
},
183
"no 'other' cookies": function (t) {
184
assert.ok(!t.cookies.some(function (c) {
185
return (/^other/).test(c.name)
186
}));
187
}
188
}
189
}
190
})
191
.addBatch({
192
"Remove cookies": {
193
topic: function () {
194
var jar = new CookieJar();
195
var cookie = Cookie.parse("a=b; Domain=example.com; Path=/");
196
var cookie2 = Cookie.parse("a=b; Domain=foo.com; Path=/");
197
var cookie3 = Cookie.parse("foo=bar; Domain=foo.com; Path=/");
198
jar.setCookie(cookie, 'http://example.com/index.html', function () {
199
});
200
jar.setCookie(cookie2, 'http://foo.com/index.html', function () {
201
});
202
jar.setCookie(cookie3, 'http://foo.com/index.html', function () {
203
});
204
return jar;
205
},
206
"all from matching domain": function (jar) {
207
jar.store.removeCookies('example.com', null, function (err) {
208
assert(err == null);
209
210
jar.store.findCookies('example.com', null, function (err, cookies) {
211
assert(err == null);
212
assert(cookies != null);
213
assert(cookies.length === 0, 'cookie was not removed');
214
});
215
216
jar.store.findCookies('foo.com', null, function (err, cookies) {
217
assert(err == null);
218
assert(cookies != null);
219
assert(cookies.length === 2, 'cookies should not have been removed');
220
});
221
});
222
},
223
"from cookie store matching domain and key": function (jar) {
224
jar.store.removeCookie('foo.com', '/', 'foo', function (err) {
225
assert(err == null);
226
227
jar.store.findCookies('foo.com', null, function (err, cookies) {
228
assert(err == null);
229
assert(cookies != null);
230
assert(cookies.length === 1, 'cookie was not removed correctly');
231
assert(cookies[0].key === 'a', 'wrong cookie was removed');
232
});
233
});
234
}
235
}
236
})
237
.addBatch({
238
"Synchronous CookieJar": {
239
"setCookieSync": {
240
topic: function () {
241
var jar = new CookieJar();
242
var cookie = Cookie.parse("a=b; Domain=example.com; Path=/");
243
cookie = jar.setCookieSync(cookie, 'http://example.com/index.html');
244
return cookie;
245
},
246
"returns a copy of the cookie": function (cookie) {
247
assert.instanceOf(cookie, Cookie);
248
}
249
},
250
"getCookiesSync": {
251
topic: function () {
252
var jar = new CookieJar();
253
var url = 'http://example.com/index.html';
254
jar.setCookieSync("a=b; Domain=example.com; Path=/", url);
255
jar.setCookieSync("c=d; Domain=example.com; Path=/", url);
256
return jar.getCookiesSync(url);
257
},
258
"returns the cookie array": function (err, cookies) {
259
assert.ok(!err);
260
assert.ok(Array.isArray(cookies));
261
assert.lengthOf(cookies, 2);
262
cookies.forEach(function (cookie) {
263
assert.instanceOf(cookie, Cookie);
264
});
265
}
266
},
267
268
"getCookieStringSync": {
269
topic: function () {
270
var jar = new CookieJar();
271
var url = 'http://example.com/index.html';
272
jar.setCookieSync("a=b; Domain=example.com; Path=/", url);
273
jar.setCookieSync("c=d; Domain=example.com; Path=/", url);
274
return jar.getCookieStringSync(url);
275
},
276
"returns the cookie header string": function (err, str) {
277
assert.ok(!err);
278
assert.typeOf(str, 'string');
279
}
280
},
281
282
"getSetCookieStringsSync": {
283
topic: function () {
284
var jar = new CookieJar();
285
var url = 'http://example.com/index.html';
286
jar.setCookieSync("a=b; Domain=example.com; Path=/", url);
287
jar.setCookieSync("c=d; Domain=example.com; Path=/", url);
288
return jar.getSetCookieStringsSync(url);
289
},
290
"returns the cookie header string": function (err, headers) {
291
assert.ok(!err);
292
assert.ok(Array.isArray(headers));
293
assert.lengthOf(headers, 2);
294
headers.forEach(function (header) {
295
assert.typeOf(header, 'string');
296
});
297
}
298
}
299
}
300
})
301
.addBatch({
302
"Synchronous API on async CookieJar": {
303
topic: function () {
304
return new tough.Store();
305
},
306
"setCookieSync": {
307
topic: function (store) {
308
var jar = new CookieJar(store);
309
try {
310
jar.setCookieSync("a=b", 'http://example.com/index.html');
311
return false;
312
} catch (e) {
313
return e;
314
}
315
},
316
"fails": function (err) {
317
assert.instanceOf(err, Error);
318
assert.equal(err.message,
319
'CookieJar store is not synchronous; use async API instead.');
320
}
321
},
322
"getCookiesSync": {
323
topic: function (store) {
324
var jar = new CookieJar(store);
325
try {
326
jar.getCookiesSync('http://example.com/index.html');
327
return false;
328
} catch (e) {
329
return e;
330
}
331
},
332
"fails": function (err) {
333
assert.instanceOf(err, Error);
334
assert.equal(err.message,
335
'CookieJar store is not synchronous; use async API instead.');
336
}
337
},
338
"getCookieStringSync": {
339
topic: function (store) {
340
var jar = new CookieJar(store);
341
try {
342
jar.getCookieStringSync('http://example.com/index.html');
343
return false;
344
} catch (e) {
345
return e;
346
}
347
},
348
"fails": function (err) {
349
assert.instanceOf(err, Error);
350
assert.equal(err.message,
351
'CookieJar store is not synchronous; use async API instead.');
352
}
353
},
354
"getSetCookieStringsSync": {
355
topic: function (store) {
356
var jar = new CookieJar(store);
357
try {
358
jar.getSetCookieStringsSync('http://example.com/index.html');
359
return false;
360
} catch (e) {
361
return e;
362
}
363
},
364
"fails": function (err) {
365
assert.instanceOf(err, Error);
366
assert.equal(err.message,
367
'CookieJar store is not synchronous; use async API instead.');
368
}
369
}
370
}
371
})
372
.export(module);
373
374