HttpClientMock Class

A mock HTTP server for testing HTTP clients. Use this to simulate HTTP endpoints without requiring a real server.

public sealed class HttpClientMock : IAsyncDisposable

Remarks

The single-argument constructors all accept a reference type, so a bare null is ambiguous. Use a named argument to select one, for instance new HttpClientMock(configureLogging: null, configureServices: services => ...).

Example

Create a mock server with a simple endpoint:

await using var mock = new HttpClientMock();
mock.MapGet("/api/users", () => Results.Ok(new { name = "John" }));
using var httpClient = mock.CreateHttpClient();
using var response = await httpClient.GetAsync(new Uri("/api/users", UriKind.Relative));
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("""{"name":"John"}""", await response.Content.ReadAsStringAsync());
Implements

Constructors

NameDescription
HttpClientMock()Initializes a new instance of the HttpClientMock class.
HttpClientMock(ILogger?)Initializes a new instance of the HttpClientMock class with a logger.
HttpClientMock(ILogger?, Action<IServiceCollection>?)Initializes a new instance of the HttpClientMock class with a logger and custom services.
HttpClientMock(ILoggerProvider?)Initializes a new instance of the HttpClientMock class with a logger provider.
HttpClientMock(ILoggerProvider?, Action<IServiceCollection>?)Initializes a new instance of the HttpClientMock class with a logger provider and custom services.
HttpClientMock(Action<ILoggingBuilder>?)Initializes a new instance of the HttpClientMock class with custom logging configuration.
HttpClientMock(Action<ILoggingBuilder>?, Action<IServiceCollection>?)Initializes a new instance of the HttpClientMock class with custom logging and service configuration.

HttpClientMock overloads

HttpClientMock()

HttpClientMock.cs

Initializes a new instance of the HttpClientMock class.

public HttpClientMock()

HttpClientMock(ILogger?)

HttpClientMock.cs

Initializes a new instance of the HttpClientMock class with a logger.

public HttpClientMock(ILogger? logger)
Parameters
logger ILogger?
The logger to use for logging HTTP requests and responses.

HttpClientMock(ILogger?, Action<IServiceCollection>?)

HttpClientMock.cs

Initializes a new instance of the HttpClientMock class with a logger and custom services.

public HttpClientMock(ILogger? logger, Action<IServiceCollection>? configureServices)
Parameters
logger ILogger?
The logger to use for logging HTTP requests and responses.
configureServices Action<IServiceCollection>?
An action to configure services.

HttpClientMock(ILoggerProvider?)

HttpClientMock.cs

Initializes a new instance of the HttpClientMock class with a logger provider.

public HttpClientMock(ILoggerProvider? loggerProvider)
Parameters
loggerProvider ILoggerProvider?
The logger provider to use for logging HTTP requests and responses.

HttpClientMock(ILoggerProvider?, Action<IServiceCollection>?)

HttpClientMock.cs

Initializes a new instance of the HttpClientMock class with a logger provider and custom services.

public HttpClientMock(ILoggerProvider? loggerProvider, Action<IServiceCollection>? configureServices)
Parameters
loggerProvider ILoggerProvider?
The logger provider to use for logging HTTP requests and responses.
configureServices Action<IServiceCollection>?
An action to configure services.

HttpClientMock(Action<ILoggingBuilder>?)

HttpClientMock.cs

Initializes a new instance of the HttpClientMock class with custom logging configuration.

public HttpClientMock(Action<ILoggingBuilder>? configureLogging)
Parameters
configureLogging Action<ILoggingBuilder>?
An action to configure logging.

HttpClientMock(Action<ILoggingBuilder>?, Action<IServiceCollection>?)

HttpClientMock.cs

Initializes a new instance of the HttpClientMock class with custom logging and service configuration.

public HttpClientMock(Action<ILoggingBuilder>? configureLogging, Action<IServiceCollection>? configureServices)
Parameters
configureLogging Action<ILoggingBuilder>?
An action to configure logging.
configureServices Action<IServiceCollection>?
An action to configure services.

Properties

NameDescription
ApplicationGets the underlying WebApplication instance.

Gets the underlying WebApplication instance.

public WebApplication Application { get; }

Property value

WebApplication

Methods

