Skip to content

Commit

Permalink
fix(utils): accept HeadersInit, null, undefined in buildOutgoingHttpH…
Browse files Browse the repository at this point in the history
…eaders (#212)
  • Loading branch information
usualoma authored Nov 4, 2024
1 parent e63a808 commit 9327d9c
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 8 deletions.
13 changes: 5 additions & 8 deletions src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,24 +42,21 @@ export function writeFromReadableStream(stream: ReadableStream<Uint8Array>, writ
}

export const buildOutgoingHttpHeaders = (
headers: Headers | Record<string, string>
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
}
Expand Down
73 changes: 73 additions & 0 deletions test/utils.test.ts
Original file line number Diff line number Diff line change
@@ -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<string, string>', () => {
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<string, string>[]', () => {
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',
})
})
})

0 comments on commit 9327d9c

Please sign in to comment.