Skip to content

Read request bodies from httpBodyStream in URL protocols - #32

Merged
mattt merged 4 commits into
mattt:mainfrom
kikeenrique:fix/urlprotocol-body-stream
Nov 18, 2026
Merged

mattt merged 4 commits into
mattt:mainfrom
kikeenrique:fix/urlprotocol-body-stream

Conversation

@kikeenrique

Copy link
Copy Markdown
Contributor

On Apple platforms, URLSession moves a request's body from httpBody into httpBodyStream before handing it to a URLProtocol. Replay reads only httpBody, so through Replay.session POSTs are recorded without postData, and the .body matcher never matches.

PlaybackURLProtocol and CaptureURLProtocol now read the stream back into httpBody before matching, recording or forwarding the request. They do this through a small internal helper, URLRequest.withBodyReadFromStream(). If reading the stream fails, the request is used unchanged. FoundationNetworking keeps httpBody, so nothing changes on Linux.

Tests

  • New test bodyMatcherSeesStreamedBody (Apple platforms only): two recorded POSTs to the same URL, told apart by .body. It fails without the change ("No Matching Entry") and passes with it.
  • Full suite passes on macOS (Swift 6.4.2) and on Linux (swift:6.4.2-noble).
  • swift format lint --strict is clean.
  • The CaptureURLProtocol change has no automated test, because capture goes to the real network.

Fixes #31

kikeenrique and others added 3 commits November 18, 2026 16:55
On Darwin, URLSession hands a URLProtocol the request with httpBody set
to nil and the body in httpBodyStream. Replay reads only httpBody, so
through Replay.session a POST is recorded without postData and the
.body matcher never matches; several POSTs to one URL with different
bodies all play back the first entry.

PlaybackURLProtocol and CaptureURLProtocol now read the stream back
into httpBody before matching, recording, or forwarding the request.
FoundationNetworking keeps httpBody, so Linux was not affected.
@mattt

mattt commented Nov 18, 2026

Copy link
Copy Markdown
Owner

Thanks for reporting this and for the fix, @kikeenrique!

I pushed two commits on top of yours. If reading the stream fails partway, or the stream was already opened, the request now fails with URLError(.requestBodyStreamExhausted) instead of going out with a truncated or empty body. The new tests cover those cases for playback.

One small correction to the description: a request built with an explicit httpBodyStream on Linux takes the same path, so it's not only Darwin.

#33 fixes the Linux CI jobs that are failing here. Once that's in, I'll merge this.

@mattt
mattt merged commit 0dafffc into mattt:main Nov 18, 2026
16 checks passed
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.

POST bodies are lost through Replay.session on Apple platforms (no postData, .body never matches)

2 participants