NameDescription
CreateHttpClient()Creates an HttpClient that sends requests to the mock server.
CreateHttpMessageHandler()Creates an HttpMessageHandler that sends requests to the mock server.
DisposeAsync()Disposes the mock server asynchronously.
ForwardUnknownRequestsToUpstream()Configures the mock to forward unmatched requests to the actual upstream server.
ForwardUnknownRequestsToUpstream(HttpClient)Configures the mock to forward unmatched requests to the actual upstream server using the specified HttpClient.
MapDelete(string, RequestDelegate)Maps a DELETE request to the specified URL pattern.
MapDelete(string, Delegate)Maps a DELETE request to the specified URL pattern.
MapGet(string, RequestDelegate)Maps a GET request to the specified URL pattern.
MapGet(string, Delegate)Maps a GET request to the specified URL pattern.
MapHead(string, RequestDelegate)Maps a HEAD request to the specified URL pattern.
MapHead(string, Delegate)Maps a HEAD request to the specified URL pattern.
MapOptions(string, RequestDelegate)Maps a OPTIONS request to the specified URL pattern.
MapOptions(string, Delegate)Maps a OPTIONS request to the specified URL pattern.
MapPatch(string, RequestDelegate)Maps a PATCH request to the specified URL pattern.
MapPatch(string, Delegate)Maps a PATCH request to the specified URL pattern.
MapPost(string, RequestDelegate)Maps a POST request to the specified URL pattern.
MapPost(string, Delegate)Maps a POST request to the specified URL pattern.
MapPut(string, RequestDelegate)Maps a PUT request to the specified URL pattern.
MapPut(string, Delegate)Maps a PUT request to the specified URL pattern.

CreateHttpClient()

HttpClientMock.cs

Creates an HttpClient that sends requests to the mock server.

public HttpClient CreateHttpClient()

Returns

HttpClient An HttpClient configured to send requests to the mock server.

CreateHttpMessageHandler()

HttpClientMock.cs

Creates an HttpMessageHandler that sends requests to the mock server.

public HttpMessageHandler CreateHttpMessageHandler()

Returns

HttpMessageHandler An HttpMessageHandler configured to send requests to the mock server.

DisposeAsync()

HttpClientMock.cs

Disposes the mock server asynchronously.

public ValueTask DisposeAsync()

Returns

ValueTask A ValueTask representing the asynchronous dispose operation.

ForwardUnknownRequestsToUpstream overloads

ForwardUnknownRequestsToUpstream()

HttpClientMock.cs

Configures the mock to forward unmatched requests to the actual upstream server.

public void ForwardUnknownRequestsToUpstream()

ForwardUnknownRequestsToUpstream(HttpClient)

HttpClientMock.cs

Configures the mock to forward unmatched requests to the actual upstream server using the specified HttpClient.

public void ForwardUnknownRequestsToUpstream(HttpClient httpClient)
Parameters
httpClient HttpClient
The HttpClient to use for forwarding requests.

MapDelete overloads

MapDelete(string, RequestDelegate)

HttpClientMock.MapMethods.cs

Maps a DELETE request to the specified URL pattern.

public IEndpointConventionBuilder MapDelete(string url, RequestDelegate handler)
Parameters
url string
The URL pattern to match. Can include route parameters, query strings, and absolute URLs with scheme and host.
handler RequestDelegate
The request handler to execute when the URL pattern is matched.
Returns

IEndpointConventionBuilder An IEndpointConventionBuilder for configuring the endpoint.

MapDelete(string, Delegate)

HttpClientMock.MapMethods.cs

Maps a DELETE request to the specified URL pattern.

public IEndpointConventionBuilder MapDelete(string url, Delegate handler)
Parameters
url string
The URL pattern to match. Can include route parameters, query strings, and absolute URLs with scheme and host.
handler Delegate
The delegate to execute when the URL pattern is matched.
Returns

IEndpointConventionBuilder An IEndpointConventionBuilder for configuring the endpoint.

MapGet overloads

MapGet(string, RequestDelegate)

HttpClientMock.MapMethods.cs

Maps a GET request to the specified URL pattern.

Parameters
url string
The URL pattern to match. Can include route parameters, query strings, and absolute URLs with scheme and host.
handler RequestDelegate
The request handler to execute when the URL pattern is matched.
Returns

IEndpointConventionBuilder An IEndpointConventionBuilder for configuring the endpoint.

MapGet(string, Delegate)

HttpClientMock.MapMethods.cs

Maps a GET request to the specified URL pattern.

public IEndpointConventionBuilder MapGet(string url, Delegate handler)
Parameters
url string
The URL pattern to match. Can include route parameters, query strings, and absolute URLs with scheme and host.
handler Delegate
The delegate to execute when the URL pattern is matched.
Returns

IEndpointConventionBuilder An IEndpointConventionBuilder for configuring the endpoint.

MapHead overloads

MapHead(string, RequestDelegate)

HttpClientMock.MapMethods.cs

Maps a HEAD request to the specified URL pattern.

