Devlead.Testing.MockHttp 2024.12.10.7

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

// Install Devlead.Testing.MockHttp as a Cake Tool
#tool nuget:?package=Devlead.Testing.MockHttp&version=2024.12.10.7                

Devlead.Testing.MockHttp

Opinionated .NET source package for mocking HTTP client requests

Installation

dotnet add package Devlead.Testing.MockHttp

Usage

Configuration

The Routes.json file is used to configure the mock HTTP client responses. It defines the routes, HTTP methods, URIs, optional headers, and authentication requirements. Each route specifies the expected request and the corresponding response, including content type, status code, and any required authorization.

Embedded Resources

The Routes.json file, along with other resources like Index.txt and Secret.json, are included as embedded resources in the project. This allows them to be accessed at runtime for simulating HTTP responses. These resources are configured in the .csproj file under <EmbeddedResource> tags.

Example Configuration

Routes.json
[
  {
    "Request": {
      "Methods": [
        {
          "Method": "GET"
        }
      ],
      "AbsoluteUri": "https://example.com/login/secret.json"
    },
    "Responses": [
      {
        "RequestHeaders": {},
        "ContentResource": "Example.Login.Secret.json",
        "ContentType": "application/json",
        "ContentHeaders": {},
        "StatusCode": 200
      }
    ],
    "Authorization": {
      "Authorization": [
        "Bearer AccessToken"
      ]
    }
  },
  {
    "Request": {
      "Methods": [
        {
          "Method": "GET"
        }
      ],
      "AbsoluteUri": "https://example.com/index.txt"
    },
    "Responses": [
      {
        "RequestHeaders": {},
        "ContentResource": "Example.Index.txt",
        "ContentType": "text/plain",
        "ContentHeaders": {},
        "StatusCode": 200
      }
    ]
  }
]
Example Resource Files
  • Index.txt: Contains plain text data used in responses.
Hello, World!
  • Secret.json: Contains JSON data representing a user, used in responses.
{
  "Login": "johdoe",
  "FirstName": "John",
  "LastName": "Doe"
}

Project Configuration

In the .csproj file, these resources are specified as embedded resources:

<ItemGroup>
  <EmbeddedResource Include="Resources\Example\Index.txt" />
  <EmbeddedResource Include="Resources\Routes.json" />
  <EmbeddedResource Include="Resources\Example\Login\Secret.json" />
</ItemGroup>

These configurations enable the mock HTTP client to simulate real HTTP requests and responses, facilitating testing without actual network calls.

Registering and Using the Mock HTTP Client

The mock HTTP client is registered using dependency injection in the ServiceProviderFixture.cs file. This setup allows for simulating HTTP requests and responses in a controlled test environment.

Registration

Create a class called ServiceProviderFixture.cs without name space and implement the partial method:

public static partial class ServiceProviderFixture
{
    static partial void InitServiceProvider(IServiceCollection services)
    {
        services.AddSingleton<MyService>()
                .AddMockHttpClient<Constants>();
    }
}

The AddMockHttpClient<Constants>() method configures the HTTP client for use in tests. Here, Constants is a type that serves as a parent to the resources configuration, encapsulating settings and paths for the mock HTTP client to use during testing.

Usage in Tests
  • HttpClientTests.cs: The mock HTTP client is used to perform HTTP GET requests, and the responses are verified.
public class HttpClientTests
{
    [Test]
    public async Task GetAsync()
    {
        var httpClient = ServiceProviderFixture.GetRequiredService<HttpClient>();
        var response = await httpClient.GetAsync("https://example.com/index.txt");
        await Verify(response);
    }
}
  • MyServiceTests.cs: The mock HTTP client is used within the MyService class to test various scenarios, including unauthorized and authorized access.
public class MyServiceTests
{
    [Test]
    public async Task GetSecret()
    {
        var myService = ServiceProviderFixture.GetRequiredService<MyService>(
                            configure => configure.ConfigureMockHttpClient<Constants>(
                                            client => client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue(
                                                "Bearer",
                                                "AccessToken"
                                                )
                                        )
                            );
        var result = await myService.GetSecret();
        await Verify(result);
    }
}

This approach ensures that your service logic is tested in isolation, without making actual network requests, by simulating HTTP interactions using the mock HTTP client. The Constants type helps manage the configuration of these interactions, providing a centralized way to define and access resource settings.

Example project

A real world example can be found in the Blobify project.

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.  net9.0 is compatible. 
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
2024.12.10.7 3,584 12/10/2024
2024.12.10.5 91 12/10/2024
2024.12.10.3 80 12/10/2024