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
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,7 @@ undici-fetch.js

# File generated by /test/request-timeout.js
test/request-timeout.10mb.bin

# Claude files
CLAUDE.md
.claude
7 changes: 6 additions & 1 deletion lib/core/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class Request {
reset,
expectContinue,
servername,
throwOnError
throwOnError,
maxRedirections
}, handler) {
if (typeof path !== 'string') {
throw new InvalidArgumentError('path must be a string')
Expand Down Expand Up @@ -86,6 +87,10 @@ class Request {
throw new InvalidArgumentError('invalid throwOnError')
}

if (maxRedirections != null && maxRedirections !== 0) {
throw new InvalidArgumentError('maxRedirections is not supported, use the redirect interceptor')
}

this.headersTimeout = headersTimeout

this.bodyTimeout = bodyTimeout
Expand Down
4 changes: 2 additions & 2 deletions lib/handler/redirect-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ class RedirectHandler {

this.dispatch = dispatch
this.location = null
this.opts = { ...opts, maxRedirections: 0 } // opts must be a copy
const { maxRedirections: _, ...cleanOpts } = opts
this.opts = cleanOpts // opts must be a copy, exclude maxRedirections
this.maxRedirections = maxRedirections
this.handler = handler
this.history = []
Expand Down Expand Up @@ -138,7 +139,6 @@ class RedirectHandler {
this.opts.headers = cleanRequestHeaders(this.opts.headers, statusCode === 303, this.opts.origin !== origin)
this.opts.path = path
this.opts.origin = origin
this.opts.maxRedirections = 0
this.opts.query = null
}

Expand Down
2 changes: 1 addition & 1 deletion lib/interceptor/redirect.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function createRedirectInterceptor ({ maxRedirections: defaultMaxRedirections }
return dispatch(opts, handler)
}

const dispatchOpts = { ...rest, maxRedirections: 0 } // Stop sub dispatcher from also redirecting.
const dispatchOpts = { ...rest } // Stop sub dispatcher from also redirecting.
const redirectHandler = new RedirectHandler(dispatch, maxRedirections, dispatchOpts, handler)
return dispatch(dispatchOpts, redirectHandler)
}
Expand Down
27 changes: 9 additions & 18 deletions test/client-request.js
Original file line number Diff line number Diff line change
Expand Up @@ -734,8 +734,7 @@ test('request post body no missing data', async (t) => {
this.push('asd')
this.push(null)
}
}),
maxRedirections: 2
})
})
await body.text()
t.ok(true, 'pass')
Expand Down Expand Up @@ -773,8 +772,7 @@ test('request post body no extra data handler', async (t) => {
const { body } = await client.request({
path: '/',
method: 'GET',
body: reqBody,
maxRedirections: 0
body: reqBody
})
await body.text()
t.ok(true, 'pass')
Expand Down Expand Up @@ -1051,8 +1049,7 @@ test('request post body Buffer from string', async (t) => {
const { body } = await client.request({
path: '/',
method: 'POST',
body: requestBody,
maxRedirections: 2
body: requestBody
})
await body.text()
t.ok(true, 'pass')
Expand Down Expand Up @@ -1083,8 +1080,7 @@ test('request post body Buffer from buffer', async (t) => {
const { body } = await client.request({
path: '/',
method: 'POST',
body: requestBody,
maxRedirections: 2
body: requestBody
})
await body.text()
t.ok(true, 'pass')
Expand Down Expand Up @@ -1115,8 +1111,7 @@ test('request post body Uint8Array', async (t) => {
const { body } = await client.request({
path: '/',
method: 'POST',
body: requestBody,
maxRedirections: 2
body: requestBody
})
await body.text()
t.ok(true, 'pass')
Expand Down Expand Up @@ -1147,8 +1142,7 @@ test('request post body Uint32Array', async (t) => {
const { body } = await client.request({
path: '/',
method: 'POST',
body: requestBody,
maxRedirections: 2
body: requestBody
})
await body.text()
t.ok(true, 'pass')
Expand Down Expand Up @@ -1179,8 +1173,7 @@ test('request post body Float64Array', async (t) => {
const { body } = await client.request({
path: '/',
method: 'POST',
body: requestBody,
maxRedirections: 2
body: requestBody
})
await body.text()
t.ok(true, 'pass')
Expand Down Expand Up @@ -1211,8 +1204,7 @@ test('request post body BigUint64Array', async (t) => {
const { body } = await client.request({
path: '/',
method: 'POST',
body: requestBody,
maxRedirections: 2
body: requestBody
})
await body.text()
t.ok(true, 'pass')
Expand Down Expand Up @@ -1243,8 +1235,7 @@ test('request post body DataView', async (t) => {
const { body } = await client.request({
path: '/',
method: 'POST',
body: requestBody,
maxRedirections: 2
body: requestBody
})
await body.text()
t.ok(true, 'pass')
Expand Down
3 changes: 1 addition & 2 deletions test/interceptors/redirect-issue-3803.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,7 @@ test('redirecting works with a FormData body', async (t) => {
const { context } = await request(`http://localhost:${server.address().port}/1`, {
body,
method: 'POST',
dispatcher: agent,
maxRedirections: 1
dispatcher: agent
})

plan.deepStrictEqual(context.history, [
Expand Down
9 changes: 3 additions & 6 deletions test/redirect-stream.js
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,7 @@ describe('when a Location response header is NOT present', async () => {

await stream(
`http://${server}/${code}`,
{ opaque: body, maxRedirections: 10 },
{ opaque: body },
({ statusCode, headers, opaque }) => {
t.strictEqual(statusCode, code)
t.strictEqual(headers.location, undefined)
Expand All @@ -357,8 +357,7 @@ test('should not follow redirects when using Readable request bodies', async t =
{
method: 'POST',
body: createReadable('REQUEST'),
opaque: body,
maxRedirections: 10
opaque: body
},
({ statusCode, headers, opaque }) => {
t.strictEqual(statusCode, 302)
Expand All @@ -377,7 +376,7 @@ test('should handle errors', async t => {
const body = []

try {
await stream('http://localhost:0', { opaque: body, maxRedirections: 10 }, ({ statusCode, headers, opaque }) => {
await stream('http://localhost:0', { opaque: body }, ({ statusCode, headers, opaque }) => {
return createWritable(opaque)
})

Expand All @@ -395,7 +394,6 @@ test('removes authorization header on third party origin', async t => {

const [server1] = await startRedirectingWithAuthorization('secret')
await stream(`http://${server1}`, {
maxRedirections: 10,
opaque: body,
headers: {
authorization: 'secret'
Expand All @@ -412,7 +410,6 @@ test('removes cookie header on third party origin', async t => {

const [server1] = await startRedirectingWithCookie('a=b')
await stream(`http://${server1}`, {
maxRedirections: 10,
opaque: body,
headers: {
cookie: 'a=b'
Expand Down
40 changes: 40 additions & 0 deletions test/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,46 @@ describe('DispatchOptions#expectContinue', () => {
})
})

describe('DispatchOptions#maxRedirections', () => {
test('Should throw if maxRedirections option is used', async t => {
t = tspl(t, { plan: 2 })

await t.rejects(request({
method: 'GET',
origin: 'http://somehost.xyz',
maxRedirections: 5
}), /maxRedirections is not supported, use the redirect interceptor/)

await t.rejects(request({
method: 'GET',
origin: 'http://somehost.xyz',
maxRedirections: 1
}), /maxRedirections is not supported, use the redirect interceptor/)

await t.completed
})

test('Should allow maxRedirections: 0 for internal use', async t => {
t = tspl(t, { plan: 2 })
const server = createServer((req, res) => {
t.ok('request received')
res.end('hello world')
})
after(() => server.close())
await new Promise((resolve) => server.listen(0, resolve))

const res = await request({
method: 'GET',
origin: `http://localhost:${server.address().port}`,
maxRedirections: 0
})

const body = await res.body.text()

t.strictEqual(body, 'hello world')
})
})

describe('DispatchOptions#reset', () => {
test('Should throw if invalid reset option', async t => {
t = tspl(t, { plan: 1 })
Expand Down
Loading