Skip to content

Commit

Permalink
fix redirect interceptor with FormData body (#3815)
Browse files Browse the repository at this point in the history
fix redirect interceptor with FormData body
  • Loading branch information
KhafraDev authored Nov 8, 2024
1 parent a2f63e7 commit c94fda1
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
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

0 comments on commit c94fda1

Please sign in to comment.