Skip to content

Commit

Permalink
Allow consumers to specify the interception options prior to app startup
Browse files Browse the repository at this point in the history
  • Loading branch information
shaynevanasperen committed Dec 31, 2020
1 parent c316259 commit 7842d8e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ public static class ServiceCollectionExtensions
/// are isolated.
/// </summary>
/// <param name="services">The <see cref="IServiceCollection" /> to add services to.</param>
/// <param name="options">The <see cref="HttpClientInterceptorOptions"/> to use for configuring the interceptions.</param>
/// <returns>A reference to this instance after the operation has completed.</returns>
public static IServiceCollection AddHttpRequestInterceptor(this IServiceCollection services)
public static IServiceCollection AddHttpRequestInterceptor(this IServiceCollection services, HttpClientInterceptorOptions options)
{
if (services == null) throw new ArgumentNullException(nameof(services));

services.AddSingleton(new HttpClientInterceptorOptions().ThrowsOnMissingRegistration());
services.AddSingleton(options);
services.AddSingleton<IHttpMessageHandlerBuilderFilter, HttpInterceptionFilter>();
services.AddTransient<ITestScopeProducer>(provider =>
new TestScopeProducer<IDisposable>(provider.GetRequiredService<HttpClientInterceptorOptions>().BeginScope, x => x.Dispose));
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ public abstract class WebApplicationTests : XunitWebApplicationTests<Startup> //
}));
factory.AfterConfigureServices(services =>
{
services.AddHttpRequestInterceptor();
services.AddHttpRequestInterceptor(InterceptHttp);
services.ReplaceLoggerFactoryWithXUnit(testOutputHelper);
});
}

public HttpClientInterceptorOptions Interceptor => App.Services.GetRequiredService<HttpClientInterceptorOptions>();
protected HttpClientInterceptorOptions InterceptHttp { get; } = new HttpClientInterceptorOptions().ThrowsOnMissingRegistration();
}
```
And then add further customization in a test class:
Expand Down

0 comments on commit 7842d8e

Please sign in to comment.