Add ReadAsBytes to IScenarioResult - #249
Open
kgkoutis wants to merge 1 commit into
Open
Conversation
There is no way to get a binary response body out of a scenario today. ReadAsText decodes as text, which corrupts anything that is not text, and ScenarioResult.Read, which does the buffering and rewinding, is not on IScenarioResult, so callers have to cast to the concrete type to reach it. ReadAsBytes and ReadAsBytesAsync go through the same Read/ReadAsync helpers as the other readers, so the body is left readable and a scenario can assert on the bytes and still call ReadAsJson<T>() afterwards. Follows the ReadAsServerSentEvents precedent from the v9 beta of adding readers straight to the interface, and carries a v9_CHANGELOG entry under New features.
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.
Closes #248
IScenarioResulthas no binary accessor. Every reader on it decodes the body as text, so an endpoint returning a file, image or PDF cannot be asserted on.ScenarioResult.Read<T>(Func<Stream, T>)does the right thing and ispublic, but it lives on the concrete class rather than the interfaceScenario(...)returns, so callers have to downcast to reach it.Change
on
IScenarioResult, implemented onScenarioResultthrough the existingRead/ReadAsynchelpers. That means they inherit the same behaviour as the other readers: the body is rewound before and after, so a scenario can assert on the bytes and still callReadAsJson<T>()afterwards — covered by one of the tests.This adds members to a public interface and so breaks external implementors. It follows the
ReadAsServerSentEventsprecedent from the v9 beta (#239) of adding readers straight to the interface rather than via a default implementation. Happy to switch to a default implementation instead if you would rather not spend the break — say the word.Tests
Three acceptance tests in
specs_against_aspnet_core_app, alongside the existingBug_92_repeated_reads_of_the_responsecases:read_the_response_as_bytesread_the_response_as_bytes_asyncreading_as_bytes_does_not_consume_the_responseFull
Alba.Testingsuite passes locally on net10.0: 666 tests, 0 failures.Docs
IScenarioResultis inside thesample_IScenarioResultregion, so the interface listing on the docs site picks the new members up when the snippets tool runs. Av9_CHANGELOG.mdentry is included under New features.Motivation
Found while adding integration tests for two endpoints that stream an
.xlsx. The test now parses the response with ClosedXML and asserts a sheet name, so a truncated or corrupt stream fails rather than passing on the content type alone — that needed the bytes.