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

Add support for supplying custom HttpClient #103

Closed
JoeBatt1989 opened this issue Jul 25, 2023 · 16 comments
Closed

Add support for supplying custom HttpClient #103

JoeBatt1989 opened this issue Jul 25, 2023 · 16 comments
Assignees
Labels
enhancement New feature or request waiting for feedback Waiting for feedback from issue submitter

Comments

@JoeBatt1989
Copy link

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
Copy link
Owner

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 ;) !

@basdijkstra basdijkstra added the question Further information is requested label Jul 25, 2023
@basdijkstra basdijkstra self-assigned this Jul 25, 2023
@JoeBatt1989
Copy link
Author

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
Copy link
Owner

basdijkstra commented Jul 25, 2023

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
Copy link
Author

Yeah that would definitely work! :)

@basdijkstra
Copy link
Owner

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 basdijkstra added enhancement New feature or request and removed question Further information is requested labels Jul 25, 2023
@basdijkstra basdijkstra changed the title (Question) RestAssured and WebApplicationFactory example Add support for supplying custom HttpClient Jul 25, 2023
@basdijkstra
Copy link
Owner

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
Copy link
Owner

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
Copy link

shack05 commented Aug 4, 2023

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
Copy link
Owner

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

@basdijkstra
Copy link
Owner

basdijkstra commented Aug 14, 2023

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: 4074077

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.

@basdijkstra basdijkstra added the waiting for feedback Waiting for feedback from issue submitter label Aug 14, 2023
@JoeBatt1989
Copy link
Author

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

image
image

@basdijkstra
Copy link
Owner

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
Copy link
Author

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
Copy link
Owner

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
Copy link

shack05 commented Aug 15, 2023

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
Copy link
Owner

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request waiting for feedback Waiting for feedback from issue submitter
Projects
None yet
Development

No branches or pull requests

3 participants