Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose ReadBody on ScenarioAssertionException #133

Merged
merged 1 commit into from
Jan 11, 2023

Conversation

Hawxy
Copy link
Collaborator

@Hawxy Hawxy commented Jan 9, 2023

Closes #132

/// <param name="context">The context of the response</param>
/// <returns>A string with the content of the body</returns>
[MemberNotNull(nameof(Body))]
public string ReadBody(HttpContext context)
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's kind of annoying that I have to call ReadBody to assign the Body property and its not very discoverable to end users that this is required and that there is a relationship between the two.

The ReadBody method itself is useful since it will rewind the stream back to the beginning, which could affect other assertions helpers. Perhaps ReadBody should actually be an extension method on HttpContext instead and have a similar to the helper methods in System.Net.Http.Json, e.g. ReadResponseBodyAsText.

Copy link
Collaborator Author

@Hawxy Hawxy Jan 9, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The intention of ScenarioAssertionException's Body property is for it to only be set for assertions that involve the body, as it showing up in error output for non-body assertions isn't particularly useful. The function call hits two birds with one stone, if the user accesses the body in the assertion it also sets it to appear in the error output.

I think we could improve discoverability/ergonomics by wrapping the HttpContext & exception inside of a AssertionContext, but that'd need to wait until the next major release (likely around .NET 8). Near-term we can look into exposing some of the internal helpers as extensions on the HttpContext to at least make assertion writing a tad easier.

@jeremydmiller
Copy link
Member

@Hawxy @egil This would have to wait for V8 like you already said, but I think I'd vote to eliminate the Body property and have folks rely on GetBody() -- or rename that as ReadBodyAsString() or something. Need to tip off folks that it requires some work to get there. And it's been awkward over the years for folks that try to read the body multiple times in the same test.

I think I also like the idea of any assertions in Scenario that assert on the contents of the body that they do their thing, but then we rewind the response stream so as not to trip up users doing secondary reads of that content.

That's all from thinking about this for just a minute or two though.

@Hawxy Hawxy merged commit fddc2ec into JasperFx:master Jan 11, 2023
@Hawxy Hawxy deleted the ReadBodyExpose branch January 11, 2023 13:25
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.

Does this look correct: a ContentShouldBeJson assertion helper
3 participants