Skip to content

fix(mock): preserve fetch FormData boundary on net-connect fallback#5262

Open
SAY-5 wants to merge 2 commits intonodejs:mainfrom
SAY-5:fix/mock-formdata-boundary-5241
Open

fix(mock): preserve fetch FormData boundary on net-connect fallback#5262
SAY-5 wants to merge 2 commits intonodejs:mainfrom
SAY-5:fix/mock-formdata-boundary-5241

Conversation

@SAY-5
Copy link
Copy Markdown
Contributor

@SAY-5 SAY-5 commented May 7, 2026

This relates to...

Fixes #5241

Rationale

When fetch sends a FormData body through a MockAgent that falls through to net connect, mock body matching wants the original request source, but the real dispatch path must keep fetch's already-encoded body so the multipart boundary stays aligned with the content-type header.

Changes

  • Keep the matcher-friendly request.body.source || request.body.stream path for MockAgent requests.
  • Carry fetch's pre-encoded request body through an internal mock-only fallback slot.
  • Restore that prepared body only when a FormData request misses the mock intercept and passes through to the original dispatcher.

Features

N/A

Bug Fixes

  • fetch + MockAgent + FormData no longer produces a content-type/body boundary mismatch on net-connect pass-through.

Breaking Changes and Deprecations

N/A

Status

When MockAgent is active, fetch sends request.body.source so body matchers
see the original input. For FormData sources, the dispatcher then re-extracts
the body via extractBody() and generates a fresh multipart boundary, while
the content-type header retains the boundary fetch produced earlier. Servers
that consult content-type and the body's leading boundary line see a
mismatch.

Skip the source override for FormData and dispatch the prepared async
iterator instead. Source matching against FormData was not usable through
matchValue anyway.

Fixes nodejs#5241

Signed-off-by: SAY-5 <saiasish.cnp@gmail.com>
Signed-off-by: Sai Asish Y <saiasish.cnp@gmail.com>
Comment thread test/mock-agent.js
req.on('data', (c) => chunks.push(c))
req.on('end', () => {
const body = Buffer.concat(chunks).toString('utf8')
const headerMatch = /boundary=(.+)$/.exec(req.headers['content-type'] || '')
@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented May 7, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 93.28%. Comparing base (01122da) to head (1f565ed).
⚠️ Report is 10 commits behind head on main.

Additional details and impacted files
@@           Coverage Diff           @@
##             main    #5262   +/-   ##
=======================================
  Coverage   93.27%   93.28%           
=======================================
  Files         110      110           
  Lines       36366    36391   +25     
=======================================
+ Hits        33922    33946   +24     
- Misses       2444     2445    +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

Comment thread lib/web/fetch/index.js Outdated
@SAY-5
Copy link
Copy Markdown
Contributor Author

SAY-5 commented May 7, 2026

FYI the only failing checks are pre-existing flakes unrelated to this PR's MockAgent FormData change:

  • Test with Node.js 24 on windows-latest: fails on test/issue-2590.js (AbortSignal.timeout(0)), unrelated; this test has been intermittently flaky on Windows
  • CodeQL: tooling failure (no actionable findings)

main branch has had similar failures during this window. Happy to rebase if a maintainer would like a re-run.

@SAY-5 SAY-5 changed the title fix(fetch): preserve FormData boundary when dispatching to MockAgent fix(mock): preserve fetch FormData boundary on net-connect fallback May 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

4 participants