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

Refactor the assertion mechanism (was: Add description of the requests that are made) #204

Open
dnperfors opened this issue Dec 9, 2022 · 0 comments
Labels
enhancement New feature or request

Comments

@dnperfors
Copy link
Member

dnperfors commented Dec 9, 2022

When an assertion fails, it is not always clear which request are actually made. This requires that you debug a test instead of reading the message.
Besides that it is a kind of mismatch of methods that you need to use and most of them have overloads for the number of requests that are made with the specific conditions.

All in all, it seems better to improve the whole assertion part of TestableHttpClient and make things easier.

Take the following example:

testableHttpClient.ShouldHaveReceivedRequestsFor("https://httpbin.org/post", 1).WithHttpMethod(POST, 1);

This is very verbose and can be shortened to:

testableHttpClient.Received(1).Post("https://httpbin.org/post");

Then there could be overloads for testing the content as well:

testableHttpClient.Recieved(1).Post("https://httpbin.org/post", "*hello*");

Testing for headers happens in the same way as before:

testableHttpClient.Received(1).Post().WithHeader("Authorization", "Bearer *");

Alternatively, Something like the UriPattern class for the HttpRequestMessage could work as well. It will make the whole experience more flexible and consistent with how requests are made as well:

HttpRequestMessagePattern sut = new()
{
    Method = Value.OneOf("GET", "POST", "DELETE")
};

using HttpRequestMessage input = new(new HttpMethod(httpMethod), "https://localhost");

Assert.Equal(match, sut.Matches(input));

The biggest advantage is that you can just create a pattern for the complete request and test it in one go.
The disadvantage is that it could be more difficult to create the most sensible error messages...

@dnperfors dnperfors added the enhancement New feature or request label Dec 9, 2022
@dnperfors dnperfors changed the title Add description of the requests that are made Refactor the assertion mechanism (was: Add description of the requests that are made) Oct 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant