HttpMocker is a testing layer for HttpClient library. The library is best suited when used with ASP.NET Core integration tests framework
Install nuget package
dotnet add package HttpMocker
Mock response for http request type class as client of HttpClient
.
The HttpClient
is often configured with the extension method AddHttpClient
.
services.AddHttpClient<YourExternalClientClass>();
In your tests override HttpClient
behavior inside YourExternalClientClass
.
services.AddHttpClientMock<YourExternalClientClass>()
.WithFallback(
() => new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("Content") });
Mock response for http request using named client.
The HttpClient
is often configured with the extension method AddHttpClient
.
services.AddHttpClient<YourExternalClientClass>("client-name");
In your tests override HttpClient
behavior inside YourExternalClientClass
using overload which accepts client name.
services.AddHttpClientMock("client-name")
.WithFallback(
() => new HttpResponseMessage(HttpStatusCode.OK) { Content = new StringContent("Content") });
Mock all registered http clients in IServiceCollection
.
Multiple HttpClient
services are registered in IServiceCollection
using typed and/or named methods.
services.AddHttpClient<YourExternalClientClass>();
services.AddHttpClient<YourExternalClientClass>("client-name");
In your tests override all HttpClient
registered inside IServiceCollection
using AddGlobalHttpClientMock
extension method.
services.AddGlobalHttpClientMock()
.WithFallback(() => new HttpResponseMessage(HttpStatusCode.NotFound));
The middleware is mechanisms to control respond to incoming HttpRequestMessage
.
The Http Mocker library contains multiple built-in middlewares that can be registered using extension methods:
WithFallback
WithCapturing
Custom middleware can be registered with extension method WithMiddleware<TMiddleware>
. The TMiddleware
type has to be registered in IServiceCollection
.