Parameters
url string
The URL pattern to match. Can include route parameters, query strings, and absolute URLs with scheme and host.
handler RequestDelegate
The request handler to execute when the URL pattern is matched.
Returns

IEndpointConventionBuilder An IEndpointConventionBuilder for configuring the endpoint.

MapHead(string, Delegate)

HttpClientMock.MapMethods.cs

Maps a HEAD request to the specified URL pattern.

public IEndpointConventionBuilder MapHead(string url, Delegate handler)
Parameters
url string
The URL pattern to match. Can include route parameters, query strings, and absolute URLs with scheme and host.
handler Delegate
The delegate to execute when the URL pattern is matched.
Returns

IEndpointConventionBuilder An IEndpointConventionBuilder for configuring the endpoint.

MapOptions overloads

MapOptions(string, RequestDelegate)

HttpClientMock.MapMethods.cs

Maps a OPTIONS request to the specified URL pattern.

public IEndpointConventionBuilder MapOptions(string url, RequestDelegate handler)
Parameters
url string
The URL pattern to match. Can include route parameters, query strings, and absolute URLs with scheme and host.
handler RequestDelegate
The request handler to execute when the URL pattern is matched.
Returns

IEndpointConventionBuilder An IEndpointConventionBuilder for configuring the endpoint.

MapOptions(string, Delegate)

HttpClientMock.MapMethods.cs

Maps a OPTIONS request to the specified URL pattern.

public IEndpointConventionBuilder MapOptions(string url, Delegate handler)
Parameters
url string
The URL pattern to match. Can include route parameters, query strings, and absolute URLs with scheme and host.
handler Delegate
The delegate to execute when the URL pattern is matched.
Returns

IEndpointConventionBuilder An IEndpointConventionBuilder for configuring the endpoint.

MapPatch overloads

MapPatch(string, RequestDelegate)

HttpClientMock.MapMethods.cs

Maps a PATCH request to the specified URL pattern.

public IEndpointConventionBuilder MapPatch(string url, RequestDelegate handler)
Parameters
url string
The URL pattern to match. Can include route parameters, query strings, and absolute URLs with scheme and host.
handler RequestDelegate
The request handler to execute when the URL pattern is matched.
Returns

IEndpointConventionBuilder An IEndpointConventionBuilder for configuring the endpoint.

MapPatch(string, Delegate)

HttpClientMock.MapMethods.cs

Maps a PATCH request to the specified URL pattern.

public IEndpointConventionBuilder MapPatch(string url, Delegate handler)
Parameters
url string
The URL pattern to match. Can include route parameters, query strings, and absolute URLs with scheme and host.
handler Delegate
The delegate to execute when the URL pattern is matched.
Returns

IEndpointConventionBuilder An IEndpointConventionBuilder for configuring the endpoint.

MapPost overloads

MapPost(string, RequestDelegate)

HttpClientMock.MapMethods.cs

Maps a POST request to the specified URL pattern.

Parameters
url string
The URL pattern to match. Can include route parameters, query strings, and absolute URLs with scheme and host.
handler RequestDelegate
The request handler to execute when the URL pattern is matched.
Returns

IEndpointConventionBuilder An IEndpointConventionBuilder for configuring the endpoint.

MapPost(string, Delegate)

HttpClientMock.MapMethods.cs

Maps a POST request to the specified URL pattern.

public IEndpointConventionBuilder MapPost(string url, Delegate handler)
Parameters
url string
The URL pattern to match. Can include route parameters, query strings, and absolute URLs with scheme and host.
handler Delegate
The delegate to execute when the URL pattern is matched.
Returns

IEndpointConventionBuilder An IEndpointConventionBuilder for configuring the endpoint.

MapPut overloads

MapPut(string, RequestDelegate)

HttpClientMock.MapMethods.cs

Maps a PUT request to the specified URL pattern.

Parameters
url string
The URL pattern to match. Can include route parameters, query strings, and absolute URLs with scheme and host.
handler RequestDelegate
The request handler to execute when the URL pattern is matched.
Returns

IEndpointConventionBuilder An IEndpointConventionBuilder for configuring the endpoint.

MapPut(string, Delegate)

HttpClientMock.MapMethods.cs

Maps a PUT request to the specified URL pattern.

public IEndpointConventionBuilder MapPut(string url, Delegate handler)
Parameters
url string
The URL pattern to match. Can include route parameters, query strings, and absolute URLs with scheme and host.
handler Delegate
The delegate to execute when the URL pattern is matched.
Returns

IEndpointConventionBuilder An IEndpointConventionBuilder for configuring the endpoint.