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 async = require('async');
36
var tough = require('../lib/cookie');
37
var Cookie = tough.Cookie;
38
var CookieJar = tough.CookieJar;
39
40
var atNow = Date.now();
41
42
function at(offset) {
43
return {now: new Date(atNow + offset)};
44
}
45
46
vows
47
.describe('Regression tests')
48
.addBatch({
49
"Issue 1": {
50
topic: function () {
51
var cj = new CookieJar();
52
cj.setCookie('hello=world; path=/some/path/', 'http://domain/some/path/file', function (err, cookie) {
53
this.callback(err, {cj: cj, cookie: cookie});
54
}.bind(this));
55
},
56
"stored a cookie": function (t) {
57
assert.ok(t.cookie);
58
},
59
"getting it back": {
60
topic: function (t) {
61
t.cj.getCookies('http://domain/some/path/file', function (err, cookies) {
62
this.callback(err, {cj: t.cj, cookies: cookies || []});
63
}.bind(this));
64
},
65
"got one cookie": function (t) {
66
assert.lengthOf(t.cookies, 1);
67
},
68
"it's the right one": function (t) {
69
var c = t.cookies[0];
70
assert.equal(c.key, 'hello');
71
assert.equal(c.value, 'world');
72
}
73
}
74
}
75
})
76
.addBatch({
77
"trailing semi-colon set into cj": {
78
topic: function () {
79
var cb = this.callback;
80
var cj = new CookieJar();
81
var ex = 'http://www.example.com';
82
var tasks = [];
83
tasks.push(function (next) {
84
cj.setCookie('broken_path=testme; path=/;', ex, at(-1), next);
85
});
86
tasks.push(function (next) {
87
cj.setCookie('b=2; Path=/;;;;', ex, at(-1), next);
88
});
89
async.parallel(tasks, function (err, cookies) {
90
cb(null, {
91
cj: cj,
92
cookies: cookies
93
});
94
});
95
},
96
"check number of cookies": function (t) {
97
assert.lengthOf(t.cookies, 2, "didn't set");
98
},
99
"check *broken_path* was set properly": function (t) {
100
assert.equal(t.cookies[0].key, "broken_path");
101
assert.equal(t.cookies[0].value, "testme");
102
assert.equal(t.cookies[0].path, "/");
103
},
104
"check *b* was set properly": function (t) {
105
assert.equal(t.cookies[1].key, "b");
106
assert.equal(t.cookies[1].value, "2");
107
assert.equal(t.cookies[1].path, "/");
108
},
109
"retrieve the cookie": {
110
topic: function (t) {
111
var cb = this.callback;
112
t.cj.getCookies('http://www.example.com', {}, function (err, cookies) {
113
t.cookies = cookies;
114
cb(err, t);
115
});
116
},
117
"get the cookie": function (t) {
118
assert.lengthOf(t.cookies, 2);
119
assert.equal(t.cookies[0].key, 'broken_path');
120
assert.equal(t.cookies[0].value, 'testme');
121
assert.equal(t.cookies[1].key, "b");
122
assert.equal(t.cookies[1].value, "2");
123
assert.equal(t.cookies[1].path, "/");
124
}
125
}
126
}
127
})
128
.addBatch({
129
"tough-cookie throws exception on malformed URI (GH-32)": {
130
topic: function () {
131
var url = "http://www.example.com/?test=100%";
132
var cj = new CookieJar();
133
134
cj.setCookieSync("Test=Test", url);
135
136
return cj.getCookieStringSync(url);
137
},
138
"cookies are set": function (cookieStr) {
139
assert.strictEqual(cookieStr, "Test=Test");
140
}
141
}
142
})
143
.export(module);
144
145