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('Lifetime')
40
.addBatch({
41
"TTL with max-age": function () {
42
var c = new Cookie();
43
c.maxAge = 123;
44
assert.equal(c.TTL(), 123000);
45
assert.equal(c.expiryTime(new Date(9000000)), 9123000);
46
},
47
"TTL with zero max-age": function () {
48
var c = new Cookie();
49
c.key = 'a';
50
c.value = 'b';
51
c.maxAge = 0; // should be treated as "earliest representable"
52
assert.equal(c.TTL(), 0);
53
assert.equal(c.expiryTime(new Date(9000000)), -Infinity);
54
assert.ok(!c.validate()); // not valid, really: non-zero-digit *DIGIT
55
},
56
"TTL with negative max-age": function () {
57
var c = new Cookie();
58
c.key = 'a';
59
c.value = 'b';
60
c.maxAge = -1; // should be treated as "earliest representable"
61
assert.equal(c.TTL(), 0);
62
assert.equal(c.expiryTime(new Date(9000000)), -Infinity);
63
assert.ok(!c.validate()); // not valid, really: non-zero-digit *DIGIT
64
},
65
"TTL with max-age and expires": function () {
66
var c = new Cookie();
67
c.maxAge = 123;
68
c.expires = new Date(Date.now() + 9000);
69
assert.equal(c.TTL(), 123000);
70
assert.ok(c.isPersistent());
71
},
72
"TTL with expires": function () {
73
var c = new Cookie();
74
var now = Date.now();
75
c.expires = new Date(now + 9000);
76
assert.equal(c.TTL(now), 9000);
77
assert.equal(c.expiryTime(), c.expires.getTime());
78
},
79
"TTL with old expires": function () {
80
var c = new Cookie();
81
c.setExpires('17 Oct 2010 00:00:00 GMT');
82
assert.ok(c.TTL() < 0);
83
assert.ok(c.isPersistent());
84
},
85
"default TTL": {
86
topic: function () {
87
return new Cookie();
88
},
89
"is Infinite-future": function (c) {
90
assert.equal(c.TTL(), Infinity)
91
},
92
"is a 'session' cookie": function (c) {
93
assert.ok(!c.isPersistent())
94
}
95
}
96
})
97
.export(module);
98
99