https://github.com/mcgppeters/aranea.httpmessagehandler
https://github.com/mcgppeters/aranea.httpmessagehandler
asp-net-core httpmessagehandler testing
Last synced: 7 months ago
JSON representation
- Host: GitHub
- URL: https://github.com/mcgppeters/aranea.httpmessagehandler
- Owner: MCGPPeters
- License: mit
- Created: 2017-03-27T09:11:20.000Z (almost 9 years ago)
- Default Branch: develop
- Last Pushed: 2017-05-20T11:47:13.000Z (over 8 years ago)
- Last Synced: 2025-06-24T01:49:15.253Z (7 months ago)
- Topics: asp-net-core, httpmessagehandler, testing
- Language: C#
- Size: 84 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# Aranea.HttpMessageHandler
A port of Damian Hickey's [OwinHttpMessageHandler](https://github.com/damianh/OwinHttpMessageHandler) so that the concept can be used for ASP .NET core as well. The readme below shows this as it is also a port of the associated readme :) .
[](https://www.nuget.org/packages/Aranea.HttpMessageHandler)
[](https://ci.appveyor.com/project/MCGPPeters/aranea-httpmessagehandler)
An implementation of System.Net.Http.HttpMessageHandler that translates an HttpRequestMessage into an ASP.NET Core compatible HttpContext, calls the supplied RequestDelegate and translates the result to an HttpResponseMessage. This allows you to call an ASP.NET Core application (RequestDelegate) / Middleware using an HttpClient without actually hitting the network stack. Useful for testing and embedded scenarios.
Install via NuGet : [](https://www.nuget.org/packages/Aranea.HttpMessageHandler)
## Using
```C#
var handler = new AspNetCoreHttpMessageHandler(appFunc) // Alternatively you can pass in Middleware
{
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 HttpContext is defined to look as though the source of the request is local. You can adjust the HttpContext via a delegate:
```C#
var httpClient = new HttpClient(new AspNetCoreHttpMessageHandler(context =>
{
if (context.Request.Path == $"/{requestPath}")
context.Response.StatusCode = 200;
...
}));
```
More information on [Http Message Handlers](http://www.asp.net/web-api/overview/working-with-http/http-message-handlers)