Fix streamed response bodies being consumed when recording - #11
Merged
craigpotter merged 1 commit intoJul 14, 2026
Merged
Conversation
When a response is backed by a non-seekable stream (e.g. Guzzle's `stream => true` config), reading the body while recording drains the stream to EOF and it cannot be rewound. The application then receives an empty body from `$response->body()` or `$response->stream()`. Barstool now records `<Streamed Body>` for non-seekable response bodies without touching the stream, matching how streamed request bodies are already handled. The content-type check also now runs before the body is read, so unsupported bodies are no longer buffered just to be discarded.
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.
The problem
When a Saloon request is sent with Guzzle's
stream => trueconfig, the response body is backed by a non-seekable socket stream that can only be read once.Barstool's
onResponsemiddleware calls$response->body()to record the body. Saloon'sResponse::body()/Response::stream()only rewind seekable streams, so for a streamed response the recording drains the stream to EOF and it can never be rewound. By the time the application reads$response->body()or$response->stream(), the body is empty.In practice this silently corrupts downstream data — e.g. writing
$response->stream()to a filesystem produces a blank file whenever Barstool is enabled.The fix
getResponseBody()now checksisSeekable()on the PSR stream before reading anything (the check itself doesn't consume the stream). Non-seekable bodies are recorded as<Streamed Body>, matching how streamed request bodies are already handled viaStreamBodyRepository.While in there, the content-type check now runs before the body is read, so unsupported bodies (large binaries etc.) are no longer buffered into memory just to be discarded as
<Unsupported Barstool Response Content>.Testing
Added a regression test that records a response backed by a
NoSeekStreamand asserts:<Streamed Body>alongside the usual status/headers metadataThe new test fails on
main(the stream comes back empty) and passes with this change.