Repository navigation
Read request bodies from httpBodyStream in URL protocols - #32
Merged
Merged
Conversation
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.
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 One small correction to the description: a request built with an explicit #33 fixes the Linux CI jobs that are failing here. Once that's in, I'll merge this. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
On Apple platforms, URLSession moves a request's body from
httpBodyintohttpBodyStreambefore handing it to aURLProtocol. Replay reads onlyhttpBody, so throughReplay.sessionPOSTs are recorded withoutpostData, and the.bodymatcher never matches.PlaybackURLProtocolandCaptureURLProtocolnow read the stream back intohttpBodybefore 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 keepshttpBody, so nothing changes on Linux.Tests
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.swift:6.4.2-noble).swift format lint --strictis clean.CaptureURLProtocolchange has no automated test, because capture goes to the real network.Fixes #31