/OwinHttpMessageHandler

An implementation of System.Net.Http.HttpMessageHandler that translates an HttpRequestMessage into an OWIN compatible environment dictionary

Primary LanguageC#MIT LicenseMIT

OwinHttpMessageHandler Build status NuGet Status

An implementation of System.Net.Http.HttpMessageHandler that translates an HttpRequestMessage into an OWIN compatible environment dictionary, calls the supplied AppFunc and translates the result to an HttpResponseMessage. This allows you to call an OWIN application / middleware using HttpClient without actually hitting the network stack. Useful for testing and embedded scenarios.

Install via nuget.

Using

var handler = new OwinHttpMessageHandler(appFunc) // Alternatively you can pass in a MidFunc
{
    UseCookies = true, // Will send cookies on subsequent requests. Default is false.
    AllowAutoRedirect = true // The handler will auto follow 301/302
}
var httpClient = new HttpClient(handler)
{
    BaseAddress = new Uri("http://localhost")
}

var response = await httpClient.GetAsync("/");

By default, the OWIN environment is defined to look as though the source of the request is local. You can adjust the OWIN environment via a delegate:

Func<IDictionary<string, object>, Task> appFunc;
...
var httpClient = new HttpClient(new OwinHttpMessageHandler(appFunc, env =>
{
    env[Constants.ServerRemoteIpAddressKey] ="10.1.1.1";
}));

More information on Http Message Handlers

Licence : MIT

Follow me @randompunter