Skip to content

Commit

Permalink
fix(fetch): do not assign default value to RequestInit.method (#1529)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev authored Jul 5, 2022
1 parent 2a2d2bb commit 5ca25c2
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/fetch/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class Fetch extends EE {
}

// https://fetch.spec.whatwg.org/#fetch-method
async function fetch (input, init = undefined) {
async function fetch (input, init = {}) {
if (arguments.length < 1) {
throw new TypeError(
`Failed to execute 'fetch' on 'Window': 1 argument required, but only ${arguments.length} present.`
Expand Down
3 changes: 1 addition & 2 deletions lib/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -843,8 +843,7 @@ webidl.converters.AbortSignal = webidl.interfaceConverter(
webidl.converters.RequestInit = webidl.dictionaryConverter([
{
key: 'method',
converter: webidl.converters.ByteString,
defaultValue: 'GET'
converter: webidl.converters.ByteString
},
{
key: 'headers',
Expand Down
21 changes: 21 additions & 0 deletions test/fetch/client-fetch.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const { Blob } = require('buffer')
const { fetch, Response, Request, FormData, File } = require('../..')
const { Client, setGlobalDispatcher, Agent } = require('../..')
const nodeFetch = require('../../index-fetch')
const { once } = require('events')

setGlobalDispatcher(new Agent({
keepAliveTimeout: 1,
Expand Down Expand Up @@ -419,3 +420,23 @@ test('error on redirect', async (t) => {
t.equal(errorCause.message, 'unexpected redirect')
})
})

// https://github.com/nodejs/undici/issues/1527
test('fetching with Request object - issue #1527', async (t) => {
const server = createServer((req, res) => {
t.pass()
res.end()
}).listen(0)

t.teardown(server.close.bind(server))
await once(server, 'listening')

const body = JSON.stringify({ foo: 'bar' })
const request = new Request(`http://localhost:${server.address().port}`, {
method: 'POST',
body
})

await t.resolves(fetch(request))
t.end()
})

0 comments on commit 5ca25c2

Please sign in to comment.