Skip to content

Commit 4d0eace

Browse files
authored
fix: handle undici Headers and Maps in redirect-handler (#3819)
1 parent d62731c commit 4d0eace

File tree

2 files changed

+59
-2
lines changed

2 files changed

+59
-2
lines changed

lib/handler/redirect-handler.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ function cleanRequestHeaders (headers, removeContent, unknownOrigin) {
228228
}
229229
}
230230
} else if (headers && typeof headers === 'object') {
231-
const entries = headers instanceof Headers ? headers.entries() : Object.entries(headers)
231+
const entries = typeof headers[Symbol.iterator] === 'function' ? headers : Object.entries(headers)
232232
for (const [key, value] of entries) {
233233
if (!shouldRemoveHeader(key, removeContent, unknownOrigin)) {
234234
ret.push(key, value)

test/redirect-request.js

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ const {
1313
startRedirectingWithQueryParams
1414
} = require('./utils/redirecting-servers')
1515
const { createReadable, createReadableStream } = require('./utils/stream')
16+
const { Headers: UndiciHeaders } = require('..')
1617
const redirect = undici.interceptors.redirect
1718

1819
for (const factory of [
@@ -227,7 +228,7 @@ for (const factory of [
227228
await t.completed
228229
})
229230

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

233234
const server = await startRedirectingServer()
@@ -255,6 +256,62 @@ for (const factory of [
255256
await t.completed
256257
})
257258

259+
test('should remove Host and request body related headers when following HTTP 303 (Undici Headers)', async t => {
260+
t = tspl(t, { plan: 3 })
261+
262+
const server = await startRedirectingServer()
263+
264+
const { statusCode, headers, body: bodyStream } = await request(t, server, undefined, `http://${server}/303`, {
265+
method: 'PATCH',
266+
headers: new UndiciHeaders({
267+
'Content-Encoding': 'gzip',
268+
'X-Foo1': '1',
269+
'X-Foo2': '2',
270+
'Content-Type': 'application/json',
271+
'X-Foo3': '3',
272+
Host: 'localhost',
273+
'X-Bar': '4'
274+
}),
275+
maxRedirections: 10
276+
})
277+
278+
const body = await bodyStream.text()
279+
280+
t.strictEqual(statusCode, 200)
281+
t.ok(!headers.location)
282+
t.strictEqual(body, `GET /5 :: host@${server} connection@keep-alive x-bar@4 x-foo1@1 x-foo2@2 x-foo3@3`)
283+
284+
await t.completed
285+
})
286+
287+
test('should remove Host and request body related headers when following HTTP 303 (Maps)', async t => {
288+
t = tspl(t, { plan: 3 })
289+
290+
const server = await startRedirectingServer()
291+
292+
const { statusCode, headers, body: bodyStream } = await request(t, server, undefined, `http://${server}/303`, {
293+
method: 'PATCH',
294+
headers: new Map([
295+
['Content-Encoding', 'gzip'],
296+
['X-Foo1', '1'],
297+
['X-Foo2', '2'],
298+
['Content-Type', 'application/json'],
299+
['X-Foo3', '3'],
300+
['Host', 'localhost'],
301+
['X-Bar', '4']
302+
]),
303+
maxRedirections: 10
304+
})
305+
306+
const body = await bodyStream.text()
307+
308+
t.strictEqual(statusCode, 200)
309+
t.ok(!headers.location)
310+
t.strictEqual(body, `GET /5 :: host@${server} connection@keep-alive x-foo1@1 x-foo2@2 x-foo3@3 x-bar@4`)
311+
312+
await t.completed
313+
})
314+
258315
test('should follow redirection after a HTTP 307', async t => {
259316
t = tspl(t, { plan: 3 })
260317

0 commit comments

Comments
 (0)