This library hosts ASP.NET Core web applications with Kestrel in the test process and allows to configure them in various ways.
$ dotnet add package Nickogl.AspNetCore.IntegrationTestingThis library hosts ASP.NET Core web applications in the test process, making tests and the application under test easy to debug and allowing each of the tests to configure the web application differently.
Unlike WebApplicationFactory from the Microsoft.AspNetCore.Mvc.Testing package,
this library hosts the web applications with Kestrel, allowing one to make real
HTTP and websocket requests against the application. It is thus ideal for orchestrating
integration tests of various components.
WebApplicationBuilder.Build():
IWebHostBuilderWebApplicationBuilder.Build() and before WebApplication.Run():
IApplicationBuilderFull code can be found here.
using var app = new WebApplicationTestHost<Program>();
app.ConfigureOptions<SampleOptions>(options => options.Text = "bar");
using var httpClient = new HttpClient() { BaseAddress = app.BaseAddress };
var response = await httpClient.GetAsync("/text");
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
Assert.AreEqual("bar", await response.Content.ReadAsStringAsync());
using var textFile = new TemporaryFile();
using var app = new WebApplicationTestHost<Program>();
app.ConfigureOptions<SampleOptions>(options => options.PersistedTextFilePath = textFile.Path);
using var httpClient = new HttpClient() { BaseAddress = app.BaseAddress };
await httpClient.PostAsync("/text", new StringContent("bar"));
await app.StopAsync();
await app.StartAsync();
var response = await httpClient.GetAsync("/text");
Assert.AreEqual(HttpStatusCode.OK, response.StatusCode);
Assert.AreEqual("bar", await response.Content.ReadAsStringAsync());
using var app = new WebApplicationTestHost<Program>();
app.ConfigureServices(services =>
{
services.RemoveAll<ISampleService>();
services.AddSingleton<ISampleService, SampleServiceMock>();
});
using var httpClient = new HttpClient() { BaseAddress = app.BaseAddress };
var response = await httpClient.GetAsync("/sample");
Assert.AreEqual("mocked", await response.Content.ReadAsStringAsync());
int observedRequests = 0;
using var app = new WebApplicationTestHost<Program>();
app.ConfigureApplication(app =>
{
app.Use((next) =>
{
return async httpContext =>
{
await next(httpContext);
Interlocked.Increment(ref observedRequests);
};
});
});
using var httpClient = new HttpClient() { BaseAddress = app.BaseAddress };
await httpClient.GetAsync("/text");
Assert.AreEqual(1, observedRequests);