diff --git a/lib/handler/redirect-handler.js b/lib/handler/redirect-handler.js index 5121cf2263b..2f1995e6a92 100644 --- a/lib/handler/redirect-handler.js +++ b/lib/handler/redirect-handler.js @@ -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) diff --git a/test/redirect-request.js b/test/redirect-request.js index 50c8931a5f1..4b7d114319f 100644 --- a/test/redirect-request.js +++ b/test/redirect-request.js @@ -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 [ @@ -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() @@ -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 })