-
-
Notifications
You must be signed in to change notification settings - Fork 19
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
Add support for supplying custom HttpClient #103
Comments
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 ;) ! |
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. |
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. |
Yeah that would definitely work! :) |
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 :) |
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. |
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/ |
This feature would be great. |
Thanks for the upvote, @shack05! I’ll probably start working on this next week. |
Hey @JoeBatt1989 and @shack05, I've published a first draft containing the ability to supply a custom 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: 4074077 Nothing documented (or even properly tested) yet but you can easily pass in an |
Hi @basdijkstra, thank you for this, looks good to me. I have done a GET test which worked as expected |
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! |
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! |
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. |
Hi @basdijkstra, I've tried |
Thank you for the feedback, @shack05, really happy to hear. Closing this issue. |
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
The text was updated successfully, but these errors were encountered: