HttpFake 2.0.2

dotnet add package HttpFake --version 2.0.2
NuGet\Install-Package HttpFake -Version 2.0.2
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="HttpFake" Version="2.0.2" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add HttpFake --version 2.0.2
#r "nuget: HttpFake, 2.0.2"
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install HttpFake as a Cake Addin
#addin nuget:?package=HttpFake&version=2.0.2

// Install HttpFake as a Cake Tool
#tool nuget:?package=HttpFake&version=2.0.2

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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.

Version Downloads Last updated
2.0.2 76 3/27/2024
2.0.1 68 3/27/2024
2.0.0 61 3/27/2024
1.0.2 165 7/31/2023
1.0.1 134 7/7/2023