HttpFake 2.0.1
See the version list below for details.
dotnet add package HttpFake --version 2.0.1
NuGet\Install-Package HttpFake -Version 2.0.1
<PackageReference Include="HttpFake" Version="2.0.1" />
paket add HttpFake --version 2.0.1
#r "nuget: HttpFake, 2.0.1"
// Install HttpFake as a Cake Addin #addin nuget:?package=HttpFake&version=2.0.1 // Install HttpFake as a Cake Tool #tool nuget:?package=HttpFake&version=2.0.1
HttpFake
HttpFake is a .NET library that helps you to intercept requests and configure responses for HTTP requests, providing a powerful tool for stubbing HttpClient requests without even reaching the network. This library is especially useful when used in conjunction with Microsoft.AspNetCore.Mvc.Testing
.
It adds a delegating handler to every HttpClient created with the HttpClientFactory that will perform the request interception.
Getting Started
Firstly, install the HttpFake library via NuGet:
Install-Package HttpFake
Usage with Microsoft.AspNetCore.Mvc.Testing
Next, you need to register HttpFake services in your test startup configuration:
public sealed class SampleWebApplicationFactory : WebApplicationFactory<IAssemblyMarker>
{
protected override void ConfigureWebHost(IWebHostBuilder builder)
{
builder.ConfigureTestServices(services =>
{
services.AddHttpClientFactoryInterceptor();
});
}
}
You can use HttpFake with Microsoft.AspNetCore.Mvc.Testing
to avoid sending actual HTTP requests during testing. This allows you to isolate your tests, providing consistent and controlled responses to HTTP requests.
Below is an example of how to set up and use HttpFake within a test:
[Collection(nameof(SampleWebApplicationTestCollectionDefinition))]
public sealed class WhenInterceptingHttpRequest
{
private readonly SampleWebApplicationFactory _webApplicationFactory;
public WhenInterceptingHttpRequest(SampleWebApplicationFactory webApplicationFactory)
{
_webApplicationFactory = webApplicationFactory;
}
[Fact]
public async Task AvoidsSendingHttpRequestThroughTheNetworkAndReturnsConfiguredResponse()
{
// Arrange
const string endpointPath = "/configured-request-by-absolute-path";
var configuredResponse = new DummyObject(97, new DateTimeOffset(2023, 1, 23, 1, 2, 3, TimeSpan.Zero), "Text");
var interceptor = _webApplicationFactory.Services.GetRequiredService<ConfiguredHttpRequestsInterceptor>();
using var _ = interceptor.Register(new ConfiguredResponseBuilder()
.WithAbsolutePath("/absolute/path")
.RespondWith(new HttpResponseMessage(HttpStatusCode.OK) { Content = JsonContent.Create(configuredResponse) })
.Build());
// Act
using var httpClient = _webApplicationFactory.CreateClient();
using var response = await httpClient.GetAsync(endpointPath);
// Assert
response.StatusCode.Should().Be(HttpStatusCode.OK, because: await response.Content.ReadAsStringAsync());
var responseContent = await response.Content.ReadFromJsonAsync<DummyObject>();
responseContent.Should().BeEquivalentTo(configuredResponse);
}
}
In this example, the HTTP GET request to "/configured-request-by-absolute-path"
will create an HTTP client with the IHttpClientFactory
and send another request to {AnyHostUrl}/absolute/path
that will be intercepted and a pre-configured response will be returned.
Contributing
HttpFake is an open-source project and we welcome contributions! Feel free to submit a pull request with enhancements, bug fixes, or other improvements.
License
HttpFake is licensed under the MIT License. For more information, please refer to the LICENSE file in the repository.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net8.0
- Microsoft.Extensions.DependencyInjection (>= 8.0.0)
- Microsoft.Extensions.Http (>= 8.0.0)
- Microsoft.Extensions.Options (>= 8.0.2)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on HttpFake:
Package | Downloads |
---|---|
HttpFake.AspNetCore.Mvc.Testing
Mock and stub HTTP requests |
GitHub repositories
This package is not used by any popular GitHub repositories.