Skip to content

Commit

Permalink
fix: handle undici Headers and Maps in redirect-handler (#3819)
Browse files Browse the repository at this point in the history
  • Loading branch information
iiAku authored Nov 10, 2024
1 parent d62731c commit 4d0eace
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/handler/redirect-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ function cleanRequestHeaders (headers, removeContent, unknownOrigin) {
}
}
} else if (headers && typeof headers === 'object') {
const entries = headers instanceof Headers ? headers.entries() : Object.entries(headers)
const entries = typeof headers[Symbol.iterator] === 'function' ? headers : Object.entries(headers)
for (const [key, value] of entries) {
if (!shouldRemoveHeader(key, removeContent, unknownOrigin)) {
ret.push(key, value)
Expand Down
59 changes: 58 additions & 1 deletion test/redirect-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {
startRedirectingWithQueryParams
} = require('./utils/redirecting-servers')
const { createReadable, createReadableStream } = require('./utils/stream')
const { Headers: UndiciHeaders } = require('..')
const redirect = undici.interceptors.redirect

for (const factory of [
Expand Down Expand Up @@ -227,7 +228,7 @@ for (const factory of [
await t.completed
})

test('should remove Host and request body related headers when following HTTP 303 (Headers)', async t => {
test('should remove Host and request body related headers when following HTTP 303 (Global Headers)', async t => {
t = tspl(t, { plan: 3 })

const server = await startRedirectingServer()
Expand Down Expand Up @@ -255,6 +256,62 @@ for (const factory of [
await t.completed
})

test('should remove Host and request body related headers when following HTTP 303 (Undici Headers)', async t => {
t = tspl(t, { plan: 3 })

const server = await startRedirectingServer()

const { statusCode, headers, body: bodyStream } = await request(t, server, undefined, `http://${server}/303`, {
method: 'PATCH',
headers: new UndiciHeaders({
'Content-Encoding': 'gzip',
'X-Foo1': '1',
'X-Foo2': '2',
'Content-Type': 'application/json',
'X-Foo3': '3',
Host: 'localhost',
'X-Bar': '4'
}),
maxRedirections: 10
})

const body = await bodyStream.text()

t.strictEqual(statusCode, 200)
t.ok(!headers.location)
t.strictEqual(body, `GET /5 :: host@${server} connection@keep-alive x-bar@4 x-foo1@1 x-foo2@2 x-foo3@3`)

await t.completed
})

test('should remove Host and request body related headers when following HTTP 303 (Maps)', async t => {
t = tspl(t, { plan: 3 })

const server = await startRedirectingServer()

const { statusCode, headers, body: bodyStream } = await request(t, server, undefined, `http://${server}/303`, {
method: 'PATCH',
headers: new Map([
['Content-Encoding', 'gzip'],
['X-Foo1', '1'],
['X-Foo2', '2'],
['Content-Type', 'application/json'],
['X-Foo3', '3'],
['Host', 'localhost'],
['X-Bar', '4']
]),
maxRedirections: 10
})

const body = await bodyStream.text()

t.strictEqual(statusCode, 200)
t.ok(!headers.location)
t.strictEqual(body, `GET /5 :: host@${server} connection@keep-alive x-foo1@1 x-foo2@2 x-foo3@3 x-bar@4`)

await t.completed
})

test('should follow redirection after a HTTP 307', async t => {
t = tspl(t, { plan: 3 })

Expand Down

0 comments on commit 4d0eace

Please sign in to comment.