forked from expressjs/session
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcookie.js
122 lines (96 loc) · 3.84 KB
/
cookie.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
var assert = require('assert')
var Cookie = require('../session/cookie')
describe('new Cookie()', function () {
it('should create a new cookie object', function () {
assert.strictEqual(typeof new Cookie(), 'object')
})
it('should default expires to null', function () {
var cookie = new Cookie()
assert.strictEqual(cookie.expires, null)
})
it('should default maxAge to null', function () {
var cookie = new Cookie()
assert.strictEqual(cookie.maxAge, null)
})
it('should default httpOnly to true', function () {
var cookie = new Cookie()
assert.strictEqual(cookie.httpOnly, true)
})
it('should default path to "/"', function () {
var cookie = new Cookie()
assert.strictEqual(cookie.path, '/')
})
it('should default maxAge to null', function () {
var cookie = new Cookie()
assert.strictEqual(cookie.maxAge, null)
})
describe('with options', function () {
it('should create a new cookie object', function () {
assert.strictEqual(typeof new Cookie({}), 'object')
})
it('should reject non-objects', function () {
assert.throws(function () { new Cookie(42) }, /argument options/)
assert.throws(function () { new Cookie('foo') }, /argument options/)
assert.throws(function () { new Cookie(true) }, /argument options/)
assert.throws(function () { new Cookie(function () {}) }, /argument options/)
})
it('should ignore "data" option', function () {
var cookie = new Cookie({ data: { foo: 'bar' }, path: '/foo' })
assert.strictEqual(typeof cookie, 'object')
assert.strictEqual(typeof cookie.data, 'object')
assert.strictEqual(cookie.data.path, '/foo')
assert.notStrictEqual(cookie.data.foo, 'bar')
})
describe('expires', function () {
it('should set expires', function () {
var expires = new Date(Date.now() + 60000)
var cookie = new Cookie({ expires: expires })
assert.strictEqual(cookie.expires, expires)
})
it('should set maxAge', function () {
var now = Date.now()
var expires = new Date(now + 60000)
var cookie = new Cookie({ expires: expires })
assert.ok(expires.getTime() - now - 1000 <= cookie.maxAge)
assert.ok(expires.getTime() - now + 1000 >= cookie.maxAge)
})
})
describe('httpOnly', function () {
it('should set httpOnly', function () {
var cookie = new Cookie({ httpOnly: false })
assert.strictEqual(cookie.httpOnly, false)
})
})
describe('maxAge', function () {
it('should set expires', function () {
var maxAge = 60000
var cookie = new Cookie({ maxAge: maxAge })
assert.ok(cookie.expires.getTime() - Date.now() - 1000 <= maxAge)
assert.ok(cookie.expires.getTime() - Date.now() + 1000 >= maxAge)
})
it('should set maxAge', function () {
var maxAge = 60000
var cookie = new Cookie({ maxAge: maxAge })
assert.strictEqual(cookie.maxAge, maxAge)
})
it('should accept Date object', function () {
var maxAge = new Date(Date.now() + 60000)
var cookie = new Cookie({ maxAge: maxAge })
assert.strictEqual(cookie.expires.getTime(), maxAge.getTime())
assert.ok(maxAge.getTime() - Date.now() - 1000 <= cookie.maxAge)
assert.ok(maxAge.getTime() - Date.now() + 1000 >= cookie.maxAge)
})
it('should reject invalid types', function() {
assert.throws(function() { new Cookie({ maxAge: '42' }) }, /maxAge/)
assert.throws(function() { new Cookie({ maxAge: true }) }, /maxAge/)
assert.throws(function() { new Cookie({ maxAge: function () {} }) }, /maxAge/)
})
})
describe('path', function () {
it('should set path', function () {
var cookie = new Cookie({ path: '/foo' })
assert.strictEqual(cookie.path, '/foo')
})
})
})
})