HttpFake 1.0.2
See the version list below for details.
dotnet add package HttpFake --version 1.0.2
NuGet\Install-Package HttpFake -Version 1.0.2
<PackageReference Include="HttpFake" Version="1.0.2" />
paket add HttpFake --version 1.0.2
#r "nuget: HttpFake, 1.0.2"
// Install HttpFake as a Cake Addin #addin nuget:?package=HttpFake&version=1.0.2 // Install HttpFake as a Cake Tool #tool nuget:?package=HttpFake&version=1.0.2
HttpFake
HttpFake is a .NET library that helps you to intercept and configure responses for HttpClient
requests, providing a powerful tool for integration testing in ASP.NET Core applications. This library is especially useful when used in conjunction with Microsoft.AspNetCore.Mvc.Testing
, as it allows you to test your application's HTTP communication without sending real network requests.
Basically it adds a delegating handler to every HttpClient created with the HttpClientFactory that will perform the 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 | net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 is compatible. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. 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. |
-
net6.0
- No dependencies.
-
net7.0
- No dependencies.
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.