public static WebApplicationFactory <T> WithAuthentication <T>(this WebApplicationFactory <T> factory, TestClaimsProvider claimsProvider) where T : class { return(factory.WithWebHostBuilder(builder => { builder.ConfigureTestServices(services => { services.AddAuthentication("Test") .AddScheme <AuthenticationSchemeOptions, TestAuthHandler>("Test", op => { }); services.AddScoped <TestClaimsProvider>(_ => claimsProvider); }); })); }
public async Task Get_PageWhenAuthenticated(string url) { //Arrange var client = _factory.WithWebHostBuilder(builder => builder.ConfigureTestServices(services => services.AddAuthentication(Names.TestName).AddScheme <AuthenticationSchemeOptions, TestAuthHandler>(Names.TestName, Options => { }))) .CreateClient(new WebApplicationFactoryClientOptions { AllowAutoRedirect = false }); client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue(Names.TestName); //Act HttpResponseMessage Page = await client.GetAsync(url); //Assert Assert.Equal(HttpStatusCode.OK, Page.StatusCode); }
private HttpClient CreateTestClient(MqConsumer consumer, ITestOutputHelper output) { return(_appFactory.WithWebHostBuilder(builder => { builder.ConfigureServices(services => { services.AddLogging(b => b .SetMinimumLevel(LogLevel.Trace) .AddFilter(level => level >= LogLevel.Debug) .AddXUnit(output)); services.AddMqConsuming(registrar => { registrar.RegisterConsumer(consumer); }) .ConfigureMq(TestMqTools.ConfigureAction); }); }).CreateClient()); }