forked from expressjs/express
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathres.cookie.js
208 lines (164 loc) · 5.15 KB
/
res.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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
'use strict'
var express = require('../')
, request = require('supertest')
, cookieParser = require('cookie-parser')
var merge = require('utils-merge');
describe('res', function(){
describe('.cookie(name, object)', function(){
it('should generate a JSON cookie', function(done){
var app = express();
app.use(function(req, res){
res.cookie('user', { name: 'tobi' }).end();
});
request(app)
.get('/')
.expect('Set-Cookie', 'user=j%3A%7B%22name%22%3A%22tobi%22%7D; Path=/')
.expect(200, done)
})
})
describe('.cookie(name, string)', function(){
it('should set a cookie', function(done){
var app = express();
app.use(function(req, res){
res.cookie('name', 'tobi').end();
});
request(app)
.get('/')
.expect('Set-Cookie', 'name=tobi; Path=/')
.expect(200, done)
})
it('should allow multiple calls', function(done){
var app = express();
app.use(function(req, res){
res.cookie('name', 'tobi');
res.cookie('age', 1);
res.cookie('gender', '?');
res.end();
});
request(app)
.get('/')
.expect('Set-Cookie', 'name=tobi; Path=/,age=1; Path=/,gender=%3F; Path=/')
.expect(200, done)
})
})
describe('.cookie(name, string, options)', function(){
it('should set params', function(done){
var app = express();
app.use(function(req, res){
res.cookie('name', 'tobi', { httpOnly: true, secure: true });
res.end();
});
request(app)
.get('/')
.expect('Set-Cookie', 'name=tobi; Path=/; HttpOnly; Secure')
.expect(200, done)
})
describe('maxAge', function(){
it('should set relative expires', function(done){
var app = express();
app.use(function(req, res){
res.cookie('name', 'tobi', { maxAge: 1000 });
res.end();
});
request(app)
.get('/')
.expect('Set-Cookie', /name=tobi; Max-Age=1; Path=\/; Expires=/)
.expect(200, done)
})
it('should set max-age', function(done){
var app = express();
app.use(function(req, res){
res.cookie('name', 'tobi', { maxAge: 1000 });
res.end();
});
request(app)
.get('/')
.expect('Set-Cookie', /Max-Age=1/, done)
})
it('should not mutate the options object', function(done){
var app = express();
var options = { maxAge: 1000 };
var optionsCopy = merge({}, options);
app.use(function(req, res){
res.cookie('name', 'tobi', options)
res.json(options)
});
request(app)
.get('/')
.expect(200, optionsCopy, done)
})
it('should not throw on null', function (done) {
var app = express()
app.use(function (req, res) {
res.cookie('name', 'tobi', { maxAge: null })
res.end()
})
request(app)
.get('/')
.expect(200)
.expect('Set-Cookie', 'name=tobi; Path=/')
.end(done)
})
it('should not throw on undefined', function (done) {
var app = express()
app.use(function (req, res) {
res.cookie('name', 'tobi', { maxAge: undefined })
res.end()
})
request(app)
.get('/')
.expect(200)
.expect('Set-Cookie', 'name=tobi; Path=/')
.end(done)
})
it('should throw an error with invalid maxAge', function (done) {
var app = express()
app.use(function (req, res) {
res.cookie('name', 'tobi', { maxAge: 'foobar' })
res.end()
})
request(app)
.get('/')
.expect(500, /option maxAge is invalid/, done)
})
})
describe('signed', function(){
it('should generate a signed JSON cookie', function(done){
var app = express();
app.use(cookieParser('foo bar baz'));
app.use(function(req, res){
res.cookie('user', { name: 'tobi' }, { signed: true }).end();
});
request(app)
.get('/')
.expect('Set-Cookie', 'user=s%3Aj%3A%7B%22name%22%3A%22tobi%22%7D.K20xcwmDS%2BPb1rsD95o5Jm5SqWs1KteqdnynnB7jkTE; Path=/')
.expect(200, done)
})
})
describe('signed without secret', function(){
it('should throw an error', function(done){
var app = express();
app.use(cookieParser());
app.use(function(req, res){
res.cookie('name', 'tobi', { signed: true }).end();
});
request(app)
.get('/')
.expect(500, /secret\S+ required for signed cookies/, done);
})
})
describe('.signedCookie(name, string)', function(){
it('should set a signed cookie', function(done){
var app = express();
app.use(cookieParser('foo bar baz'));
app.use(function(req, res){
res.cookie('name', 'tobi', { signed: true }).end();
});
request(app)
.get('/')
.expect('Set-Cookie', 'name=s%3Atobi.xJjV2iZ6EI7C8E5kzwbfA9PVLl1ZR07UTnuTgQQ4EnQ; Path=/')
.expect(200, done)
})
})
})
})