Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions lib/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ const requestFinalizer = new FinalizationRegistry(({ signal, abort }) => {
signal.removeEventListener('abort', abort)
})

let patchMethodWarning = false

// https://fetch.spec.whatwg.org/#request-class
class Request {
// https://fetch.spec.whatwg.org/#dom-request
Expand Down Expand Up @@ -335,6 +337,14 @@ class Request {
// 4. Set request’s method to method.
request.method = method
}

if (!patchMethodWarning && request.method === 'patch') {
process.emitWarning('Using `patch` is highly likely to result in a `405 Method Not Allowed`. `PATCH` is much more likely to succeed.', {
code: 'UNDICI-FETCH-patch'
})

patchMethodWarning = true
}
}

// 26. If init["signal"] exists, then set signal to it.
Expand Down
21 changes: 21 additions & 0 deletions test/fetch/issue-2294-patch-method.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
'use strict'

const { test } = require('tap')
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we use tap rather than node:test here? (context: #2267)

Copy link
Member Author

Choose a reason for hiding this comment

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

these tests use tap still

const { Request } = require('../..')

test('Using `patch` method emits a warning.', (t) => {
t.plan(1)

const { emitWarning } = process

t.teardown(() => {
process.emitWarning = emitWarning
})

process.emitWarning = (warning, options) => {
t.equal(options.code, 'UNDICI-FETCH-patch')
}

// eslint-disable-next-line no-new
new Request('https://a', { method: 'patch' })
})