Skip to content

Commit

Permalink
fix(Request): URLSearchParams body w/ Headers obj
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev committed May 3, 2022
1 parent 3ec5a8e commit 0babdb1
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
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’s 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)
}
}

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

0 comments on commit 0babdb1

Please sign in to comment.