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
Extend httpResponseMessage.Should().HaveStatusCode() to dump request body on failure #1811
Comments
/cc @mu88 |
Thank you for your input, @bart-degreed ! I understand your preference for providing the request body as well. From the perspective of a multi-purpose library like Fluent Assertions, I would ask: why the request body and not all the headers as well? So it is more a product decision :) Let's bring @dennisdoomen and @jnyrup into the discussion. |
I think it's a great idea. We would welcome a PR for that. |
While the output looks good, introducing a blocking call would go against our statement of "fully embrace asynchronous code".
https://www.continuousimprover.com/2021/08/fluent-assertions-v6.html |
Regarding the output format, I'd prefer if the actual and expected status code are close together not separated by dozens of lines in case the request body is large. That somehow breaks the beauty and simplicity of Fluent Assertions. |
Ah yes, I didn't even consider that. |
Are there other pieces of information in the |
There are headers and cookies - but I think it really depends on the situation what you need |
Maybe we can use |
Information that might be useful is the full request URL including query string, request/response body, incoming and outgoing cookies, headers, and the response status code. When writing production logs, I'd include all that information, at the cost of information overload, because if I don't, it becomes difficult or even impossible to obtain that information. In test assertions, however, I'd like to quickly see just the essential information instead. The response body typically explains why something failed. If you want to go fancy, I'd suggest adding an overload that takes a I wouldn't mind if this were an async method. And like I said, the message template can be improved, so it would be nice to put the actual/expected status codes close to each other and produce a sentence that's proper English. |
Another enhancement could be to check the Content-Type header first, and if it's binary, then dump the body in hex. Another enhancement could be to truncate the body if it exceeds some threshold. |
The HTTP version is missing from my list of possibly useful information above, as well as the sender IP, but these are unlikely to be relevant in tests. |
If you like, feel free to copy https://github.com/bkoelman/DogAgilityCompetitionManagement/blob/5847fc641b9e81fb732f87294baa108024c2dcd2/src/Circe/Protocol/ByteArrayExtensions.cs. It renders binary data in hex format, something like this: |
It would be nice to move this into a sub-namespace in the next major release. Currently it's cumbersome to use my own extension method on |
Or even a separate package, e.g. |
A separate package already exists at https://github.com/balanikas/FluentAssertions.Http. Makes me wonder why this was integrated. |
We also conflicted with the much more maintained https://www.nuget.org/packages/FluentAssertions.Web We did discussed some concerns in #1737, but mostly about the dependency on It seems that creating custom extensions for I would be fine splitting this off to a separate library for vNext. |
Yep, something we can fix in v7. |
So a different NuGet package (e. g. |
That remains to be seen. We're postponing v7 as long as possible. |
I just stumbled on this too and was somewhat disappointed that the default assertion message does not include the HTTP body. Like Bart described, displaying the body (which contains an error message) would have saved me a trip in the debugger. I'll be using FluentAssertions.Web in the meantime which has excellent output messages on failure. |
Is this still in scope for v7? What matters most to me is that the existing implementation gets moved into a separate namespace, to resolve the conflict with our own implementation. |
I haven't looked at this for a while and can't promise anything for v7. I guess the first discussion that we need to have is what path to take. The most logical path is remove direct support entirely and steer existing users to https://github.com/adrianiftode/FluentAssertions.Web |
After updating to the latest FA, I found that our custom extension method conflicts with the one added in v6.4.0 here.
The difference is that ours dumps the request body on HTTP status code mismatch, which is helpful in analyzing what's going on without debugging the test.
For an assertion like:
when that returns 404, we get the following test output:
I know the message is a bit messy, but it contains all the info we need: the actual status code, the expected status code, and the returned response body.
Our extension method code is the following, feel free to reuse parts as it makes sense.
The text was updated successfully, but these errors were encountered: