Skip to content

Commit 46ebb31

Browse files
committed
fix: support tuple headers in responseViaCache
1 parent 7cfd1e3 commit 46ebb31

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

src/listener.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,10 @@ const responseViaCache = async (
7777
} else if (header instanceof Headers) {
7878
hasContentLength = header.has('content-length')
7979
header = buildOutgoingHttpHeaders(header)
80+
} else if (Array.isArray(header)) {
81+
const headerObj = new Headers(header)
82+
hasContentLength = headerObj.has('content-length')
83+
header = buildOutgoingHttpHeaders(headerObj)
8084
} else {
8185
for (const key in header) {
8286
if (key.length === 14 && key.toLowerCase() === 'content-length') {

src/response.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ export const cacheKey = Symbol('cache')
1010
export type InternalCache = [
1111
number,
1212
string | ReadableStream,
13-
Record<string, string> | Headers | OutgoingHttpHeaders | undefined,
13+
Record<string, string> | [string, string][] | Headers | OutgoingHttpHeaders | undefined,
1414
]
1515
interface LightResponse {
1616
[responseCache]?: globalThis.Response

test/server.test.ts

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,25 @@ describe('various response body types', () => {
254254
})
255255
return response
256256
})
257+
app.get('/text-with-content-length-array', () => {
258+
const response = new Response('Hello Hono!', {
259+
headers: [
260+
['content-type', 'text/plain'],
261+
['content-length', '00011'],
262+
],
263+
})
264+
return response
265+
})
266+
app.get('/text-with-set-cookie-array', () => {
267+
const response = new Response('Hello Hono!', {
268+
headers: [
269+
['content-type', 'text/plain'],
270+
['set-cookie', 'a=1'],
271+
['set-cookie', 'b=2'],
272+
],
273+
})
274+
return response
275+
})
257276

258277
app.use('/etag/*', etag())
259278
app.get('/etag/buffer', () => {
@@ -400,6 +419,22 @@ describe('various response body types', () => {
400419
expect(res.text).toBe('Hello Hono!')
401420
})
402421

422+
it('Should return 200 response - GET /text-with-content-length-array', async () => {
423+
const res = await request(server).get('/text-with-content-length-array')
424+
expect(res.status).toBe(200)
425+
expect(res.headers['content-type']).toMatch('text/plain')
426+
expect(res.headers['content-length']).toBe('00011')
427+
expect(res.text).toBe('Hello Hono!')
428+
})
429+
430+
it('Should return 200 response - GET /text-with-set-cookie-array', async () => {
431+
const res = await request(server).get('/text-with-set-cookie-array')
432+
expect(res.status).toBe(200)
433+
expect(res.headers['content-type']).toMatch('text/plain')
434+
expect(res.headers['set-cookie']).toEqual(['a=1', 'b=2'])
435+
expect(res.text).toBe('Hello Hono!')
436+
})
437+
403438
it('Should return 200 response - GET /etag/buffer', async () => {
404439
const res = await request(server).get('/etag/buffer')
405440
expect(res.status).toBe(200)

0 commit comments

Comments
 (0)