Skip to content

Add ReadAsBytes to IScenarioResult - #249

Open
kgkoutis wants to merge 1 commit into
JasperFx:masterfrom
kgkoutis:feat/read-as-bytes
Open

Add ReadAsBytes to IScenarioResult#249
kgkoutis wants to merge 1 commit into
JasperFx:masterfrom
kgkoutis:feat/read-as-bytes

Conversation

@kgkoutis

Copy link
Copy Markdown

Closes #248

IScenarioResult has 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 is public, but it lives on the concrete class rather than the interface Scenario(...) returns, so callers have to downcast to reach it.

Change

byte[] ReadAsBytes();
Task<byte[]> ReadAsBytesAsync();

on IScenarioResult, implemented on ScenarioResult through the existing Read/ReadAsync helpers. 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 call ReadAsJson<T>() afterwards — covered by one of the tests.

This adds members to a public interface and so breaks external implementors. It follows the ReadAsServerSentEvents precedent 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 existing Bug_92_repeated_reads_of_the_response cases:

  • read_the_response_as_bytes
  • read_the_response_as_bytes_async
  • reading_as_bytes_does_not_consume_the_response

Full Alba.Testing suite passes locally on net10.0: 666 tests, 0 failures.

Docs

IScenarioResult is inside the sample_IScenarioResult region, so the interface listing on the docs site picks the new members up when the snippets tool runs. A v9_CHANGELOG.md entry 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.

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.
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.

No way to read a binary response body: add ReadAsBytes to IScenarioResult

1 participant