Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions lib/handler/redirect-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,11 @@ class RedirectHandler {
}

onResponseStart (controller, statusCode, headers, statusMessage) {
if (statusCode < 200) {
this.handler.onResponseStart?.(controller, statusCode, headers, statusMessage)
return
}

if (this.opts.throwOnMaxRedirect && this.history.length >= this.maxRedirections) {
throw new Error('max redirects')
}
Expand Down
32 changes: 32 additions & 0 deletions test/interceptors/redirect.js
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,38 @@ test('should redirect to relative URL according to RFC 7231', async t => {
t.strictEqual(finalPath, '/absolute/b')
})

test('informational responses are not added to redirect history', async t => {
t = tspl(t, { plan: 4 })

const server = createServer((_req, res) => {
for (let i = 0; i < 20; i++) {
res.writeProcessing()
}
res.end('hello world!')
}).listen(0)

after(() => server.close())
await once(server, 'listening')

const dispatcher = new undici.Client(`http://localhost:${server.address().port}`)
.compose(redirect({ maxRedirections: 1, throwOnMaxRedirect: true }))
after(() => dispatcher.close())

const infos = []
const response = await dispatcher.request({
method: 'GET',
path: '/',
onInfo: info => infos.push(info)
})

t.strictEqual(response.statusCode, 200)
t.strictEqual(await response.body.text(), 'hello world!')
t.strictEqual(infos.length, 20)
t.deepStrictEqual(response.context.history, [
new URL(`http://localhost:${server.address().port}/`)
])
})

test('same-origin redirect preserves plain object headers with polluted Object.prototype[Symbol.iterator]', async (t) => {
const { strictEqual } = tspl(t, { plan: 2 })

Expand Down
Loading