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
function matchVows(func, table) {
39
var theVows = {};
40
table.forEach(function (item) {
41
var str = item[0];
42
var dom = item[1];
43
var expect = item[2];
44
var label = str + (expect ? " matches " : " doesn't match ") + dom;
45
theVows[label] = function () {
46
assert.equal(func(str, dom), expect);
47
};
48
});
49
return theVows;
50
}
51
52
function defaultPathVows(table) {
53
var theVows = {};
54
table.forEach(function (item) {
55
var str = item[0];
56
var expect = item[1];
57
var label = str + " gives " + expect;
58
theVows[label] = function () {
59
assert.equal(tough.defaultPath(str), expect);
60
};
61
});
62
return theVows;
63
}
64
65
vows
66
.describe('Domain and Path')
67
.addBatch({
68
"domain normalization": {
69
"simple": function () {
70
var c = new Cookie();
71
c.domain = "EXAMPLE.com";
72
assert.equal(c.canonicalizedDomain(), "example.com");
73
},
74
"extra dots": function () {
75
var c = new Cookie();
76
c.domain = ".EXAMPLE.com";
77
assert.equal(c.cdomain(), "example.com");
78
},
79
"weird trailing dot": function () {
80
var c = new Cookie();
81
c.domain = "EXAMPLE.ca.";
82
assert.equal(c.canonicalizedDomain(), "example.ca.");
83
},
84
"weird internal dots": function () {
85
var c = new Cookie();
86
c.domain = "EXAMPLE...ca.";
87
assert.equal(c.canonicalizedDomain(), "example...ca.");
88
},
89
"IDN": function () {
90
var c = new Cookie();
91
c.domain = "δοκιμή.δοκιμή"; // "test.test" in greek
92
assert.equal(c.canonicalizedDomain(), "xn--jxalpdlp.xn--jxalpdlp");
93
}
94
}
95
})
96
.addBatch({
97
"Domain Match": matchVows(tough.domainMatch, [
98
// str, dom, expect
99
["example.com", "example.com", true],
100
["eXaMpLe.cOm", "ExAmPlE.CoM", true],
101
["no.ca", "yes.ca", false],
102
["wwwexample.com", "example.com", false],
103
["www.example.com", "example.com", true],
104
["example.com", "www.example.com", false],
105
["www.subdom.example.com", "example.com", true],
106
["www.subdom.example.com", "subdom.example.com", true],
107
["example.com", "example.com.", false], // RFC6265 S4.1.2.3
108
["192.168.0.1", "168.0.1", false], // S5.1.3 "The string is a host name"
109
[null, "example.com", null],
110
["example.com", null, null],
111
[null, null, null],
112
[undefined, undefined, null],
113
])
114
})
115
116
.addBatch({
117
"default-path": defaultPathVows([
118
[null, "/"],
119
["/", "/"],
120
["/file", "/"],
121
["/dir/file", "/dir"],
122
["noslash", "/"],
123
])
124
})
125
.addBatch({
126
"Path-Match": matchVows(tough.pathMatch, [
127
// request, cookie, match
128
["/", "/", true],
129
["/dir", "/", true],
130
["/", "/dir", false],
131
["/dir/", "/dir/", true],
132
["/dir/file", "/dir/", true],
133
["/dir/file", "/dir", true],
134
["/directory", "/dir", false],
135
])
136
})
137
.addBatch({
138
"permuteDomain": {
139
"base case": {
140
topic: tough.permuteDomain.bind(null, 'example.com'),
141
"got the domain": function (list) {
142
assert.deepEqual(list, ['example.com']);
143
}
144
},
145
"two levels": {
146
topic: tough.permuteDomain.bind(null, 'foo.bar.example.com'),
147
"got three things": function (list) {
148
assert.deepEqual(list, ['example.com', 'bar.example.com', 'foo.bar.example.com']);
149
}
150
},
151
"local domain": {
152
topic: tough.permuteDomain.bind(null, 'foo.bar.example.localduhmain'),
153
"got three things": function (list) {
154
assert.deepEqual(list, ['example.localduhmain', 'bar.example.localduhmain', 'foo.bar.example.localduhmain']);
155
}
156
}
157
},
158
"permutePath": {
159
"base case": {
160
topic: tough.permutePath.bind(null, '/'),
161
"just slash": function (list) {
162
assert.deepEqual(list, ['/']);
163
}
164
},
165
"single case": {
166
topic: tough.permutePath.bind(null, '/foo'),
167
"two things": function (list) {
168
assert.deepEqual(list, ['/foo', '/']);
169
},
170
"path matching": function (list) {
171
list.forEach(function (e) {
172
assert.ok(tough.pathMatch('/foo', e));
173
});
174
}
175
},
176
"double case": {
177
topic: tough.permutePath.bind(null, '/foo/bar'),
178
"four things": function (list) {
179
assert.deepEqual(list, ['/foo/bar', '/foo', '/']);
180
},
181
"path matching": function (list) {
182
list.forEach(function (e) {
183
assert.ok(tough.pathMatch('/foo/bar', e));
184
});
185
}
186
},
187
"trailing slash": {
188
topic: tough.permutePath.bind(null, '/foo/bar/'),
189
"three things": function (list) {
190
assert.deepEqual(list, ['/foo/bar', '/foo', '/']);
191
},
192
"path matching": function (list) {
193
list.forEach(function (e) {
194
assert.ok(tough.pathMatch('/foo/bar/', e));
195
});
196
}
197
}
198
}
199
})
200
.export(module);
201
202
203