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
32
'use strict';
33
var vows = require('vows');
34
var assert = require('assert');
35
var tough = require('../lib/cookie');
36
var Cookie = tough.Cookie;
37
38
vows
39
.describe('Parsing')
40
.addBatch({
41
"simple": {
42
topic: function() {
43
return Cookie.parse('a=bcd') || null;
44
},
45
"parsed": function(c) { assert.ok(c) },
46
"key": function(c) { assert.equal(c.key, 'a') },
47
"value": function(c) { assert.equal(c.value, 'bcd') },
48
"no path": function(c) { assert.equal(c.path, null) },
49
"no domain": function(c) { assert.equal(c.domain, null) },
50
"no extensions": function(c) { assert.ok(!c.extensions) }
51
},
52
"with expiry": {
53
topic: function() {
54
return Cookie.parse('a=bcd; Expires=Tue, 18 Oct 2011 07:05:03 GMT') || null;
55
},
56
"parsed": function(c) { assert.ok(c) },
57
"key": function(c) { assert.equal(c.key, 'a') },
58
"value": function(c) { assert.equal(c.value, 'bcd') },
59
"has expires": function(c) {
60
assert.ok(c.expires !== Infinity, 'expiry is infinite when it shouldn\'t be');
61
assert.equal(c.expires.getTime(), 1318921503000);
62
}
63
},
64
"with expiry and path": {
65
topic: function() {
66
return Cookie.parse('abc="xyzzy!"; Expires=Tue, 18 Oct 2011 07:05:03 GMT; Path=/aBc') || null;
67
},
68
"parsed": function(c) { assert.ok(c) },
69
"key": function(c) { assert.equal(c.key, 'abc') },
70
"value": function(c) { assert.equal(c.value, '"xyzzy!"') },
71
"has expires": function(c) {
72
assert.ok(c.expires !== Infinity, 'expiry is infinite when it shouldn\'t be');
73
assert.equal(c.expires.getTime(), 1318921503000);
74
},
75
"has path": function(c) { assert.equal(c.path, '/aBc'); },
76
"no httponly or secure": function(c) {
77
assert.ok(!c.httpOnly);
78
assert.ok(!c.secure);
79
}
80
},
81
"with everything": {
82
topic: function() {
83
return Cookie.parse('abc="xyzzy!"; Expires=Tue, 18 Oct 2011 07:05:03 GMT; Path=/aBc; Domain=example.com; Secure; HTTPOnly; Max-Age=1234; Foo=Bar; Baz') || null;
84
},
85
"parsed": function(c) { assert.ok(c) },
86
"key": function(c) { assert.equal(c.key, 'abc') },
87
"value": function(c) { assert.equal(c.value, '"xyzzy!"') },
88
"has expires": function(c) {
89
assert.ok(c.expires !== Infinity, 'expiry is infinite when it shouldn\'t be');
90
assert.equal(c.expires.getTime(), 1318921503000);
91
},
92
"has path": function(c) { assert.equal(c.path, '/aBc'); },
93
"has domain": function(c) { assert.equal(c.domain, 'example.com'); },
94
"has httponly": function(c) { assert.equal(c.httpOnly, true); },
95
"has secure": function(c) { assert.equal(c.secure, true); },
96
"has max-age": function(c) { assert.equal(c.maxAge, 1234); },
97
"has extensions": function(c) {
98
assert.ok(c.extensions);
99
assert.equal(c.extensions[0], 'Foo=Bar');
100
assert.equal(c.extensions[1], 'Baz');
101
}
102
},
103
"invalid expires": function() {
104
var c = Cookie.parse("a=b; Expires=xyzzy");
105
assert.ok(c);
106
assert.equal(c.expires, Infinity);
107
},
108
"zero max-age": function() {
109
var c = Cookie.parse("a=b; Max-Age=0");
110
assert.ok(c);
111
assert.equal(c.maxAge, 0);
112
},
113
"negative max-age": function() {
114
var c = Cookie.parse("a=b; Max-Age=-1");
115
assert.ok(c);
116
assert.equal(c.maxAge, -1);
117
},
118
"empty domain": function() {
119
var c = Cookie.parse("a=b; domain=");
120
assert.ok(c);
121
assert.equal(c.domain, null);
122
},
123
"dot domain": function() {
124
var c = Cookie.parse("a=b; domain=.");
125
assert.ok(c);
126
assert.equal(c.domain, null);
127
},
128
"uppercase domain": function() {
129
var c = Cookie.parse("a=b; domain=EXAMPLE.COM");
130
assert.ok(c);
131
assert.equal(c.domain, 'example.com');
132
},
133
"trailing dot in domain": {
134
topic: function() {
135
return Cookie.parse("a=b; Domain=example.com.", true) || null;
136
},
137
"has the domain": function(c) { assert.equal(c.domain,"example.com.") },
138
"but doesn't validate": function(c) { assert.equal(c.validate(),false) }
139
},
140
"empty path": function() {
141
var c = Cookie.parse("a=b; path=");
142
assert.ok(c);
143
assert.equal(c.path, null);
144
},
145
"no-slash path": function() {
146
var c = Cookie.parse("a=b; path=xyzzy");
147
assert.ok(c);
148
assert.equal(c.path, null);
149
},
150
"trailing semi-colons after path": {
151
topic: function () {
152
return [
153
"a=b; path=/;",
154
"c=d;;;;"
155
];
156
},
157
"strips semi-colons": function (t) {
158
var c1 = Cookie.parse(t[0]);
159
var c2 = Cookie.parse(t[1]);
160
assert.ok(c1);
161
assert.ok(c2);
162
assert.equal(c1.path, '/');
163
}
164
},
165
"secure-with-value": function() {
166
var c = Cookie.parse("a=b; Secure=xyzzy");
167
assert.ok(c);
168
assert.equal(c.secure, true);
169
},
170
"httponly-with-value": function() {
171
var c = Cookie.parse("a=b; HttpOnly=xyzzy");
172
assert.ok(c);
173
assert.equal(c.httpOnly, true);
174
},
175
"garbage": {
176
topic: function() {
177
return Cookie.parse("\x08", true) || null;
178
},
179
"doesn't parse": function(c) { assert.equal(c,null) }
180
},
181
"public suffix domain": {
182
topic: function() {
183
return Cookie.parse("a=b; domain=kyoto.jp", true) || null;
184
},
185
"parses fine": function(c) {
186
assert.ok(c);
187
assert.equal(c.domain, 'kyoto.jp');
188
},
189
"but fails validation": function(c) {
190
assert.ok(c);
191
assert.ok(!c.validate());
192
}
193
},
194
"public suffix foonet.net": {
195
"top level": {
196
topic: function() {
197
return Cookie.parse("a=b; domain=foonet.net") || null;
198
},
199
"parses and is valid": function(c) {
200
assert.ok(c);
201
assert.equal(c.domain, 'foonet.net');
202
assert.ok(c.validate());
203
}
204
},
205
"www": {
206
topic: function() {
207
return Cookie.parse("a=b; domain=www.foonet.net") || null;
208
},
209
"parses and is valid": function(c) {
210
assert.ok(c);
211
assert.equal(c.domain, 'www.foonet.net');
212
assert.ok(c.validate());
213
}
214
},
215
"with a dot": {
216
topic: function() {
217
return Cookie.parse("a=b; domain=.foonet.net") || null;
218
},
219
"parses and is valid": function(c) {
220
assert.ok(c);
221
assert.equal(c.domain, 'foonet.net');
222
assert.ok(c.validate());
223
}
224
}
225
},
226
"Ironically, Google 'GAPS' cookie has very little whitespace": {
227
topic: function() {
228
return Cookie.parse("GAPS=1:A1aaaaAaAAa1aaAaAaaAAAaaa1a11a:aaaAaAaAa-aaaA1-;Path=/;Expires=Thu, 17-Apr-2014 02:12:29 GMT;Secure;HttpOnly");
229
},
230
"parsed": function(c) { assert.ok(c) },
231
"key": function(c) { assert.equal(c.key, 'GAPS') },
232
"value": function(c) { assert.equal(c.value, '1:A1aaaaAaAAa1aaAaAaaAAAaaa1a11a:aaaAaAaAa-aaaA1-') },
233
"path": function(c) {
234
assert.notEqual(c.path, '/;Expires'); // BUG
235
assert.equal(c.path, '/');
236
},
237
"expires": function(c) {
238
assert.notEqual(c.expires, Infinity);
239
assert.equal(c.expires.getTime(), 1397700749000);
240
},
241
"secure": function(c) { assert.ok(c.secure) },
242
"httponly": function(c) { assert.ok(c.httpOnly) }
243
},
244
"lots of equal signs": {
245
topic: function() {
246
return Cookie.parse("queryPref=b=c&d=e; Path=/f=g; Expires=Thu, 17 Apr 2014 02:12:29 GMT; HttpOnly");
247
},
248
"parsed": function(c) { assert.ok(c) },
249
"key": function(c) { assert.equal(c.key, 'queryPref') },
250
"value": function(c) { assert.equal(c.value, 'b=c&d=e') },
251
"path": function(c) {
252
assert.equal(c.path, '/f=g');
253
},
254
"expires": function(c) {
255
assert.notEqual(c.expires, Infinity);
256
assert.equal(c.expires.getTime(), 1397700749000);
257
},
258
"httponly": function(c) { assert.ok(c.httpOnly) }
259
},
260
"spaces in value": {
261
topic: function() {
262
return Cookie.parse('a=one two three',false) || null;
263
},
264
"parsed": function(c) { assert.ok(c) },
265
"key": function(c) { assert.equal(c.key, 'a') },
266
"value": function(c) { assert.equal(c.value, 'one two three') },
267
"no path": function(c) { assert.equal(c.path, null) },
268
"no domain": function(c) { assert.equal(c.domain, null) },
269
"no extensions": function(c) { assert.ok(!c.extensions) }
270
},
271
"quoted spaces in value": {
272
topic: function() {
273
return Cookie.parse('a="one two three"',false) || null;
274
},
275
"parsed": function(c) { assert.ok(c) },
276
"key": function(c) { assert.equal(c.key, 'a') },
277
"value": function(c) { assert.equal(c.value, '"one two three"') },
278
"no path": function(c) { assert.equal(c.path, null) },
279
"no domain": function(c) { assert.equal(c.domain, null) },
280
"no extensions": function(c) { assert.ok(!c.extensions) }
281
},
282
"non-ASCII in value": {
283
topic: function() {
284
return Cookie.parse('farbe=weiß',false) || null;
285
},
286
"parsed": function(c) { assert.ok(c) },
287
"key": function(c) { assert.equal(c.key, 'farbe') },
288
"value": function(c) { assert.equal(c.value, 'weiß') },
289
"no path": function(c) { assert.equal(c.path, null) },
290
"no domain": function(c) { assert.equal(c.domain, null) },
291
"no extensions": function(c) { assert.ok(!c.extensions) }
292
}
293
})
294
.export(module);
295
296