Skip to content
Open
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
2 changes: 2 additions & 0 deletions lib/core/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ function wrapRequestBody (body) {
// to determine whether or not it has been disturbed. This is just
// a workaround.
return new BodyAsyncIterable(body)
} else if (body && isFormDataLike(body)) {
return body
} else if (
body &&
typeof body !== 'string' &&
Expand Down
71 changes: 71 additions & 0 deletions test/issue-4691.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
'use strict'

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

const { RetryAgent, Client, FormData } = require('..')
// https://github.com/nodejs/undici/issues/4691
test('Should retry status code with FormData body', async t => {
t = tspl(t, { plan: 2 })

let counter = 0
const server = createServer({ joinDuplicateHeaders: true })
const opts = {
maxRetries: 5,
timeout: 1,
timeoutFactor: 1
}

server.on('request', (req, res) => {
switch (counter++) {
case 0:
req.destroy()
return
case 1:
res.writeHead(500)
res.end('failed')
return
case 2:
res.writeHead(200)
res.end('hello world!')
return
default:
t.fail()
}
})

server.listen(0, () => {
const client = new Client(`http://localhost:${server.address().port}`)
const agent = new RetryAgent(client, opts)

after(async () => {
await agent.close()
server.close()

await once(server, 'close')
})

const formData = new FormData()
formData.append('field', 'test string')
agent.request({
method: 'GET',
path: '/',
headers: {
'content-type': 'application/json'
},
body: formData
}).then((res) => {
t.equal(res.statusCode, 200)
res.body.setEncoding('utf8')
let chunks = ''
res.body.on('data', chunk => { chunks += chunk })
res.body.on('end', () => {
t.equal(chunks, 'hello world!')
})
})
})

await t.completed
})