diff --git a/src/utils.ts b/src/utils.ts index d73e6fb..c704c93 100644 --- a/src/utils.ts +++ b/src/utils.ts @@ -42,24 +42,21 @@ export function writeFromReadableStream(stream: ReadableStream, writ } export const buildOutgoingHttpHeaders = ( - headers: Headers | Record + headers: Headers | HeadersInit | null | undefined ): OutgoingHttpHeaders => { const res: OutgoingHttpHeaders = {} + if (!(headers instanceof Headers)) { + headers = new Headers(headers ?? undefined) + } const cookies = [] - const entries = - headers instanceof Headers - ? headers.entries() - : Object.entries(headers).filter(([, value]) => value) - - for (const [k, v] of entries) { + for (const [k, v] of headers) { if (k === 'set-cookie') { cookies.push(v) } else { res[k] = v } } - if (cookies.length > 0) { res['set-cookie'] = cookies } diff --git a/test/utils.test.ts b/test/utils.test.ts new file mode 100644 index 0000000..3673c5f --- /dev/null +++ b/test/utils.test.ts @@ -0,0 +1,73 @@ +import { buildOutgoingHttpHeaders } from '../src/utils' + +describe('buildOutgoingHttpHeaders', () => { + it('original content-type is preserved', () => { + const headers = new Headers({ + a: 'b', + 'content-type': 'text/html; charset=UTF-8', + }) + const result = buildOutgoingHttpHeaders(headers) + expect(result).toEqual({ + a: 'b', + 'content-type': 'text/html; charset=UTF-8', + }) + }) + + it('multiple set-cookie', () => { + const headers = new Headers() + headers.append('set-cookie', 'a') + headers.append('set-cookie', 'b') + const result = buildOutgoingHttpHeaders(headers) + expect(result).toEqual({ + 'set-cookie': ['a', 'b'], + 'content-type': 'text/plain; charset=UTF-8', + }) + }) + + it('Headers', () => { + const headers = new Headers({ + a: 'b', + }) + const result = buildOutgoingHttpHeaders(headers) + expect(result).toEqual({ + a: 'b', + 'content-type': 'text/plain; charset=UTF-8', + }) + }) + + it('Record', () => { + const headers = { + a: 'b', + 'Set-Cookie': 'c', // case-insensitive + } + const result = buildOutgoingHttpHeaders(headers) + expect(result).toEqual({ + a: 'b', + 'set-cookie': ['c'], + 'content-type': 'text/plain; charset=UTF-8', + }) + }) + + it('Record[]', () => { + const headers: HeadersInit = [['a', 'b']] + const result = buildOutgoingHttpHeaders(headers) + expect(result).toEqual({ + a: 'b', + 'content-type': 'text/plain; charset=UTF-8', + }) + }) + + it('null', () => { + const result = buildOutgoingHttpHeaders(null) + expect(result).toEqual({ + 'content-type': 'text/plain; charset=UTF-8', + }) + }) + + it('undefined', () => { + const result = buildOutgoingHttpHeaders(undefined) + expect(result).toEqual({ + 'content-type': 'text/plain; charset=UTF-8', + }) + }) +})