forked from honojs/hono
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.test.ts
More file actions
448 lines (392 loc) · 15.9 KB
/
Copy pathindex.test.ts
File metadata and controls
448 lines (392 loc) · 15.9 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
import { Hono } from '../../hono'
import { deleteCookie, getCookie, getSignedCookie, setCookie, setSignedCookie } from '.'
describe('Cookie Middleware', () => {
describe('Parse cookie', () => {
const apps: Record<string, Hono> = {}
apps['get by name'] = (() => {
const app = new Hono()
app.get('/cookie', (c) => {
const yummyCookie = getCookie(c, 'yummy_cookie')
const tastyCookie = getCookie(c, 'tasty_cookie')
const res = new Response('Good cookie')
if (yummyCookie && tastyCookie) {
res.headers.set('Yummy-Cookie', yummyCookie)
res.headers.set('Tasty-Cookie', tastyCookie)
}
return res
})
return app
})()
apps['get all as an object'] = (() => {
const app = new Hono()
app.get('/cookie', (c) => {
const { yummy_cookie: yummyCookie, tasty_cookie: tastyCookie } = getCookie(c)
const res = new Response('Good cookie')
res.headers.set('Yummy-Cookie', yummyCookie)
res.headers.set('Tasty-Cookie', tastyCookie)
return res
})
return app
})()
describe.each(Object.keys(apps))('%s', (name) => {
const app = apps[name]
it('Parse cookie with getCookie()', async () => {
const req = new Request('http://localhost/cookie')
const cookieString = 'yummy_cookie=choco; tasty_cookie = strawberry'
req.headers.set('Cookie', cookieString)
const res = await app.request(req)
expect(res.headers.get('Yummy-Cookie')).toBe('choco')
expect(res.headers.get('Tasty-Cookie')).toBe('strawberry')
})
})
const app = new Hono()
app.get('/cookie-signed-get-all', async (c) => {
const secret = 'secret lucky charm'
const { fortune_cookie: fortuneCookie, fruit_cookie: fruitCookie } = await getSignedCookie(
c,
secret
)
const res = new Response('Signed fortune cookie')
if (typeof fortuneCookie !== 'undefined' && typeof fruitCookie !== 'undefined') {
// just examples for tests sake
res.headers.set('Fortune-Cookie', fortuneCookie || 'INVALID')
res.headers.set('Fruit-Cookie', fruitCookie || 'INVALID')
}
return res
})
app.get('/cookie-signed-get-one', async (c) => {
const secret = 'secret lucky charm'
const fortuneCookie = await getSignedCookie(c, secret, 'fortune_cookie')
const res = new Response('Signed fortune cookie')
if (typeof fortuneCookie !== 'undefined') {
// just an example for tests sake
res.headers.set('Fortune-Cookie', fortuneCookie || 'INVALID')
}
return res
})
it('Get signed cookies', async () => {
const req = new Request('http://localhost/cookie-signed-get-all')
const cookieString =
'fortune_cookie=lots-of-money.UO6vMygDM6NCDU4LdvBnzdVb2Xcdj+h+ZTnmS8X7iH8%3D; fruit_cookie=mango.lRwgtW9ooM9%2Fd9ZZA%2FInNRG64CbQsfWGXQyFLPM9520%3D'
req.headers.set('Cookie', cookieString)
const res = await app.request(req)
expect(res.headers.get('Fortune-Cookie')).toBe('lots-of-money')
expect(res.headers.get('Fruit-Cookie')).toBe('mango')
})
it('Get signed cookies invalid signature', async () => {
const req = new Request('http://localhost/cookie-signed-get-all')
// fruit_cookie has invalid signature
const cookieString =
'fortune_cookie=lots-of-money.UO6vMygDM6NCDU4LdvBnzdVb2Xcdj+h+ZTnmS8X7iH8%3D; fruit_cookie=mango.LAa7RX43t2vCrLNcKmNG65H41OkyV02sraRPuY5RuVg%3D'
req.headers.set('Cookie', cookieString)
const res = await app.request(req)
expect(res.headers.get('Fortune-Cookie')).toBe('lots-of-money')
expect(res.headers.get('Fruit-Cookie')).toBe('INVALID')
})
it('Get signed cookie', async () => {
const req = new Request('http://localhost/cookie-signed-get-one')
const cookieString =
'fortune_cookie=lots-of-money.UO6vMygDM6NCDU4LdvBnzdVb2Xcdj+h+ZTnmS8X7iH8%3D; fruit_cookie=mango.lRwgtW9ooM9%2Fd9ZZA%2FInNRG64CbQsfWGXQyFLPM9520%3D'
req.headers.set('Cookie', cookieString)
const res = await app.request(req)
expect(res.headers.get('Fortune-Cookie')).toBe('lots-of-money')
})
it('Get signed cookie with invalid signature', async () => {
const req = new Request('http://localhost/cookie-signed-get-one')
// fortune_cookie has invalid signature
const cookieString =
'fortune_cookie=lots-of-money.LAa7RX43t2vCrLNcKmNG65H41OkyV02sraRPuY5RuVg=; fruit_cookie=mango.lRwgtW9ooM9%2Fd9ZZA%2FInNRG64CbQsfWGXQyFLPM9520%3D'
req.headers.set('Cookie', cookieString)
const res = await app.request(req)
expect(res.headers.get('Fortune-Cookie')).toBe('INVALID')
})
describe('get null if the value is undefined', () => {
const app = new Hono()
app.get('/cookie', (c) => {
const yummyCookie = getCookie(c, 'yummy_cookie')
const res = new Response('Good cookie')
if (yummyCookie) {
res.headers.set('Yummy-Cookie', yummyCookie)
}
return res
})
it('Should be null', async () => {
const req = new Request('http://localhost/cookie')
const cookieString = 'yummy_cookie='
req.headers.set('Cookie', cookieString)
const res = await app.request(req)
expect(res.headers.get('Yummy-Cookie')).toBe(null)
})
})
})
describe('Set cookie', () => {
const app = new Hono()
app.get('/set-cookie', (c) => {
setCookie(c, 'delicious_cookie', 'macha')
return c.text('Give cookie')
})
it('Set cookie with setCookie()', async () => {
const res = await app.request('http://localhost/set-cookie')
expect(res.status).toBe(200)
const header = res.headers.get('Set-Cookie')
expect(header).toBe('delicious_cookie=macha; Path=/')
})
app.get('/a/set-cookie-path', (c) => {
setCookie(c, 'delicious_cookie', 'macha', { path: '/a' })
return c.text('Give cookie')
})
it('Set cookie with setCookie() and path option', async () => {
const res = await app.request('http://localhost/a/set-cookie-path')
expect(res.status).toBe(200)
const header = res.headers.get('Set-Cookie')
expect(header).toBe('delicious_cookie=macha; Path=/a')
})
app.get('/set-signed-cookie', async (c) => {
const secret = 'secret chocolate chips'
await setSignedCookie(c, 'delicious_cookie', 'macha', secret)
return c.text('Give signed cookie')
})
it('Set signed cookie with setSignedCookie()', async () => {
const res = await app.request('http://localhost/set-signed-cookie')
expect(res.status).toBe(200)
const header = res.headers.get('Set-Cookie')
expect(header).toBe(
'delicious_cookie=macha.diubJPY8O7hI1pLa42QSfkPiyDWQ0I4DnlACH%2FN2HaA%3D; Path=/'
)
})
app.get('/a/set-signed-cookie-path', async (c) => {
const secret = 'secret chocolate chips'
await setSignedCookie(c, 'delicious_cookie', 'macha', secret, { path: '/a' })
return c.text('Give signed cookie')
})
it('Set signed cookie with setSignedCookie() and path option', async () => {
const res = await app.request('http://localhost/a/set-signed-cookie-path')
expect(res.status).toBe(200)
const header = res.headers.get('Set-Cookie')
expect(header).toBe(
'delicious_cookie=macha.diubJPY8O7hI1pLa42QSfkPiyDWQ0I4DnlACH%2FN2HaA%3D; Path=/a'
)
})
app.get('/get-secure-prefix-cookie', async (c) => {
const cookie = getCookie(c, 'delicious_cookie', 'secure')
if (cookie) {
return c.text(cookie)
} else {
return c.notFound()
}
})
app.get('/get-host-prefix-cookie', async (c) => {
const cookie = getCookie(c, 'delicious_cookie', 'host')
if (cookie) {
return c.text(cookie)
} else {
return c.notFound()
}
})
app.get('/set-secure-prefix-cookie', (c) => {
setCookie(c, 'delicious_cookie', 'macha', {
prefix: 'secure',
secure: false, // this will be ignore
})
return c.text('Set secure prefix cookie')
})
it('Set cookie with secure prefix', async () => {
const res = await app.request('http://localhost/set-secure-prefix-cookie')
expect(res.status).toBe(200)
const header = res.headers.get('Set-Cookie')
expect(header).toBe('__Secure-delicious_cookie=macha; Path=/; Secure')
})
it('Get cookie with secure prefix', async () => {
const setCookie = await app.request('http://localhost/set-secure-prefix-cookie')
const header = setCookie.headers.get('Set-Cookie')
if (!header) {
assert.fail('invalid header')
}
const res = await app.request('http://localhost/get-secure-prefix-cookie', {
headers: {
Cookie: header,
},
})
const response = await res.text()
expect(res.status).toBe(200)
expect(response).toBe('macha')
})
app.get('/set-host-prefix-cookie', (c) => {
setCookie(c, 'delicious_cookie', 'macha', {
prefix: 'host',
path: '/foo', // this will be ignored
domain: 'example.com', // this will be ignored
secure: false, // this will be ignored
})
return c.text('Set host prefix cookie')
})
it('Set cookie with host prefix', async () => {
const res = await app.request('http://localhost/set-host-prefix-cookie')
expect(res.status).toBe(200)
const header = res.headers.get('Set-Cookie')
expect(header).toBe('__Host-delicious_cookie=macha; Path=/; Secure')
})
it('Get cookie with host prefix', async () => {
const setCookie = await app.request('http://localhost/set-host-prefix-cookie')
const header = setCookie.headers.get('Set-Cookie')
if (!header) {
assert.fail('invalid header')
}
const res = await app.request('http://localhost/get-host-prefix-cookie', {
headers: {
Cookie: header,
},
})
const response = await res.text()
expect(res.status).toBe(200)
expect(response).toBe('macha')
})
app.get('/set-signed-secure-prefix-cookie', async (c) => {
await setSignedCookie(c, 'delicious_cookie', 'macha', 'secret choco chips', {
prefix: 'secure',
})
return c.text('Set secure prefix cookie')
})
it('Set signed cookie with secure prefix', async () => {
const res = await app.request('http://localhost/set-signed-secure-prefix-cookie')
expect(res.status).toBe(200)
const header = res.headers.get('Set-Cookie')
expect(header).toBe(
'__Secure-delicious_cookie=macha.i225faTyCrJUY8TvpTuJHI20HBWbQ89B4GV7lT4E%2FB0%3D; Path=/; Secure'
)
})
app.get('/set-signed-host-prefix-cookie', async (c) => {
await setSignedCookie(c, 'delicious_cookie', 'macha', 'secret choco chips', {
prefix: 'host',
domain: 'example.com', // this will be ignored
path: 'example.com', // thi will be ignored
secure: false, // this will be ignored
})
return c.text('Set host prefix cookie')
})
it('Set signed cookie with host prefix', async () => {
const res = await app.request('http://localhost/set-signed-host-prefix-cookie')
expect(res.status).toBe(200)
const header = res.headers.get('Set-Cookie')
expect(header).toBe(
'__Host-delicious_cookie=macha.i225faTyCrJUY8TvpTuJHI20HBWbQ89B4GV7lT4E%2FB0%3D; Path=/; Secure'
)
})
app.get('/set-cookie-complex', (c) => {
setCookie(c, 'great_cookie', 'banana', {
path: '/',
secure: true,
domain: 'example.com',
httpOnly: true,
maxAge: 1000,
expires: new Date(Date.UTC(2000, 11, 24, 10, 30, 59, 900)),
sameSite: 'Strict',
})
return c.text('Give cookie')
})
it('Complex pattern', async () => {
const res = await app.request('http://localhost/set-cookie-complex')
expect(res.status).toBe(200)
const header = res.headers.get('Set-Cookie')
expect(header).toBe(
'great_cookie=banana; Max-Age=1000; Domain=example.com; Path=/; Expires=Sun, 24 Dec 2000 10:30:59 GMT; HttpOnly; Secure; SameSite=Strict'
)
})
app.get('/set-signed-cookie-complex', async (c) => {
const secret = 'secret chocolate chips'
await setSignedCookie(c, 'great_cookie', 'banana', secret, {
path: '/',
secure: true,
domain: 'example.com',
httpOnly: true,
maxAge: 1000,
expires: new Date(Date.UTC(2000, 11, 24, 10, 30, 59, 900)),
sameSite: 'Strict',
})
return c.text('Give signed cookie')
})
it('Complex pattern (signed)', async () => {
const res = await app.request('http://localhost/set-signed-cookie-complex')
expect(res.status).toBe(200)
const header = res.headers.get('Set-Cookie')
expect(header).toBe(
'great_cookie=banana.hSo6gB7YT2db0WBiEAakEmh7dtwEL0DSp76G23WvHuQ%3D; Max-Age=1000; Domain=example.com; Path=/; Expires=Sun, 24 Dec 2000 10:30:59 GMT; HttpOnly; Secure; SameSite=Strict'
)
})
app.get('/set-cookie-multiple', (c) => {
setCookie(c, 'delicious_cookie', 'macha')
setCookie(c, 'delicious_cookie', 'choco')
return c.text('Give cookie')
})
it('Multiple values', async () => {
const res = await app.request('http://localhost/set-cookie-multiple')
expect(res.status).toBe(200)
const header = res.headers.get('Set-Cookie')
expect(header).toBe('delicious_cookie=macha; Path=/, delicious_cookie=choco; Path=/')
})
})
describe('Delete cookie', () => {
const app = new Hono()
app.get('/delete-cookie', (c) => {
deleteCookie(c, 'delicious_cookie')
return c.text('Give cookie')
})
it('Delete cookie', async () => {
const res2 = await app.request('http://localhost/delete-cookie')
expect(res2.status).toBe(200)
const header2 = res2.headers.get('Set-Cookie')
expect(header2).toBe('delicious_cookie=; Max-Age=0; Path=/')
})
app.get('/delete-cookie-multiple', (c) => {
deleteCookie(c, 'delicious_cookie')
deleteCookie(c, 'delicious_cookie2')
return c.text('Give cookie')
})
it('Delete multiple cookies', async () => {
const res2 = await app.request('http://localhost/delete-cookie-multiple')
expect(res2.status).toBe(200)
const header2 = res2.headers.get('Set-Cookie')
expect(header2).toBe(
'delicious_cookie=; Max-Age=0; Path=/, delicious_cookie2=; Max-Age=0; Path=/'
)
})
app.get('/delete-cookie-with-options', (c) => {
deleteCookie(c, 'delicious_cookie', {
path: '/',
secure: true,
domain: 'example.com',
})
return c.text('Give cookie')
})
it('Delete cookie with options', async () => {
const res2 = await app.request('http://localhost/delete-cookie-with-options')
expect(res2.status).toBe(200)
const header2 = res2.headers.get('Set-Cookie')
expect(header2).toBe('delicious_cookie=; Max-Age=0; Domain=example.com; Path=/; Secure')
})
app.get('/delete-cookie-with-deleted-value', (c) => {
const deleted = deleteCookie(c, 'delicious_cookie')
return c.text(deleted || '')
})
it('Get deleted value', async () => {
const cookieString = 'delicious_cookie=choco'
const req = new Request('http://localhost/delete-cookie-with-deleted-value')
req.headers.set('Cookie', cookieString)
const res = await app.request(req)
expect(res.status).toBe(200)
expect(await res.text()).toBe('choco')
})
app.get('/delete-cookie-with-prefix', (c) => {
const deleted = deleteCookie(c, 'delicious_cookie', { prefix: 'secure' })
return c.text(deleted || '')
})
it('Get deleted value with prefix', async () => {
const cookieString = '__Secure-delicious_cookie=choco'
const req = new Request('http://localhost/delete-cookie-with-prefix')
req.headers.set('Cookie', cookieString)
const res = await app.request(req)
expect(res.status).toBe(200)
expect(await res.text()).toBe('choco')
})
})
})