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(Request): allow passing Headers instance to init.headers #1401

Merged
merged 5 commits into from May 8, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 3 additions & 4 deletions lib/fetch/request.js
Expand Up @@ -420,10 +420,9 @@ class Request {
// 4. If headers is a Headers object, then for each header in its header
// list, append header’s name/header’s value to this’s headers.
if (headers instanceof Headers) {
// TODO (fix): Why doesn't this work?
// for (const [key, val] of headers[kHeadersList]) {
// this[kHeaders].append(key, val)
// }
for (const [key, val] of headers) {
KhafraDev marked this conversation as resolved.
Show resolved Hide resolved
this[kHeaders].append(key, val)
}
KhafraDev marked this conversation as resolved.
Show resolved Hide resolved

this[kState].headersList = new HeadersList([
...this[kState].headersList,
Expand Down
35 changes: 34 additions & 1 deletion test/fetch/request.js
Expand Up @@ -4,7 +4,8 @@

const { test } = require('tap')
const {
Request
Request,
Headers
} = require('../../')
const { kState } = require('../../lib/fetch/symbols.js')

Expand Down Expand Up @@ -272,3 +273,35 @@ test('post aborted signal cloned', t => {
})
ac.abort()
})

test('Passing headers in init', (t) => {
// https://github.com/nodejs/undici/issues/1400
t.test('Headers instance', (t) => {
const req = new Request('http://localhost', {
headers: new Headers({ key: 'value' })
})

t.equal(req.headers.get('key'), 'value')
t.end()
})

t.test('key:value object', (t) => {
const req = new Request('http://localhost', {
headers: { key: 'value' }
})

t.equal(req.headers.get('key'), 'value')
t.end()
})

t.test('[key, value][]', (t) => {
const req = new Request('http://localhost', {
headers: [['key', 'value']]
})

t.equal(req.headers.get('key'), 'value')
t.end()
})

t.end()
})