Skip to content

Instantly share code, notes, and snippets.

@josephwoodward
Created May 22, 2020 00:26
Show Gist options
  • Save josephwoodward/558ef565ae664f60acd365948ea55a20 to your computer and use it in GitHub Desktop.
Save josephwoodward/558ef565ae664f60acd365948ea55a20 to your computer and use it in GitHub Desktop.
public class Fixture<TStartup> : WebApplicationFactory<TStartup> where TStartup : class
{
public HttpClientInterceptorOptions InterceptorOptions { get; } =
new HttpClientInterceptorOptions {ThrowOnMissingRegistration = true};
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureServices(s
=> s.AddSingleton<IHttpMessageHandlerBuilderFilter, InterceptionFilter>(
_ => new InterceptionFilter(InterceptorOptions)));
base.ConfigureWebHost(builder);
}
private sealed class InterceptionFilter : IHttpMessageHandlerBuilderFilter
{
private readonly HttpClientInterceptorOptions _options;
internal InterceptionFilter(HttpClientInterceptorOptions options)
=> _options = options;
public Action<HttpMessageHandlerBuilder> Configure(Action<HttpMessageHandlerBuilder> next)
=> builder =>
{
next(builder);
builder.AdditionalHandlers.Add(_options.CreateHttpMessageHandler());
};
}
}
@mastery-pankaj-gaykawad

Which namespase should I use for HttpClientInterceptorOptions ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment