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

fetch: warn when using patch method #2577

Merged
merged 5 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from 4 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
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 @@ -334,6 +336,14 @@ class Request {

// 4. Set request’s method to method.
request.method = method

if (!patchMethodWarning && 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
}
}
}

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' })
})