Skip to content

Commit 9327d9c

Browse files
authored
fix(utils): accept HeadersInit, null, undefined in buildOutgoingHttpHeaders (#212)
1 parent e63a808 commit 9327d9c

File tree

2 files changed

+78
-8
lines changed

2 files changed

+78
-8
lines changed

src/utils.ts

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -42,24 +42,21 @@ export function writeFromReadableStream(stream: ReadableStream<Uint8Array>, writ
4242
}
4343

4444
export const buildOutgoingHttpHeaders = (
45-
headers: Headers | Record<string, string>
45+
headers: Headers | HeadersInit | null | undefined
4646
): OutgoingHttpHeaders => {
4747
const res: OutgoingHttpHeaders = {}
48+
if (!(headers instanceof Headers)) {
49+
headers = new Headers(headers ?? undefined)
50+
}
4851

4952
const cookies = []
50-
const entries =
51-
headers instanceof Headers
52-
? headers.entries()
53-
: Object.entries(headers).filter(([, value]) => value)
54-
55-
for (const [k, v] of entries) {
53+
for (const [k, v] of headers) {
5654
if (k === 'set-cookie') {
5755
cookies.push(v)
5856
} else {
5957
res[k] = v
6058
}
6159
}
62-
6360
if (cookies.length > 0) {
6461
res['set-cookie'] = cookies
6562
}

test/utils.test.ts

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
import { buildOutgoingHttpHeaders } from '../src/utils'
2+
3+
describe('buildOutgoingHttpHeaders', () => {
4+
it('original content-type is preserved', () => {
5+
const headers = new Headers({
6+
a: 'b',
7+
'content-type': 'text/html; charset=UTF-8',
8+
})
9+
const result = buildOutgoingHttpHeaders(headers)
10+
expect(result).toEqual({
11+
a: 'b',
12+
'content-type': 'text/html; charset=UTF-8',
13+
})
14+
})
15+
16+
it('multiple set-cookie', () => {
17+
const headers = new Headers()
18+
headers.append('set-cookie', 'a')
19+
headers.append('set-cookie', 'b')
20+
const result = buildOutgoingHttpHeaders(headers)
21+
expect(result).toEqual({
22+
'set-cookie': ['a', 'b'],
23+
'content-type': 'text/plain; charset=UTF-8',
24+
})
25+
})
26+
27+
it('Headers', () => {
28+
const headers = new Headers({
29+
a: 'b',
30+
})
31+
const result = buildOutgoingHttpHeaders(headers)
32+
expect(result).toEqual({
33+
a: 'b',
34+
'content-type': 'text/plain; charset=UTF-8',
35+
})
36+
})
37+
38+
it('Record<string, string>', () => {
39+
const headers = {
40+
a: 'b',
41+
'Set-Cookie': 'c', // case-insensitive
42+
}
43+
const result = buildOutgoingHttpHeaders(headers)
44+
expect(result).toEqual({
45+
a: 'b',
46+
'set-cookie': ['c'],
47+
'content-type': 'text/plain; charset=UTF-8',
48+
})
49+
})
50+
51+
it('Record<string, string>[]', () => {
52+
const headers: HeadersInit = [['a', 'b']]
53+
const result = buildOutgoingHttpHeaders(headers)
54+
expect(result).toEqual({
55+
a: 'b',
56+
'content-type': 'text/plain; charset=UTF-8',
57+
})
58+
})
59+
60+
it('null', () => {
61+
const result = buildOutgoingHttpHeaders(null)
62+
expect(result).toEqual({
63+
'content-type': 'text/plain; charset=UTF-8',
64+
})
65+
})
66+
67+
it('undefined', () => {
68+
const result = buildOutgoingHttpHeaders(undefined)
69+
expect(result).toEqual({
70+
'content-type': 'text/plain; charset=UTF-8',
71+
})
72+
})
73+
})

0 commit comments

Comments
 (0)