Skip to content
Myr

index.test.ts

This file

Path

src/helper/cookie/index.test.ts

510 lines · 17.8 KiB · TypeScript

Last changed by

chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2

History of this file

Take it

The walk stopped after 200 commits: lines older than f0b094d point at it.

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 1–9why, one step back
1import {
2 deleteCookie,3 getCookie,4 getSignedCookie,5 setCookie,6 setSignedCookie,7 generateCookie,8 generateSignedCookie,9} from '.'

chore: let oxfmt sort imports instead of eslint (#5442)Yusuke Wadabefore adoption859e2c2

line 10why, one step back
10import { Hono } from '../../hono'

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 11–69why, one step back
11
12describe('Cookie Middleware', () => {13 describe('Parse cookie', () => {14 const apps: Record<string, Hono> = {}15 apps['get by name'] = (() => {16 const app = new Hono()1718 app.get('/cookie', (c) => {19 const yummyCookie = getCookie(c, 'yummy_cookie')20 const tastyCookie = getCookie(c, 'tasty_cookie')21 const res = new Response('Good cookie')22 if (yummyCookie && tastyCookie) {23 res.headers.set('Yummy-Cookie', yummyCookie)24 res.headers.set('Tasty-Cookie', tastyCookie)25 }26 return res27 })2829 return app30 })()3132 apps['get all as an object'] = (() => {33 const app = new Hono()3435 app.get('/cookie', (c) => {36 const { yummy_cookie: yummyCookie, tasty_cookie: tastyCookie } = getCookie(c)37 const res = new Response('Good cookie')38 res.headers.set('Yummy-Cookie', yummyCookie)39 res.headers.set('Tasty-Cookie', tastyCookie)40 return res41 })4243 return app44 })()4546 describe.each(Object.keys(apps))('%s', (name) => {47 const app = apps[name]48 it('Parse cookie with getCookie()', async () => {49 const req = new Request('http://localhost/cookie')50 const cookieString = 'yummy_cookie=choco; tasty_cookie = strawberry'51 req.headers.set('Cookie', cookieString)52 const res = await app.request(req)5354 expect(res.headers.get('Yummy-Cookie')).toBe('choco')55 expect(res.headers.get('Tasty-Cookie')).toBe('strawberry')56 })57 })5859 const app = new Hono()6061 app.get('/cookie-signed-get-all', async (c) => {62 const secret = 'secret lucky charm'63 const { fortune_cookie: fortuneCookie, fruit_cookie: fruitCookie } = await getSignedCookie(64 c,65 secret66 )67 const res = new Response('Signed fortune cookie')68 if (typeof fortuneCookie !== 'undefined' && typeof fruitCookie !== 'undefined') {69 // just examples for tests sake

fix(cookie): allow parsing signed cookies with empty string values (#5246)Nanasibefore adoption241ae4c

lines 70–71why, one step back
70 res.headers.set('Fortune-Cookie', fortuneCookie === false ? 'INVALID' : fortuneCookie)
71 res.headers.set('Fruit-Cookie', fruitCookie === false ? 'INVALID' : fruitCookie)

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 72–81why, one step back
72 }
73 return res74 })7576 app.get('/cookie-signed-get-one', async (c) => {77 const secret = 'secret lucky charm'78 const fortuneCookie = await getSignedCookie(c, secret, 'fortune_cookie')79 const res = new Response('Signed fortune cookie')80 if (typeof fortuneCookie !== 'undefined') {81 // just an example for tests sake

fix(cookie): allow parsing signed cookies with empty string values (#5246)Nanasibefore adoption241ae4c

line 82why, one step back
82 res.headers.set('Fortune-Cookie', fortuneCookie === false ? 'INVALID' : fortuneCookie)

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 83–124why, one step back
83 }
84 return res85 })8687 it('Get signed cookies', async () => {88 const req = new Request('http://localhost/cookie-signed-get-all')89 const cookieString =90 'fortune_cookie=lots-of-money.UO6vMygDM6NCDU4LdvBnzdVb2Xcdj+h+ZTnmS8X7iH8%3D; fruit_cookie=mango.lRwgtW9ooM9%2Fd9ZZA%2FInNRG64CbQsfWGXQyFLPM9520%3D'91 req.headers.set('Cookie', cookieString)92 const res = await app.request(req)93 expect(res.headers.get('Fortune-Cookie')).toBe('lots-of-money')94 expect(res.headers.get('Fruit-Cookie')).toBe('mango')95 })9697 it('Get signed cookies invalid signature', async () => {98 const req = new Request('http://localhost/cookie-signed-get-all')99 // fruit_cookie has invalid signature100 const cookieString =101 'fortune_cookie=lots-of-money.UO6vMygDM6NCDU4LdvBnzdVb2Xcdj+h+ZTnmS8X7iH8%3D; fruit_cookie=mango.LAa7RX43t2vCrLNcKmNG65H41OkyV02sraRPuY5RuVg%3D'102 req.headers.set('Cookie', cookieString)103 const res = await app.request(req)104 expect(res.headers.get('Fortune-Cookie')).toBe('lots-of-money')105 expect(res.headers.get('Fruit-Cookie')).toBe('INVALID')106 })107108 it('Get signed cookie', async () => {109 const req = new Request('http://localhost/cookie-signed-get-one')110 const cookieString =111 'fortune_cookie=lots-of-money.UO6vMygDM6NCDU4LdvBnzdVb2Xcdj+h+ZTnmS8X7iH8%3D; fruit_cookie=mango.lRwgtW9ooM9%2Fd9ZZA%2FInNRG64CbQsfWGXQyFLPM9520%3D'112 req.headers.set('Cookie', cookieString)113 const res = await app.request(req)114 expect(res.headers.get('Fortune-Cookie')).toBe('lots-of-money')115 })116117 it('Get signed cookie with invalid signature', async () => {118 const req = new Request('http://localhost/cookie-signed-get-one')119 // fortune_cookie has invalid signature120 const cookieString =121 'fortune_cookie=lots-of-money.LAa7RX43t2vCrLNcKmNG65H41OkyV02sraRPuY5RuVg=; fruit_cookie=mango.lRwgtW9ooM9%2Fd9ZZA%2FInNRG64CbQsfWGXQyFLPM9520%3D'122 req.headers.set('Cookie', cookieString)123 const res = await app.request(req)124 expect(res.headers.get('Fortune-Cookie')).toBe('INVALID')

fix(cookie): allow parsing signed cookies with empty string values (#5246)Nanasibefore adoption241ae4c

lines 125–133why, one step back
125 })
126127 it('Get signed cookie with empty string value', async () => {128 const secret = 'secret lucky charm'129 const emptySignedCookie = await generateSignedCookie('fortune_cookie', '', secret)130 const req = new Request('http://localhost/cookie-signed-get-one')131 req.headers.set('Cookie', emptySignedCookie)132 const res = await app.request(req)133 expect(res.headers.get('Fortune-Cookie')).toBe('')

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 134–316why, one step back
134 })
135136 describe('get null if the value is undefined', () => {137 const app = new Hono()138139 app.get('/cookie', (c) => {140 const yummyCookie = getCookie(c, 'yummy_cookie')141 const res = new Response('Good cookie')142 if (yummyCookie) {143 res.headers.set('Yummy-Cookie', yummyCookie)144 }145 return res146 })147148 it('Should be null', async () => {149 const req = new Request('http://localhost/cookie')150 const cookieString = 'yummy_cookie='151 req.headers.set('Cookie', cookieString)152 const res = await app.request(req)153 expect(res.headers.get('Yummy-Cookie')).toBe(null)154 })155 })156 })157158 describe('Set cookie', () => {159 const app = new Hono()160161 app.get('/set-cookie', (c) => {162 setCookie(c, 'delicious_cookie', 'macha')163 return c.text('Give cookie')164 })165166 it('Set cookie with setCookie()', async () => {167 const res = await app.request('http://localhost/set-cookie')168 expect(res.status).toBe(200)169 const header = res.headers.get('Set-Cookie')170 expect(header).toBe('delicious_cookie=macha; Path=/')171 })172173 app.get('/a/set-cookie-path', (c) => {174 setCookie(c, 'delicious_cookie', 'macha', { path: '/a' })175 return c.text('Give cookie')176 })177178 it('Set cookie with setCookie() and path option', async () => {179 const res = await app.request('http://localhost/a/set-cookie-path')180 expect(res.status).toBe(200)181 const header = res.headers.get('Set-Cookie')182 expect(header).toBe('delicious_cookie=macha; Path=/a')183 })184185 app.get('/set-signed-cookie', async (c) => {186 const secret = 'secret chocolate chips'187 await setSignedCookie(c, 'delicious_cookie', 'macha', secret)188 return c.text('Give signed cookie')189 })190191 it('Set signed cookie with setSignedCookie()', async () => {192 const res = await app.request('http://localhost/set-signed-cookie')193 expect(res.status).toBe(200)194 const header = res.headers.get('Set-Cookie')195 expect(header).toBe(196 'delicious_cookie=macha.diubJPY8O7hI1pLa42QSfkPiyDWQ0I4DnlACH%2FN2HaA%3D; Path=/'197 )198 })199200 app.get('/a/set-signed-cookie-path', async (c) => {201 const secret = 'secret chocolate chips'202 await setSignedCookie(c, 'delicious_cookie', 'macha', secret, { path: '/a' })203 return c.text('Give signed cookie')204 })205206 it('Set signed cookie with setSignedCookie() and path option', async () => {207 const res = await app.request('http://localhost/a/set-signed-cookie-path')208 expect(res.status).toBe(200)209 const header = res.headers.get('Set-Cookie')210 expect(header).toBe(211 'delicious_cookie=macha.diubJPY8O7hI1pLa42QSfkPiyDWQ0I4DnlACH%2FN2HaA%3D; Path=/a'212 )213 })214215 app.get('/get-secure-prefix-cookie', async (c) => {216 const cookie = getCookie(c, 'delicious_cookie', 'secure')217 if (cookie) {218 return c.text(cookie)219 } else {220 return c.notFound()221 }222 })223224 app.get('/get-host-prefix-cookie', async (c) => {225 const cookie = getCookie(c, 'delicious_cookie', 'host')226 if (cookie) {227 return c.text(cookie)228 } else {229 return c.notFound()230 }231 })232233 app.get('/set-secure-prefix-cookie', (c) => {234 setCookie(c, 'delicious_cookie', 'macha', {235 prefix: 'secure',236 secure: false, // this will be ignore237 })238 return c.text('Set secure prefix cookie')239 })240241 it('Set cookie with secure prefix', async () => {242 const res = await app.request('http://localhost/set-secure-prefix-cookie')243 expect(res.status).toBe(200)244 const header = res.headers.get('Set-Cookie')245 expect(header).toBe('__Secure-delicious_cookie=macha; Path=/; Secure')246 })247248 it('Get cookie with secure prefix', async () => {249 const setCookie = await app.request('http://localhost/set-secure-prefix-cookie')250 const header = setCookie.headers.get('Set-Cookie')251 if (!header) {252 assert.fail('invalid header')253 }254 const res = await app.request('http://localhost/get-secure-prefix-cookie', {255 headers: {256 Cookie: header,257 },258 })259 const response = await res.text()260 expect(res.status).toBe(200)261 expect(response).toBe('macha')262 })263264 app.get('/set-host-prefix-cookie', (c) => {265 setCookie(c, 'delicious_cookie', 'macha', {266 prefix: 'host',267 path: '/foo', // this will be ignored268 domain: 'example.com', // this will be ignored269 secure: false, // this will be ignored270 })271 return c.text('Set host prefix cookie')272 })273274 it('Set cookie with host prefix', async () => {275 const res = await app.request('http://localhost/set-host-prefix-cookie')276 expect(res.status).toBe(200)277 const header = res.headers.get('Set-Cookie')278 expect(header).toBe('__Host-delicious_cookie=macha; Path=/; Secure')279 })280281 it('Get cookie with host prefix', async () => {282 const setCookie = await app.request('http://localhost/set-host-prefix-cookie')283 const header = setCookie.headers.get('Set-Cookie')284 if (!header) {285 assert.fail('invalid header')286 }287 const res = await app.request('http://localhost/get-host-prefix-cookie', {288 headers: {289 Cookie: header,290 },291 })292 const response = await res.text()293 expect(res.status).toBe(200)294 expect(response).toBe('macha')295 })296297 app.get('/set-signed-secure-prefix-cookie', async (c) => {298 await setSignedCookie(c, 'delicious_cookie', 'macha', 'secret choco chips', {299 prefix: 'secure',300 })301 return c.text('Set secure prefix cookie')302 })303304 it('Set signed cookie with secure prefix', async () => {305 const res = await app.request('http://localhost/set-signed-secure-prefix-cookie')306 expect(res.status).toBe(200)307 const header = res.headers.get('Set-Cookie')308 expect(header).toBe(309 '__Secure-delicious_cookie=macha.i225faTyCrJUY8TvpTuJHI20HBWbQ89B4GV7lT4E%2FB0%3D; Path=/; Secure'310 )311 })312313 app.get('/set-signed-host-prefix-cookie', async (c) => {314 await setSignedCookie(c, 'delicious_cookie', 'macha', 'secret choco chips', {315 prefix: 'host',316 domain: 'example.com', // this will be ignored

docs: fix typos in code comments and link third-party middleware section (#5343)Aidanbefore adoptione7b38ee

line 317why, one step back
317 path: 'example.com', // this will be ignored

Merge commit from forkYusuke Wadabefore adoptionf0b094d

lines 318–510why, one step back
318 secure: false, // this will be ignored
319 })320 return c.text('Set host prefix cookie')321 })322323 it('Set signed cookie with host prefix', async () => {324 const res = await app.request('http://localhost/set-signed-host-prefix-cookie')325 expect(res.status).toBe(200)326 const header = res.headers.get('Set-Cookie')327 expect(header).toBe(328 '__Host-delicious_cookie=macha.i225faTyCrJUY8TvpTuJHI20HBWbQ89B4GV7lT4E%2FB0%3D; Path=/; Secure'329 )330 })331332 app.get('/set-cookie-complex', (c) => {333 setCookie(c, 'great_cookie', 'banana', {334 path: '/',335 secure: true,336 domain: 'example.com',337 httpOnly: true,338 maxAge: 1000,339 expires: new Date(Date.UTC(2000, 11, 24, 10, 30, 59, 900)),340 sameSite: 'Strict',341 })342 return c.text('Give cookie')343 })344345 it('Complex pattern', async () => {346 const res = await app.request('http://localhost/set-cookie-complex')347 expect(res.status).toBe(200)348 const header = res.headers.get('Set-Cookie')349 expect(header).toBe(350 'great_cookie=banana; Max-Age=1000; Domain=example.com; Path=/; Expires=Sun, 24 Dec 2000 10:30:59 GMT; HttpOnly; Secure; SameSite=Strict'351 )352 })353354 app.get('/set-signed-cookie-complex', async (c) => {355 const secret = 'secret chocolate chips'356 await setSignedCookie(c, 'great_cookie', 'banana', secret, {357 path: '/',358 secure: true,359 domain: 'example.com',360 httpOnly: true,361 maxAge: 1000,362 expires: new Date(Date.UTC(2000, 11, 24, 10, 30, 59, 900)),363 sameSite: 'Strict',364 })365 return c.text('Give signed cookie')366 })367368 it('Complex pattern (signed)', async () => {369 const res = await app.request('http://localhost/set-signed-cookie-complex')370 expect(res.status).toBe(200)371 const header = res.headers.get('Set-Cookie')372 expect(header).toBe(373 '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'374 )375 })376377 app.get('/set-cookie-multiple', (c) => {378 setCookie(c, 'delicious_cookie', 'macha')379 setCookie(c, 'delicious_cookie', 'choco')380 return c.text('Give cookie')381 })382383 it('Multiple values', async () => {384 const res = await app.request('http://localhost/set-cookie-multiple')385 expect(res.status).toBe(200)386 const header = res.headers.get('Set-Cookie')387 expect(header).toBe('delicious_cookie=macha; Path=/, delicious_cookie=choco; Path=/')388 })389 })390391 describe('Delete cookie', () => {392 const app = new Hono()393394 app.get('/delete-cookie', (c) => {395 deleteCookie(c, 'delicious_cookie')396 return c.text('Give cookie')397 })398399 it('Delete cookie', async () => {400 const res2 = await app.request('http://localhost/delete-cookie')401 expect(res2.status).toBe(200)402 const header2 = res2.headers.get('Set-Cookie')403 expect(header2).toBe('delicious_cookie=; Max-Age=0; Path=/')404 })405406 app.get('/delete-cookie-multiple', (c) => {407 deleteCookie(c, 'delicious_cookie')408 deleteCookie(c, 'delicious_cookie2')409 return c.text('Give cookie')410 })411412 it('Delete multiple cookies', async () => {413 const res2 = await app.request('http://localhost/delete-cookie-multiple')414 expect(res2.status).toBe(200)415 const header2 = res2.headers.get('Set-Cookie')416 expect(header2).toBe(417 'delicious_cookie=; Max-Age=0; Path=/, delicious_cookie2=; Max-Age=0; Path=/'418 )419 })420421 app.get('/delete-cookie-with-options', (c) => {422 deleteCookie(c, 'delicious_cookie', {423 path: '/',424 secure: true,425 domain: 'example.com',426 })427 return c.text('Give cookie')428 })429430 it('Delete cookie with options', async () => {431 const res2 = await app.request('http://localhost/delete-cookie-with-options')432 expect(res2.status).toBe(200)433 const header2 = res2.headers.get('Set-Cookie')434 expect(header2).toBe('delicious_cookie=; Max-Age=0; Domain=example.com; Path=/; Secure')435 })436437 app.get('/delete-cookie-with-deleted-value', (c) => {438 const deleted = deleteCookie(c, 'delicious_cookie')439 return c.text(deleted || '')440 })441442 it('Get deleted value', async () => {443 const cookieString = 'delicious_cookie=choco'444 const req = new Request('http://localhost/delete-cookie-with-deleted-value')445 req.headers.set('Cookie', cookieString)446 const res = await app.request(req)447 expect(res.status).toBe(200)448 expect(await res.text()).toBe('choco')449 })450451 app.get('/delete-cookie-with-prefix', (c) => {452 const deleted = deleteCookie(c, 'delicious_cookie', { prefix: 'secure' })453 return c.text(deleted || '')454 })455456 it('Get deleted value with prefix', async () => {457 const cookieString = '__Secure-delicious_cookie=choco'458 const req = new Request('http://localhost/delete-cookie-with-prefix')459 req.headers.set('Cookie', cookieString)460 const res = await app.request(req)461 expect(res.status).toBe(200)462 expect(await res.text()).toBe('choco')463 })464 })465466 describe('Generate cookie', () => {467 it('should generate a cookie', () => {468 const cookie = generateCookie('delicious_cookie', 'macha')469 expect(cookie).toBe('delicious_cookie=macha; Path=/')470 })471472 it('should generate a cookie with options', () => {473 const cookie = generateCookie('delicious_cookie', 'macha', {474 path: '/',475 secure: true,476 httpOnly: true,477 domain: 'example.com',478 })479 expect(cookie).toBe('delicious_cookie=macha; Domain=example.com; Path=/; HttpOnly; Secure')480 })481482 it('should generate a signed cookie', async () => {483 const cookie = await generateSignedCookie(484 'delicious_cookie',485 'macha',486 'secret chocolate chips'487 )488 expect(cookie).toBe(489 'delicious_cookie=macha.diubJPY8O7hI1pLa42QSfkPiyDWQ0I4DnlACH%2FN2HaA%3D; Path=/'490 )491 })492493 it('should generate a signed cookie with options', async () => {494 const cookie = await generateSignedCookie(495 'delicious_cookie',496 'macha',497 'secret chocolate chips',498 {499 path: '/',500 secure: true,501 httpOnly: true,502 domain: 'example.com',503 }504 )505 expect(cookie).toBe(506 'delicious_cookie=macha.diubJPY8O7hI1pLa42QSfkPiyDWQ0I4DnlACH%2FN2HaA%3D; Domain=example.com; Path=/; HttpOnly; Secure'507 )508 })509 })510})