Skip to content
Closed
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
6 changes: 6 additions & 0 deletions lib/mock/mock-utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -386,6 +386,12 @@ function dispatchMockReply (mockDispatches, mockDispatch, key, opts, handler, re
paused: false,
rawHeaders: null,
rawTrailers: null,
// Mirrors the `aborted` property of a real Request, which fetch consults to
// decide whether an error on the current connection was caused by itself
// aborting the connection (e.g. during a redirect)
get aborted () {
return aborted
},
pause () {
this.paused = true
},
Expand Down
22 changes: 22 additions & 0 deletions test/fetch/issue-5963.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
'use strict'

const { test } = require('node:test')
const { fetch, MockAgent } = require('../..')

// https://github.com/nodejs/undici/issues/5963
test('MockAgent follows a redirect', async (t) => {
const mockAgent = new MockAgent()
mockAgent.disableNetConnect()
t.after(() => mockAgent.close())

const mockPool = mockAgent.get('http://localhost')
mockPool.intercept({ path: '/redirect', method: 'GET' })
.reply(307, '', { headers: { location: 'http://localhost/final' } })
mockPool.intercept({ path: '/final', method: 'GET' }).reply(200, 'ok')

const response = await fetch('http://localhost/redirect', { dispatcher: mockAgent })

t.assert.strictEqual(response.status, 200)
t.assert.strictEqual(await response.text(), 'ok')
t.assert.ok(response.redirected)
})