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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Request): URLSearchParams body w/ Headers obj #1410

Merged
merged 1 commit into from May 4, 2022
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/fetch/request.js
Expand Up @@ -467,8 +467,8 @@ class Request {
// not contain `Content-Type`, then append `Content-Type`/Content-Type to
// this鈥檚 headers.
if (contentType && !this[kHeaders].has('content-type')) {
this[kHeaders].append('content-type', contentType)
this[kState].headersList.append('content-type', contentType)
this[kHeaders].set('content-type', contentType)
this[kState].headersList.set('content-type', contentType)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the reason for the duplication is this[kHeaders][kHeadersList] === this[kState].headersList, making this second call redundant.

See

return this[kHeadersList].append(String(args[0]), String(args[1]))

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought so too, but removing the second call caused some tests (from the node-fetch test suite) to fail.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think here we should use .set. If I remember correctly, the old implementation was using .set/.push as well.

}
}

Expand Down
22 changes: 22 additions & 0 deletions test/fetch/request.js
Expand Up @@ -7,6 +7,7 @@ const {
Request
} = require('../../')
const { kState } = require('../../lib/fetch/symbols.js')
const { URLSearchParams } = require('url')

test('arg validation', (t) => {
// constructor
Expand Down Expand Up @@ -261,6 +262,27 @@ test('pre aborted signal cloned', t => {
t.end()
})

test('URLSearchParams body with Headers object - issue #1407', async (t) => {
const body = new URLSearchParams({
abc: 123
})

const request = new Request(
'http://localhost',
{
method: 'POST',
body,
headers: {
Authorization: 'test'
}
}
)

t.equal(request.headers.get('content-type'), 'application/x-www-form-urlencoded;charset=UTF-8')
t.equal(request.headers.get('authorization'), 'test')
t.equal(await request.text(), 'abc=123')
})

test('post aborted signal cloned', t => {
t.plan(2)

Expand Down