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 : IAsyncDisposableRemarks
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
| Name | Description |
|---|---|
| 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.csInitializes a new instance of the HttpClientMock class.
public HttpClientMock()HttpClientMock(ILogger?)
HttpClientMock.csInitializes a new instance of the HttpClientMock class with a logger.
public HttpClientMock(ILogger? logger)Parameters
loggerILogger?- The logger to use for logging HTTP requests and responses.
HttpClientMock(ILogger?, Action<IServiceCollection>?)
HttpClientMock.csInitializes a new instance of the HttpClientMock class with a logger and custom services.
public HttpClientMock(ILogger? logger, Action<IServiceCollection>? configureServices)Parameters
loggerILogger?- The logger to use for logging HTTP requests and responses.
configureServicesAction<IServiceCollection>?- An action to configure services.
HttpClientMock(ILoggerProvider?)
HttpClientMock.csInitializes a new instance of the HttpClientMock class with a logger provider.
public HttpClientMock(ILoggerProvider? loggerProvider)Parameters
loggerProviderILoggerProvider?- The logger provider to use for logging HTTP requests and responses.
HttpClientMock(ILoggerProvider?, Action<IServiceCollection>?)
HttpClientMock.csInitializes a new instance of the HttpClientMock class with a logger provider and custom services.
public HttpClientMock(ILoggerProvider? loggerProvider, Action<IServiceCollection>? configureServices)Parameters
loggerProviderILoggerProvider?- The logger provider to use for logging HTTP requests and responses.
configureServicesAction<IServiceCollection>?- An action to configure services.
HttpClientMock(Action<ILoggingBuilder>?)
HttpClientMock.csInitializes a new instance of the HttpClientMock class with custom logging configuration.
public HttpClientMock(Action<ILoggingBuilder>? configureLogging)Parameters
configureLoggingAction<ILoggingBuilder>?- An action to configure logging.
HttpClientMock(Action<ILoggingBuilder>?, Action<IServiceCollection>?)
HttpClientMock.csInitializes a new instance of the HttpClientMock class with custom logging and service configuration.
public HttpClientMock(Action<ILoggingBuilder>? configureLogging, Action<IServiceCollection>? configureServices)Parameters
configureLoggingAction<ILoggingBuilder>?- An action to configure logging.
configureServicesAction<IServiceCollection>?- An action to configure services.
Properties
| Name | Description |
|---|---|
| Application | Gets the underlying WebApplication instance. |
Application
HttpClientMock.csGets the underlying WebApplication instance.
public WebApplication Application { get; }Property value
Methods
| Name | Description |
|---|---|
| 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.csCreates 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.csCreates 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.csForwardUnknownRequestsToUpstream overloads
ForwardUnknownRequestsToUpstream()
HttpClientMock.csConfigures the mock to forward unmatched requests to the actual upstream server.
public void ForwardUnknownRequestsToUpstream()ForwardUnknownRequestsToUpstream(HttpClient)
HttpClientMock.csConfigures the mock to forward unmatched requests to the actual upstream server using the specified HttpClient.
public void ForwardUnknownRequestsToUpstream(HttpClient httpClient)Parameters
httpClientHttpClient- The
HttpClientto use for forwarding requests.
MapDelete overloads
MapDelete(string, RequestDelegate)
HttpClientMock.MapMethods.csMaps a DELETE request to the specified URL pattern.
public IEndpointConventionBuilder MapDelete(string url, RequestDelegate handler)Parameters
urlstring- The URL pattern to match. Can include route parameters, query strings, and absolute URLs with scheme and host.
handlerRequestDelegate- The request handler to execute when the URL pattern is matched.
Returns
IEndpointConventionBuilder An IEndpointConventionBuilder for configuring the endpoint.
MapDelete(string, Delegate)
HttpClientMock.MapMethods.csMaps a DELETE request to the specified URL pattern.
public IEndpointConventionBuilder MapDelete(string url, Delegate handler)Parameters
urlstring- The URL pattern to match. Can include route parameters, query strings, and absolute URLs with scheme and host.
handlerDelegate- 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.csMaps a GET request to the specified URL pattern.
public IEndpointConventionBuilder MapGet(string url, RequestDelegate handler)Parameters
urlstring- The URL pattern to match. Can include route parameters, query strings, and absolute URLs with scheme and host.
handlerRequestDelegate- The request handler to execute when the URL pattern is matched.
Returns
IEndpointConventionBuilder An IEndpointConventionBuilder for configuring the endpoint.
MapGet(string, Delegate)
HttpClientMock.MapMethods.csMaps a GET request to the specified URL pattern.
public IEndpointConventionBuilder MapGet(string url, Delegate handler)Parameters
urlstring- The URL pattern to match. Can include route parameters, query strings, and absolute URLs with scheme and host.
handlerDelegate- 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.csMaps a HEAD request to the specified URL pattern.
public IEndpointConventionBuilder MapHead(string url, RequestDelegate handler)Parameters
urlstring- The URL pattern to match. Can include route parameters, query strings, and absolute URLs with scheme and host.
handlerRequestDelegate- The request handler to execute when the URL pattern is matched.
Returns
IEndpointConventionBuilder An IEndpointConventionBuilder for configuring the endpoint.
MapHead(string, Delegate)
HttpClientMock.MapMethods.csMaps a HEAD request to the specified URL pattern.
public IEndpointConventionBuilder MapHead(string url, Delegate handler)Parameters
urlstring- The URL pattern to match. Can include route parameters, query strings, and absolute URLs with scheme and host.
handlerDelegate- 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.csMaps a OPTIONS request to the specified URL pattern.
public IEndpointConventionBuilder MapOptions(string url, RequestDelegate handler)Parameters
urlstring- The URL pattern to match. Can include route parameters, query strings, and absolute URLs with scheme and host.
handlerRequestDelegate- The request handler to execute when the URL pattern is matched.
Returns
IEndpointConventionBuilder An IEndpointConventionBuilder for configuring the endpoint.
MapOptions(string, Delegate)
HttpClientMock.MapMethods.csMaps a OPTIONS request to the specified URL pattern.
public IEndpointConventionBuilder MapOptions(string url, Delegate handler)Parameters
urlstring- The URL pattern to match. Can include route parameters, query strings, and absolute URLs with scheme and host.
handlerDelegate- 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.csMaps a PATCH request to the specified URL pattern.
public IEndpointConventionBuilder MapPatch(string url, RequestDelegate handler)Parameters
urlstring- The URL pattern to match. Can include route parameters, query strings, and absolute URLs with scheme and host.
handlerRequestDelegate- The request handler to execute when the URL pattern is matched.
Returns
IEndpointConventionBuilder An IEndpointConventionBuilder for configuring the endpoint.
MapPatch(string, Delegate)
HttpClientMock.MapMethods.csMaps a PATCH request to the specified URL pattern.
public IEndpointConventionBuilder MapPatch(string url, Delegate handler)Parameters
urlstring- The URL pattern to match. Can include route parameters, query strings, and absolute URLs with scheme and host.
handlerDelegate- 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.csMaps a POST request to the specified URL pattern.
public IEndpointConventionBuilder MapPost(string url, RequestDelegate handler)Parameters
urlstring- The URL pattern to match. Can include route parameters, query strings, and absolute URLs with scheme and host.
handlerRequestDelegate- The request handler to execute when the URL pattern is matched.
Returns
IEndpointConventionBuilder An IEndpointConventionBuilder for configuring the endpoint.
MapPost(string, Delegate)
HttpClientMock.MapMethods.csMaps a POST request to the specified URL pattern.
public IEndpointConventionBuilder MapPost(string url, Delegate handler)Parameters
urlstring- The URL pattern to match. Can include route parameters, query strings, and absolute URLs with scheme and host.
handlerDelegate- 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.csMaps a PUT request to the specified URL pattern.
public IEndpointConventionBuilder MapPut(string url, RequestDelegate handler)Parameters
urlstring- The URL pattern to match. Can include route parameters, query strings, and absolute URLs with scheme and host.
handlerRequestDelegate- The request handler to execute when the URL pattern is matched.
Returns
IEndpointConventionBuilder An IEndpointConventionBuilder for configuring the endpoint.
MapPut(string, Delegate)
HttpClientMock.MapMethods.csMaps a PUT request to the specified URL pattern.
public IEndpointConventionBuilder MapPut(string url, Delegate handler)Parameters
urlstring- The URL pattern to match. Can include route parameters, query strings, and absolute URLs with scheme and host.
handlerDelegate- The delegate to execute when the URL pattern is matched.
Returns
IEndpointConventionBuilder An IEndpointConventionBuilder for configuring the endpoint.