I have code which adds a named HttpClient
to the ServiceCollection
and adds a DelegatingHandler
to the client. Example code:
public class MyDelegatingHandler : DelegatingHandler
{
// ...
}
public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureServices((hostContext, services) =>
{
services.AddHttpClient("MyClient")
.AddHttpMessageHandler(provider => new MyDelegatingHandler());
});
When writing unit tests, how can I verify that the DelegatingHandler
implementation added was MyDelegatingHanlder
?
This information is stored down in a private field of the
HttpClient
. You can get at it, but it's a tad unorthodox. So, you may or may not be interested, but I am going to throw this out there in case you are:To test, you'd first have to create an
HttpClient
using the factory:Then run it through the extension: