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
'use strict';
32
var vows = require('vows');
33
var assert = require('assert');
34
var tough = require('../lib/cookie');
35
var Cookie = tough.Cookie;
36
37
function toKeyArray(cookies) {
38
return cookies.map(function (c) {
39
return c.key
40
});
41
}
42
43
vows
44
.describe('Cookie sorting')
45
.addBatch({
46
"Cookie Sorting": {
47
topic: function () {
48
var cookies = [];
49
cookies.push(Cookie.parse("a=0; Domain=example.com"));
50
cookies.push(Cookie.parse("b=1; Domain=www.example.com"));
51
cookies.push(Cookie.parse("c=2; Domain=example.com; Path=/pathA"));
52
cookies.push(Cookie.parse("d=3; Domain=www.example.com; Path=/pathA"));
53
cookies.push(Cookie.parse("e=4; Domain=example.com; Path=/pathA/pathB"));
54
cookies.push(Cookie.parse("f=5; Domain=www.example.com; Path=/pathA/pathB"));
55
56
// weak shuffle:
57
cookies = cookies.sort(function () {
58
return Math.random() - 0.5
59
});
60
61
cookies = cookies.sort(tough.cookieCompare);
62
return cookies;
63
},
64
"got": function (cookies) {
65
assert.lengthOf(cookies, 6);
66
assert.deepEqual(toKeyArray(cookies), ['e', 'f', 'c', 'd', 'a', 'b']);
67
}
68
}
69
})
70
.addBatch({
71
"Changing creation date affects sorting": {
72
topic: function () {
73
var cookies = [];
74
var now = Date.now();
75
cookies.push(Cookie.parse("a=0;"));
76
cookies.push(Cookie.parse("b=1;"));
77
cookies.push(Cookie.parse("c=2;"));
78
79
cookies.forEach(function (cookie, idx) {
80
cookie.creation = new Date(now - 100 * idx);
81
});
82
83
return cookies.sort(tough.cookieCompare);
84
},
85
"got": function (cookies) {
86
assert.deepEqual(toKeyArray(cookies), ['c', 'b', 'a']);
87
}
88
}
89
})
90
.export(module);
91
92