Skip to content

Commit

Permalink
fix(lambda-edge): handle multiple Set-Cookie headers correctly
Browse files Browse the repository at this point in the history
Previously, only the last Set-Cookie header was returned. This update ensures all Set-Cookie headers are processed and returned as expected.
  • Loading branch information
Kent committed Jan 26, 2025
1 parent 8e03de6 commit 2dd251d
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 1 deletion.
32 changes: 32 additions & 0 deletions src/adapter/lambda-edge/handler.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { describe } from 'vitest'
import { setCookie } from '../../helper/cookie'
import { Hono } from '../../hono'
import { encodeBase64 } from '../../utils/encode'
import type { CloudFrontEdgeEvent } from './handler'
Expand Down Expand Up @@ -87,4 +88,35 @@ describe('handle', () => {

expect(res.body).toBe('https://hono.dev/test-path')
})

it('Should support multiple cookies', async () => {
const app = new Hono()
app.get('/test-path', (c) => {
setCookie(c, 'cookie1', 'value1')
setCookie(c, 'cookie2', 'value2')
return c.text('')
})
const handler = handle(app)

const res = await handler(cloudFrontEdgeEvent)

expect(res.headers).toEqual({
'content-type': [
{
key: 'content-type',
value: 'text/plain; charset=UTF-8',
},
],
'set-cookie': [
{
key: 'set-cookie',
value: 'cookie1=value1; Path=/',
},
{
key: 'set-cookie',
value: 'cookie2=value2; Path=/',
},
],
})
})
})
5 changes: 4 additions & 1 deletion src/adapter/lambda-edge/handler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ interface CloudFrontResult {
const convertHeaders = (headers: Headers): CloudFrontHeaders => {
const cfHeaders: CloudFrontHeaders = {}
headers.forEach((value, key) => {
cfHeaders[key.toLowerCase()] = [{ key: key.toLowerCase(), value }]
cfHeaders[key.toLowerCase()] = [
...(cfHeaders[key.toLowerCase()] || []),
{ key: key.toLowerCase(), value },
]
})
return cfHeaders
}
Expand Down

0 comments on commit 2dd251d

Please sign in to comment.