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(utils): accept HeadersInit, null, undefined in buildOutgoingHttpHeaders #212

Merged
merged 1 commit into from
Nov 4, 2024
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
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',
})
})
})