Skip to content

fix(mock): emit request body lifecycle hooks#5367

Open
marko1olo wants to merge 4 commits into
nodejs:mainfrom
marko1olo:fix-mockagent-request-hooks
Open

fix(mock): emit request body lifecycle hooks#5367
marko1olo wants to merge 4 commits into
nodejs:mainfrom
marko1olo:fix-mockagent-request-hooks

Conversation

@marko1olo
Copy link
Copy Markdown

@marko1olo marko1olo commented Jun 6, 2026

This relates to...

Fixes #3843.

Rationale

MockAgent dispatches currently skip request body lifecycle callbacks, so code that works with real dispatchers cannot be tested accurately with mocks. The mock dispatcher should emit onBodySent(chunk) for request body chunks and onRequestSent() once request body dispatch has completed, while preserving the existing mocked reply behavior.

Changes

Features

N/A

Bug Fixes

  • emit onBodySent(chunk) and onRequestSent() from MockAgent dispatches before resolving the mocked reply
  • support string/buffer-like bodies, sync iterables, async iterables, and requests without a body
  • preserve async iterable request bodies for functional mock reply callbacks and reply-options callbacks by replaying consumed chunks
  • keep request body matching behavior intact when lifecycle hooks are present
  • route body lifecycle hook errors and async iterable producer errors through onResponseError
  • stop body dispatch and response delivery after request-start, request-sent, or body-hook aborts, including delayed replies

Breaking Changes and Deprecations

N/A

Validation

  • node --test --test-timeout=30000 --test-name-pattern "request body lifecycle hooks|request sent hook|async iterable request body errors|replayed async iterable bodies|match request bodies when lifecycle hooks|replay async iterable request bodies|request body lifecycle hook errors|delayed replies after request sent aborts|stop reading async iterable request bodies|request start aborts" test/mock-agent.js
  • node --test --test-timeout=30000 --test-name-pattern "dispatch onBodySent" test/node-test/client-dispatch.js
  • npm run lint -- --no-cache lib/mock/mock-utils.js test/mock-agent.js
  • git diff --check origin/main..HEAD

Note: npx borp --timeout 180000 --expose-gc -p "test/mock-agent.js" did not complete locally before the wrapper timeout in this Windows environment, so I used focused Node test runs for the changed behavior and adjacent dispatcher behavior.

AI-assisted contribution: implemented with OpenAI Codex and reviewed locally before submission.

Status

marko1olo and others added 4 commits June 6, 2026 07:52
Mock dispatch now forwards request bodies through onBodySent/onRequestSent before resolving the mocked reply, including async iterable bodies.

Co-authored-by: OpenAI Codex <codex@openai.com>
Signed-off-by: marko1olo <barsukdana@gmail.com>
Do not continue request body lifecycle processing or resolve a mock response after the mock request controller has been aborted.

Co-authored-by: OpenAI Codex <codex@openai.com>
Signed-off-by: marko1olo <barsukdana@gmail.com>
Signed-off-by: marko1olo <barsukdana@gmail.com>
Signed-off-by: marko1olo <barsukdana@gmail.com>
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.

onBodySent is not called in mockDispatch

1 participant