HttpFake.AspNetCore.Mvc.Testing 1.0.2

Suggested Alternatives

HttpFake

Additional Details

The package does not longer provide separate support for Microsoft.AspNetCore.Mvc.Testing, it comes already by installing the HttpFake NuGet package.

dotnet add package HttpFake.AspNetCore.Mvc.Testing --version 1.0.2                
NuGet\Install-Package HttpFake.AspNetCore.Mvc.Testing -Version 1.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.AspNetCore.Mvc.Testing" Version="1.0.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add HttpFake.AspNetCore.Mvc.Testing --version 1.0.2                
#r "nuget: HttpFake.AspNetCore.Mvc.Testing, 1.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.AspNetCore.Mvc.Testing as a Cake Addin
#addin nuget:?package=HttpFake.AspNetCore.Mvc.Testing&version=1.0.2

// Install HttpFake.AspNetCore.Mvc.Testing as a Cake Tool
#tool nuget:?package=HttpFake.AspNetCore.Mvc.Testing&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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.2 205 7/31/2023 1.0.2 is deprecated because it is no longer maintained.