Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix redirect interceptor with FormData body #3815

Merged
merged 1 commit into from
Nov 8, 2024
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
3 changes: 2 additions & 1 deletion lib/handler/redirect-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ class RedirectHandler {
this.opts.body &&
typeof this.opts.body !== 'string' &&
!ArrayBuffer.isView(this.opts.body) &&
util.isIterable(this.opts.body)
util.isIterable(this.opts.body) &&
!util.isFormDataLike(this.opts.body)
) {
// TODO: Should we allow re-using iterable if !this.opts.idempotent
// or through some other flag?
Expand Down
40 changes: 40 additions & 0 deletions test/interceptors/redirect-issue-3803.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
'use strict'

const { FormData, request, Agent, interceptors } = require('../..')
const { test } = require('node:test')
const { createServer } = require('node:http')
const { once } = require('node:events')
const { tspl } = require('@matteo.collina/tspl')

test('redirecting works with a FormData body', async (t) => {
const plan = tspl(t, { plan: 1 })

const server = createServer((req, res) => {
if (req.url === '/1') {
res.writeHead(302, undefined, { location: '/2' })
res.end()
} else {
res.end('OK')
}
}).listen(0)

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

const agent = new Agent().compose(interceptors.redirect({ maxRedirections: 1 }))

const body = new FormData()
body.append('hello', 'world')

const { context } = await request(`http://localhost:${server.address().port}/1`, {
body,
method: 'POST',
dispatcher: agent,
maxRedirections: 1
})

plan.deepStrictEqual(context.history, [
new URL(`http://localhost:${server.address().port}/1`),
new URL(`http://localhost:${server.address().port}/2`)
])
})
5 changes: 0 additions & 5 deletions test/types/interceptor.test-d.ts

This file was deleted.

1 change: 0 additions & 1 deletion types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,6 @@ declare namespace Undici {
const RedirectHandler: typeof import ('./handlers').RedirectHandler
const DecoratorHandler: typeof import ('./handlers').DecoratorHandler
const RetryHandler: typeof import ('./retry-handler').default
const createRedirectInterceptor: typeof import ('./interceptors').default.createRedirectInterceptor
const BalancedPool: typeof import('./balanced-pool').default
const Client: typeof import('./client').default
const buildConnector: typeof import('./connector').default
Expand Down
1 change: 0 additions & 1 deletion types/interceptors.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ declare namespace Interceptors {
affinity?: 4 | 6
}

export function createRedirectInterceptor (opts: RedirectInterceptorOpts): Dispatcher.DispatcherComposeInterceptor
export function dump (opts?: DumpInterceptorOpts): Dispatcher.DispatcherComposeInterceptor
export function retry (opts?: RetryInterceptorOpts): Dispatcher.DispatcherComposeInterceptor
export function redirect (opts?: RedirectInterceptorOpts): Dispatcher.DispatcherComposeInterceptor
Expand Down
Loading