0
0
mirror of https://github.com/honojs/hono.git synced 2024-11-22 11:17:33 +01:00

fix(helper/cookie): make default Path=/ for setCookie()/setSignedCookie() (#1742)

* fix(helper/cookie): make default `path` as `/` for `setCookie()`/`setSignedCookie()`

* denoify

* fixed test
This commit is contained in:
Yusuke Wada 2023-11-28 17:52:34 +09:00 committed by GitHub
parent c3275eebf0
commit 7dc215577a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 42 additions and 11 deletions

View File

@ -39,7 +39,7 @@ export const getSignedCookie: GetSignedCookie = async (c, secret, key?) => {
}
export const setCookie = (c: Context, name: string, value: string, opt?: CookieOptions): void => {
const cookie = serialize(name, value, opt)
const cookie = serialize(name, value, { path: '/', ...opt })
c.header('set-cookie', cookie, { append: true })
}
@ -50,7 +50,7 @@ export const setSignedCookie = async (
secret: string | BufferSource,
opt?: CookieOptions
): Promise<void> => {
const cookie = await serializeSigned(name, value, secret, opt)
const cookie = await serializeSigned(name, value, secret, { path: '/', ...opt })
c.header('set-cookie', cookie, { append: true })
}

View File

@ -95,14 +95,14 @@ describe('AWS Lambda Adapter for Hono', () => {
key: 'id',
value: crypto.randomUUID(),
get serialized() {
return `${this.key}=${this.value}`
return `${this.key}=${this.value}; Path=/`
},
}
const testCookie2 = {
key: 'secret',
value: crypto.randomUUID(),
get serialized() {
return `${this.key}=${this.value}`
return `${this.key}=${this.value}; Path=/`
},
}

View File

@ -148,7 +148,19 @@ describe('Cookie Middleware', () => {
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')
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) => {
@ -161,7 +173,24 @@ describe('Cookie Middleware', () => {
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')
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('/set-cookie-complex', (c) => {
@ -219,7 +248,7 @@ describe('Cookie Middleware', () => {
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, delicious_cookie=choco')
expect(header).toBe('delicious_cookie=macha; Path=/, delicious_cookie=choco; Path=/')
})
})
@ -235,7 +264,7 @@ describe('Cookie Middleware', () => {
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')
expect(header2).toBe('delicious_cookie=; Max-Age=0; Path=/')
})
app.get('/delete-cookie-multiple', (c) => {
@ -248,7 +277,9 @@ describe('Cookie Middleware', () => {
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, delicious_cookie2=; Max-Age=0')
expect(header2).toBe(
'delicious_cookie=; Max-Age=0; Path=/, delicious_cookie2=; Max-Age=0; Path=/'
)
})
app.get('/delete-cookie-with-options', (c) => {

View File

@ -39,7 +39,7 @@ export const getSignedCookie: GetSignedCookie = async (c, secret, key?) => {
}
export const setCookie = (c: Context, name: string, value: string, opt?: CookieOptions): void => {
const cookie = serialize(name, value, opt)
const cookie = serialize(name, value, { path: '/', ...opt })
c.header('set-cookie', cookie, { append: true })
}
@ -50,7 +50,7 @@ export const setSignedCookie = async (
secret: string | BufferSource,
opt?: CookieOptions
): Promise<void> => {
const cookie = await serializeSigned(name, value, secret, opt)
const cookie = await serializeSigned(name, value, secret, { path: '/', ...opt })
c.header('set-cookie', cookie, { append: true })
}