Created
May 22, 2020 00:26
-
-
Save josephwoodward/558ef565ae664f60acd365948ea55a20 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | |
}; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Which namespase should I use for HttpClientInterceptorOptions ?