basdijkstra / rest-assured-net

C# DSL for writing readable tests for HTTP-based APIs
Apache License 2.0
134 stars 16 forks source link

Add support for supplying custom HttpClient #103

Closed JoeBatt1989 closed 10 months ago

JoeBatt1989 commented 11 months ago

Are there any examples of integrating RestAssured and WebApplicationFactory, just wondering if you can override the RestAssured client with what is returned when spinning up services in memory

basdijkstra commented 11 months ago

Hey @JoeBatt1989, currently, RestAssured .Net is basically a wrapper around the HttpClient built into .NET.

I don’t have any examples using WebApplicationFactory, what’s the type of the client returned when you spin up such a service?

And can you give me your ideas on the benefits that this integration would provide?

I’m happy to investigate (once I return back to work after my summer holiday break ;) !

JoeBatt1989 commented 11 months ago

Hi @basdijkstra, thanks for getting back so quickly (definitely dont want to interrupt your holiday :) )

So genrally we work really closely with developers and have access to the backend code and spin up the services as part of tests (https://github.com/Project-MONAI/monai-deploy-workflow-manager/blob/develop/tests/IntegrationTests/TaskManager.IntegrationTests/Hooks.cs)

We use WebApplicationFactory to do this as it provides us the ability to override databases, auth etc. and keeps your tests inline with src Program.cs. We usually integrate this with SpecFlow for the tests (similar to this https://blog.tonysneed.com/2022/01/28/using-specflow-for-bdd-with-net-6-web-api/). However I was wondering if we could ditch Specflow for RestAssured (most common framework in Java so switching language is fairly easy) but in order to interact with the services running in memory you need to use the HttpClient thats returned from the WeApplicationFactory implementation rather than spinning up one yourself.

We could use IHost to spin up the services but I have seen instances where the src program.cs and Test IHost can diverage so I try to use WebApplicationFactory where possible.

basdijkstra commented 11 months ago

Thanks @JoeBatt1989 ! Don’t worry about interrupting me, it’s my choice to have a look at and answer incoming messages :)

Would providing an overload for the Given() method that takes an argument of type HttpClient work? You could then use that to pass in your HttpClient instance and RestAssured will then use that instead of creating a new one when sending a request.

I’ll have to make sure that any settings / config / properties is properly preserved but I think this is something that can be done.

Let me know if that sounds like a plan and I’m happy to have a look when I return early August.

JoeBatt1989 commented 11 months ago

Yeah that would definitely work! :)

basdijkstra commented 11 months ago

Awesome, I’ll get to work once I’m back home.

Thank you for the suggestion, I can see how this could make RestAssured a much more versatile library, which is one of the things I wanted to work on at some point anyway :)

basdijkstra commented 11 months ago

Note to self: just had a quick look at the code and implementing this seems entirely doable.

Start by creating the Given() overload, optionally move creation of HttpClient (if not supplied) to separate method (maybe split this off to separate class), pass HttpClient to HTTP request processor instead of creating it there.

basdijkstra commented 11 months ago

Here’s a very simple example that can likely be repurposed to test this new feature: https://www.claudiobernasconi.ch/2022/03/03/integration-testing-asp-net-core-6-webapi-applications/

shack05 commented 11 months ago

This feature would be great. My use case is that I'd like to use a HttpClient with a HttpClientHandler with AllowAutoRedirect set to false.

basdijkstra commented 11 months ago

Thanks for the upvote, @shack05! I’ll probably start working on this next week.

basdijkstra commented 10 months ago

Hey @JoeBatt1989 and @shack05, I've published a first draft containing the ability to supply a custom HttpClient in 4.1.0-beta.3: https://www.nuget.org/packages/RestAssured.Net/4.1.0-beta.3

Could you please try it out and see if it works as expected? It's a really straightforward implementation at the moment but I'd like to see if this is enough first.

Here's the commit containing all the changes: https://github.com/basdijkstra/rest-assured-net/commit/407407745f3558acd07c915f869dd612ecf057df

Nothing documented (or even properly tested) yet but you can easily pass in an HttpClient as an argument to the Given() method. When none is supplied, the library will create one.

JoeBatt1989 commented 10 months ago

Hi @basdijkstra, thank you for this, looks good to me. I have done a GET test which worked as expected

image image

basdijkstra commented 10 months ago

Nice. Why are the middle two tests failing, by the way? Do they still use the client that RestAssured creates?

I'll give @shack05 some time to give me their feedback and if all goes well I will probably release 4.1.0 somewhere tomorrow. Still have to build some tests myself but I can always do that later.

Thank you for the quick feedback!

JoeBatt1989 commented 10 months ago

Oh that was just a few negative tests, one using a new HttpClient not generated by WebApplicationFactory and one using the HttpClient RestAssured creates, both couldnt connect to the services which is what I expected.

Fantastic, thank you for turning this around so quick!

basdijkstra commented 10 months ago

You're welcome! I've managed to set up and run a test project of my own now, too, will incorporate that in the repository soon.

shack05 commented 10 months ago

Hey @JoeBatt1989 and @shack05, I've published a first draft containing the ability to supply a custom HttpClient in 4.1.0-beta.3: https://www.nuget.org/packages/RestAssured.Net/4.1.0-beta.3

Could you please try it out and see if it works as expected? It's a really straightforward implementation at the moment but I'd like to see if this is enough first.

Hi @basdijkstra, I've tried 4.1.0-beta.3. It works great. Thank you!

basdijkstra commented 10 months ago

Thank you for the feedback, @shack05, really happy to hear. Closing this issue.