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('Cookie.toJSON()')
40
.addBatch({
41
"JSON": {
42
"serialization": {
43
topic: function() {
44
var c = Cookie.parse('alpha=beta; Domain=example.com; Path=/foo; Expires=Tue, 19 Jan 2038 03:14:07 GMT; HttpOnly');
45
return JSON.stringify(c);
46
},
47
"gives a string": function(str) {
48
assert.equal(typeof str, "string");
49
},
50
"date is in ISO format": function(str) {
51
assert.match(str, /"expires":"2038-01-19T03:14:07\.000Z"/, 'expires is in ISO format');
52
}
53
},
54
"deserialization": {
55
topic: function() {
56
var json = '{"key":"alpha","value":"beta","domain":"example.com","path":"/foo","expires":"2038-01-19T03:14:07.000Z","httpOnly":true,"lastAccessed":2000000000123}';
57
return Cookie.fromJSON(json);
58
},
59
"works": function(c) {
60
assert.ok(c);
61
},
62
"key": function(c) { assert.equal(c.key, "alpha") },
63
"value": function(c) { assert.equal(c.value, "beta") },
64
"domain": function(c) { assert.equal(c.domain, "example.com") },
65
"path": function(c) { assert.equal(c.path, "/foo") },
66
"httpOnly": function(c) { assert.strictEqual(c.httpOnly, true) },
67
"secure": function(c) { assert.strictEqual(c.secure, false) },
68
"hostOnly": function(c) { assert.strictEqual(c.hostOnly, null) },
69
"expires is a date object": function(c) {
70
assert.equal(c.expires.getTime(), 2147483647000);
71
},
72
"lastAccessed is a date object": function(c) {
73
assert.equal(c.lastAccessed.getTime(), 2000000000123);
74
},
75
"creation defaulted": function(c) {
76
assert.ok(c.creation.getTime());
77
}
78
},
79
"null deserialization": {
80
topic: function() {
81
return Cookie.fromJSON(null);
82
},
83
"is null": function(cookie) {
84
assert.equal(cookie,null);
85
}
86
}
87
},
88
"expiry deserialization": {
89
"Infinity": {
90
topic: Cookie.fromJSON.bind(null, '{"expires":"Infinity"}'),
91
"is infinite": function(c) {
92
assert.strictEqual(c.expires, "Infinity");
93
assert.equal(c.expires, Infinity);
94
}
95
}
96
},
97
"maxAge serialization": {
98
topic: function() {
99
return function(toSet) {
100
var c = new Cookie();
101
c.key = 'foo'; c.value = 'bar';
102
c.setMaxAge(toSet);
103
return JSON.stringify(c);
104
};
105
},
106
"zero": {
107
topic: function(f) { return f(0) },
108
"looks good": function(str) {
109
assert.match(str, /"maxAge":0/);
110
}
111
},
112
"Infinity": {
113
topic: function(f) { return f(Infinity) },
114
"looks good": function(str) {
115
assert.match(str, /"maxAge":"Infinity"/);
116
}
117
},
118
"-Infinity": {
119
topic: function(f) { return f(-Infinity) },
120
"looks good": function(str) {
121
assert.match(str, /"maxAge":"-Infinity"/);
122
}
123
},
124
"null": {
125
topic: function(f) { return f(null) },
126
"looks good": function(str) {
127
assert.match(str, /"maxAge":null/);
128
}
129
}
130
},
131
"maxAge deserialization": {
132
"number": {
133
topic: Cookie.fromJSON.bind(null,'{"key":"foo","value":"bar","maxAge":123}'),
134
"is the number": function(c) {
135
assert.strictEqual(c.maxAge, 123);
136
}
137
},
138
"null": {
139
topic: Cookie.fromJSON.bind(null,'{"key":"foo","value":"bar","maxAge":null}'),
140
"is null": function(c) {
141
assert.strictEqual(c.maxAge, null);
142
}
143
},
144
"less than zero": {
145
topic: Cookie.fromJSON.bind(null,'{"key":"foo","value":"bar","maxAge":-123}'),
146
"is -123": function(c) {
147
assert.strictEqual(c.maxAge, -123);
148
}
149
},
150
"Infinity": {
151
topic: Cookie.fromJSON.bind(null,'{"key":"foo","value":"bar","maxAge":"Infinity"}'),
152
"is inf-as-string": function(c) {
153
assert.strictEqual(c.maxAge, "Infinity");
154
}
155
},
156
"-Infinity": {
157
topic: Cookie.fromJSON.bind(null,'{"key":"foo","value":"bar","maxAge":"-Infinity"}'),
158
"is inf-as-string": function(c) {
159
assert.strictEqual(c.maxAge, "-Infinity");
160
}
161
}
162
}
163
})
164
.export(module);
165
166