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

fix(fetch): remove content-length header on redirect #2022

Merged
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
8 changes: 7 additions & 1 deletion lib/fetch/constants.js
Expand Up @@ -48,11 +48,17 @@ const requestCache = [
'only-if-cached'
]

// https://fetch.spec.whatwg.org/#request-body-header-name
const requestBodyHeader = [
'content-encoding',
'content-language',
'content-location',
'content-type'
'content-type',
// See https://github.com/nodejs/undici/issues/2021
// 'Content-Length' is a forbidden header name, which is typically
// removed in the Headers implementation. However, undici doesn't
// filter out headers, so we add it here.
'content-length'
]

// https://fetch.spec.whatwg.org/#enumdef-requestduplex
Expand Down
32 changes: 32 additions & 0 deletions test/fetch/issue-2021.js
@@ -0,0 +1,32 @@
'use strict'

const { test } = require('tap')
const { once } = require('events')
const { createServer } = require('http')
const { fetch } = require('../..')

// https://github.com/nodejs/undici/issues/2021
test('content-length header is removed on redirect', async (t) => {
const server = createServer((req, res) => {
if (req.url === '/redirect') {
res.writeHead(302, { Location: '/redirect2' })
res.end()
return
}

res.end()
}).listen(0).unref()

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

const body = 'a+b+c'

await t.resolves(fetch(`http://localhost:${server.address().port}/redirect`, {
method: 'POST',
body,
headers: {
'content-length': Buffer.byteLength(body)
}
}))
})