Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 3 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions deno_dist/helper/cookie/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}

Expand All @@ -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 })
}

Expand Down
4 changes: 2 additions & 2 deletions runtime_tests/lambda/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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=/`
},
}

Expand Down
41 changes: 36 additions & 5 deletions src/helper/cookie/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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) => {
Expand Down Expand Up @@ -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=/')
})
})

Expand All @@ -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) => {
Expand All @@ -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) => {
Expand Down
4 changes: 2 additions & 2 deletions src/helper/cookie/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 })
}

Expand All @@ -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 })
}

Expand Down
Loading