Skip to content

Commit c94fda1

Browse files
authored
fix redirect interceptor with FormData body (#3815)
fix redirect interceptor with FormData body
1 parent a2f63e7 commit c94fda1

File tree

5 files changed

+42
-8
lines changed

5 files changed

+42
-8
lines changed

lib/handler/redirect-handler.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ class RedirectHandler {
7575
this.opts.body &&
7676
typeof this.opts.body !== 'string' &&
7777
!ArrayBuffer.isView(this.opts.body) &&
78-
util.isIterable(this.opts.body)
78+
util.isIterable(this.opts.body) &&
79+
!util.isFormDataLike(this.opts.body)
7980
) {
8081
// TODO: Should we allow re-using iterable if !this.opts.idempotent
8182
// or through some other flag?
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
'use strict'
2+
3+
const { FormData, request, Agent, interceptors } = require('../..')
4+
const { test } = require('node:test')
5+
const { createServer } = require('node:http')
6+
const { once } = require('node:events')
7+
const { tspl } = require('@matteo.collina/tspl')
8+
9+
test('redirecting works with a FormData body', async (t) => {
10+
const plan = tspl(t, { plan: 1 })
11+
12+
const server = createServer((req, res) => {
13+
if (req.url === '/1') {
14+
res.writeHead(302, undefined, { location: '/2' })
15+
res.end()
16+
} else {
17+
res.end('OK')
18+
}
19+
}).listen(0)
20+
21+
t.after(() => server.close())
22+
await once(server, 'listening')
23+
24+
const agent = new Agent().compose(interceptors.redirect({ maxRedirections: 1 }))
25+
26+
const body = new FormData()
27+
body.append('hello', 'world')
28+
29+
const { context } = await request(`http://localhost:${server.address().port}/1`, {
30+
body,
31+
method: 'POST',
32+
dispatcher: agent,
33+
maxRedirections: 1
34+
})
35+
36+
plan.deepStrictEqual(context.history, [
37+
new URL(`http://localhost:${server.address().port}/1`),
38+
new URL(`http://localhost:${server.address().port}/2`)
39+
])
40+
})

test/types/interceptor.test-d.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

types/index.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,6 @@ declare namespace Undici {
4040
const RedirectHandler: typeof import ('./handlers').RedirectHandler
4141
const DecoratorHandler: typeof import ('./handlers').DecoratorHandler
4242
const RetryHandler: typeof import ('./retry-handler').default
43-
const createRedirectInterceptor: typeof import ('./interceptors').default.createRedirectInterceptor
4443
const BalancedPool: typeof import('./balanced-pool').default
4544
const Client: typeof import('./client').default
4645
const buildConnector: typeof import('./connector').default

types/interceptors.d.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@ declare namespace Interceptors {
2525
affinity?: 4 | 6
2626
}
2727

28-
export function createRedirectInterceptor (opts: RedirectInterceptorOpts): Dispatcher.DispatcherComposeInterceptor
2928
export function dump (opts?: DumpInterceptorOpts): Dispatcher.DispatcherComposeInterceptor
3029
export function retry (opts?: RetryInterceptorOpts): Dispatcher.DispatcherComposeInterceptor
3130
export function redirect (opts?: RedirectInterceptorOpts): Dispatcher.DispatcherComposeInterceptor

0 commit comments

Comments
 (0)