-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Configure existing HttpClient using WebApplicationFactory (e.g. for named clients using IServiceCollection.AddHttpClient<T>) #38883
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
Comments
We could sensibly add a Tagging this alongside #33846 as we approach addressing some of these issues the WAF for .NET 7. |
Thanks for contacting us. |
Thanks for contacting us. We're moving this issue to the |
I created a custom public class TestServerHttpMessageHandlerBuilder : HttpMessageHandlerBuilder
{
public TestServerHttpMessageHandlerBuilder(TestServer testServer, IServiceProvider services)
{
Services = services;
PrimaryHandler = testServer.CreateHandler();
}
private string? _name;
[DisallowNull]
public override string? Name
{
get => _name;
set
{
ArgumentNullException.ThrowIfNull(value);
_name = value;
}
}
public override HttpMessageHandler PrimaryHandler { get; set; }
public override IList<DelegatingHandler> AdditionalHandlers { get; } = new List<DelegatingHandler>();
public override IServiceProvider Services { get; }
public override HttpMessageHandler Build()
{
if (PrimaryHandler == null)
{
throw new InvalidOperationException($"{nameof(PrimaryHandler)} must not be null");
}
return CreateHandlerPipeline(PrimaryHandler, AdditionalHandlers);
}
} Then in the startup code register the custom clientServices.AddTransient<HttpMessageHandlerBuilder>(sp => new TestServerHttpMessageHandlerBuilder(webAppFactory.Server, sp));
|
not sure if helpful but also such libraries do similar things?
|
Is your feature request related to a problem? Please describe.
This is a duplicate of #5967 but because it was closed due to inactivity, I am creating a new one with more details and use cases.
I am trying to run tests against a Hot Chocolate (GraphQL) server and the Strawberry Shake GraphQL client, which uses
HttpClientFactory
with a named client.I'd like to start my app in my tests using
Microsoft.AspNetCore.Mvc.Testing
, and use the code generation features of Strawberry Shake to generate the GraphQL client. I can't, because Strawberry Shake uses theHttpClientFactory
, and all of the methods required to build or configure an HttpClient are internal inWebApplicationFactory
.Example of the code in my tests I'd like to have (simplified and inlined):
Describe the solution you'd like
It would be much simpler if instead of just having
CreateClient()
, I could also useConfigureClient(HttpClient)
.e.g.:
The code is mostly the same as is already inside
WebApplicationFactory
, however instead of making more properties and methods public, there would be a method to assist in cases where using HttpClientFactory is desirable.Additional context
While my use case (and the one from #5967) are because of external libraries, I believe making use of a pooled HttpClient factory can make sense for reusing existing code from internal libraries.
The text was updated successfully, but these errors were encountered: