Skip to content

Incorrect multipart/form-data boundary in header/body with MockAgent #5241

@wbyoung

Description

@wbyoung

Bug Description

The boundary specified in a request made when a MockAgent is in use is not the same value that's used within the request body.

Reproducible By

Run nc -l 9999 and while it's listening, run node issue.ts or USE_MOCK_AGENT=1 node issue.ts.

issue.ts
import {
  install as installUndici,
  MockAgent,
  setGlobalDispatcher,
} from 'undici'

installUndici()

if (process.env.USE_MOCK_AGENT) {
  const agent = new MockAgent()
  agent.enableNetConnect('localhost:9999')
  setGlobalDispatcher(agent)
}

const body = new FormData()
const buffer = Buffer.from('hello world', 'utf8')
const blob = new Blob([buffer], { type: 'txt' })
body.append('file', blob, 'document.txt')

const res = await fetch('http://localhost:9999', {
  body,
  method: 'POST',
})
console.log(await res.json())

Request sent without using the mock agent:

$ nc -l 9999
POST / HTTP/1.1
host: localhost:9999
connection: keep-alive
content-type: multipart/form-data; boundary=----formdata-undici-033603461557
accept: */*
accept-language: *
sec-fetch-mode: cors
user-agent: undici
accept-encoding: gzip, deflate
content-length: 178

------formdata-undici-033603461557
Content-Disposition: form-data; name="file"; filename="document.txt"
Content-Type: txt

hello world
------formdata-undici-033603461557--

Request sent using the mock agent:

$ nc -l 9999
POST / HTTP/1.1
host: localhost:9999
connection: keep-alive
content-type: multipart/form-data; boundary=----formdata-undici-012602790914
accept: */*
accept-language: *
sec-fetch-mode: cors
user-agent: undici
accept-encoding: gzip, deflate
content-length: 178

------formdata-undici-099234737323
Content-Disposition: form-data; name="file"; filename="document.txt"
Content-Type: txt

hello world
------formdata-undici-099234737323--

Expected Behavior

The value in the Content-Type header includes a boundary that's used within the request body even when the MockAgent is used.

Environment

$ node --version
v22.22.0

undici@8.2.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions