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.toString()')
40
.addBatch({
41
"a simple cookie": {
42
topic: function () {
43
var c = new Cookie();
44
c.key = 'a';
45
c.value = 'b';
46
return c;
47
},
48
"validates": function (c) {
49
assert.ok(c.validate());
50
},
51
"to string": function (c) {
52
assert.equal(c.toString(), 'a=b');
53
}
54
},
55
"a cookie with spaces in the value": {
56
topic: function () {
57
var c = new Cookie();
58
c.key = 'a';
59
c.value = 'beta gamma';
60
return c;
61
},
62
"doesn't validate": function (c) {
63
assert.ok(!c.validate());
64
},
65
"'garbage in, garbage out'": function (c) {
66
assert.equal(c.toString(), 'a=beta gamma');
67
}
68
},
69
"with an empty value and HttpOnly": {
70
topic: function () {
71
var c = new Cookie();
72
c.key = 'a';
73
c.httpOnly = true;
74
return c;
75
},
76
"to string": function (c) {
77
assert.equal(c.toString(), 'a=; HttpOnly');
78
}
79
},
80
"with an expiry": {
81
topic: function () {
82
var c = new Cookie();
83
c.key = 'a';
84
c.value = 'b';
85
c.setExpires("Oct 18 2011 07:05:03 GMT");
86
return c;
87
},
88
"validates": function (c) {
89
assert.ok(c.validate());
90
},
91
"to string": function (c) {
92
assert.equal(c.toString(), 'a=b; Expires=Tue, 18 Oct 2011 07:05:03 GMT');
93
},
94
"to short string": function (c) {
95
assert.equal(c.cookieString(), 'a=b');
96
}
97
},
98
"with a max-age": {
99
topic: function () {
100
var c = new Cookie();
101
c.key = 'a';
102
c.value = 'b';
103
c.setExpires("Oct 18 2011 07:05:03 GMT");
104
c.maxAge = 12345;
105
return c;
106
},
107
"validates": function (c) {
108
assert.ok(c.validate()); // mabe this one *shouldn't*?
109
},
110
"to string": function (c) {
111
assert.equal(c.toString(), 'a=b; Expires=Tue, 18 Oct 2011 07:05:03 GMT; Max-Age=12345');
112
}
113
},
114
"with a bunch of things": function () {
115
var c = new Cookie();
116
c.key = 'a';
117
c.value = 'b';
118
c.setExpires("Oct 18 2011 07:05:03 GMT");
119
c.maxAge = 12345;
120
c.domain = 'example.com';
121
c.path = '/foo';
122
c.secure = true;
123
c.httpOnly = true;
124
c.extensions = ['MyExtension'];
125
assert.equal(c.toString(), 'a=b; Expires=Tue, 18 Oct 2011 07:05:03 GMT; Max-Age=12345; Domain=example.com; Path=/foo; Secure; HttpOnly; MyExtension');
126
},
127
"a host-only cookie": {
128
topic: function () {
129
var c = new Cookie();
130
c.key = 'a';
131
c.value = 'b';
132
c.hostOnly = true;
133
c.domain = 'shouldnt-stringify.example.com';
134
c.path = '/should-stringify';
135
return c;
136
},
137
"validates": function (c) {
138
assert.ok(c.validate());
139
},
140
"to string": function (c) {
141
assert.equal(c.toString(), 'a=b; Path=/should-stringify');
142
}
143
},
144
"minutes are '10'": {
145
topic: function () {
146
var c = new Cookie();
147
c.key = 'a';
148
c.value = 'b';
149
c.expires = new Date(1284113410000);
150
return c;
151
},
152
"validates": function (c) {
153
assert.ok(c.validate());
154
},
155
"to string": function (c) {
156
var str = c.toString();
157
assert.notEqual(str, 'a=b; Expires=Fri, 010 Sep 2010 010:010:010 GMT');
158
assert.equal(str, 'a=b; Expires=Fri, 10 Sep 2010 10:10:10 GMT');
159
}
160
}
161
})
162
.export(module);
163
164