-
-
Notifications
You must be signed in to change notification settings - Fork 158
Expand file tree
/
Copy pathrestify.js
More file actions
189 lines (146 loc) · 4.6 KB
/
restify.js
File metadata and controls
189 lines (146 loc) · 4.6 KB
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
var assert = require('assert')
, Cookies = require('../')
, keys = new Cookies.Keygrip(['a', 'b'])
, request = require('supertest')
var restify = tryRequire('restify')
var describeRestify = restify ? describe : describe.skip
describeRestify('Restify', function () {
it('should set a cookie on the response', function (done) {
var server = restify.createServer()
server.get('/', function (req, res) {
var cookies = new Cookies(req, res)
cookies.set('foo', 'bar')
res.send(200)
})
request(server)
.get('/')
.expect(shouldSetCookies([
{ name: 'foo', value: 'bar', path: '/', httponly: true }
]))
.expect(200, done)
})
it('should get a cookie from the request', function (done) {
var server = restify.createServer()
server.get('/', function (req, res) {
var cookies = new Cookies(req, res)
res.send({ foo: String(cookies.get('foo')) })
})
request(server)
.get('/')
.set('cookie', 'foo=bar')
.expect(200, { foo: 'bar' }, done)
})
describe('with multiple cookies', function () {
it('should set all cookies on the response', function (done) {
var server = restify.createServer()
server.get('/', function (req, res) {
var cookies = new Cookies(req, res)
cookies.set('foo', 'bar')
cookies.set('fizz', 'buzz')
res.send(200)
})
request(server)
.get('/')
.expect(shouldSetCookies([
{ name: 'foo', value: 'bar', path: '/', httponly: true },
{ name: 'fizz', value: 'buzz', path: '/', httponly: true }
]))
.expect(200, done)
})
it('should get each cookie from the request', function (done) {
var server = restify.createServer()
server.get('/', function (req, res) {
var cookies = new Cookies(req, res)
res.send({
fizz: String(cookies.get('fizz')),
foo: String(cookies.get('foo'))
})
})
request(server)
.get('/')
.set('cookie', 'foo=bar; fizz=buzz')
.expect(200, { foo: 'bar', fizz: 'buzz' }, done)
})
})
describe('when "overwrite: false"', function () {
it('should set second cookie with same name', function (done) {
var server = restify.createServer()
server.get('/', function (req, res) {
var cookies = new Cookies(req, res)
cookies.set('foo', 'bar')
cookies.set('foo', 'fizz', { overwrite: false })
res.send(200)
})
request(server)
.get('/')
.expect(shouldSetCookies([
{ name: 'foo', value: 'bar', path: '/', httponly: true },
{ name: 'foo', value: 'fizz', path: '/', httponly: true }
]))
.expect(200, done)
})
})
describe('when "overwrite: true"', function () {
it('should replace previously set value', function (done) {
var server = restify.createServer()
server.get('/', function (req, res) {
var cookies = new Cookies(req, res)
cookies.set('foo', 'bar')
cookies.set('foo', 'fizz', { overwrite: true })
res.send(200)
})
request(server)
.get('/')
.expect(shouldSetCookies([
{ name: 'foo', value: 'fizz', path: '/', httponly: true }
]))
.expect(200, done)
})
it('should set signature correctly', function (done) {
var server = restify.createServer()
server.get('/', function (req, res) {
var cookies = new Cookies(req, res, keys)
cookies.set('foo', 'bar')
cookies.set('foo', 'fizz', { overwrite: true })
res.send(200)
})
request(server)
.get('/')
.expect(shouldSetCookies([
{ name: 'foo', value: 'fizz', path: '/', httponly: true },
{ name: 'foo.sig', value: 'hVIYdxZSelh3gIK5wQxzrqoIndU', path: '/', httponly: true }
]))
.expect(200, done)
})
})
})
function getCookies (res) {
var setCookies = res.headers['set-cookie'] || []
return setCookies.map(parseSetCookie)
}
function parseSetCookie (header) {
var match
var pairs = []
var pattern = /\s*([^=;]+)(?:=([^;]*);?|;|$)/g
while ((match = pattern.exec(header))) {
pairs.push({ name: match[1], value: match[2] })
}
var cookie = pairs.shift()
for (var i = 0; i < pairs.length; i++) {
match = pairs[i]
cookie[match.name.toLowerCase()] = (match.value || true)
}
return cookie
}
function shouldSetCookies (expected) {
return function (res) {
assert.deepEqual(getCookies(res), expected)
}
}
function tryRequire (name) {
try {
return require(name)
} catch (e) {
return undefined
}
}