The TestServer class is the primary tool for unit testing OWIN components. It allows you to create an OWIN request processing pipeline and submit requests that are processed directly in memory, bypassing the network stack.
To use it, call TestServer.Create and provide an OwinStartup delegate to configure your pipeline (e.g., adding middleware or terminal endpoints). Once the server is created, you can interact with it using the HttpClient property or via fluent request builder methods.
using(var server = TestServer.Create(app =>
{
app.UseErrorPage(); // See Microsoft.Owin.Diagnostics
app.Run(context =>
{
return context.Response.WriteAsync("Hello world using OWIN TestServer");
});
}))
{
HttpResponseMessage response = await server.HttpClient.GetAsync("/");
// TODO: Validate response
}