Class MockerHttpExtensions
Mocker Http Extensions Class.
Inherited Members
Namespace: FastMoq.Extensions
Assembly: FastMoq.Core.dll
Syntax
public static class MockerHttpExtensions
Methods
ConfigureHttpClient(Mocker, string, HttpStatusCode, string)
Configures the built-in provider-neutral HTTP compatibility registrations without creating a client immediately.
Declaration
public static Mocker ConfigureHttpClient(this Mocker mocker, string baseAddress = "http://localhost", HttpStatusCode statusCode = HttpStatusCode.OK, string stringContent = "[{'id':1}]")
Parameters
| Type | Name | Description |
|---|---|---|
| Mocker | mocker | The current mocker. |
| string | baseAddress | The base address applied to clients created through the built-in compatibility factory. |
| HttpStatusCode | statusCode | The default response status code used when no route-specific match is configured. |
| string | stringContent | The default response body used when no route-specific match is configured. |
Returns
| Type | Description |
|---|---|
| Mocker | The current Mocker instance. |
Remarks
Use this when a test wants the built-in IHttpClientFactory and HttpClient compatibility path to be configured first, but will resolve clients later through constructor injection or CreateClient(...) calls.
CreateHttpClient(Mocker, string, string, HttpStatusCode, string)
Creates an HTTP client backed by FastMoq-managed HTTP dependencies.
Declaration
public static HttpClient CreateHttpClient(this Mocker mocker, string clientName = "FastMoqHttpClient", string baseAddress = "http://localhost", HttpStatusCode statusCode = HttpStatusCode.OK, string stringContent = "[{'id':1}]")
Parameters
| Type | Name | Description |
|---|---|---|
| Mocker | mocker | |
| string | clientName | |
| string | baseAddress | |
| HttpStatusCode | statusCode | |
| string | stringContent |
Returns
| Type | Description |
|---|---|
| HttpClient |
Remarks
This entry point remains supported after v5.
New tests should prefer the provider-neutral request helpers in this class for HTTP behavior.
Prefer ConfigureHttpClient(Mocker, string, HttpStatusCode, string) when the test only needs to update the built-in compatibility factory and handler without creating a client immediately.
Advanced protected-member setups remain available from the Moq provider package for migration scenarios.
When the current Mocker is still using the built-in HTTP compatibility registrations,
repeated calls update that built-in handler and IHttpClientFactory path.
If the test replaces IHttpClientFactory explicitly, it owns CreateClient(...) behavior.
GetContentBytesAsync(HttpContent)
Gets the content bytes.
Declaration
public static Task<byte[]> GetContentBytesAsync(this HttpContent content)
Parameters
| Type | Name | Description |
|---|---|---|
| HttpContent | content | The content. |
Returns
| Type | Description |
|---|---|
| Task<byte[]> | byte[]. |
GetContentStreamAsync(HttpContent)
Gets the content stream.
Declaration
public static Task<Stream> GetContentStreamAsync(this HttpContent content)
Parameters
| Type | Name | Description |
|---|---|---|
| HttpContent | content | The content. |
Returns
| Type | Description |
|---|---|
| Task<Stream> | System.IO.Stream. |
WhenHttpRequest(Mocker, Func<HttpRequestMessage, bool>, Func<HttpResponseMessage>)
Configures a provider-neutral HTTP response for requests that match the supplied predicate.
Declaration
public static void WhenHttpRequest(this Mocker mocker, Func<HttpRequestMessage, bool> requestPredicate, Func<HttpResponseMessage> responseFactory)
Parameters
| Type | Name | Description |
|---|---|---|
| Mocker | mocker | The mocker. |
| Func<HttpRequestMessage, bool> | requestPredicate | Predicate used to match an outgoing request. |
| Func<HttpResponseMessage> | responseFactory | Factory used to create the HTTP response for a matching request. |
WhenHttpRequest(Mocker, HttpMethod, string, Func<HttpResponseMessage>)
Configures a provider-neutral HTTP response for a specific method and request URI or path.
Declaration
public static void WhenHttpRequest(this Mocker mocker, HttpMethod method, string requestUriOrPath, Func<HttpResponseMessage> responseFactory)
Parameters
| Type | Name | Description |
|---|---|---|
| Mocker | mocker | The mocker. |
| HttpMethod | method | The expected HTTP method. |
| string | requestUriOrPath | The absolute URI or relative path to match. |
| Func<HttpResponseMessage> | responseFactory | Factory used to create the HTTP response for a matching request. |
WhenHttpRequestJson(Mocker, HttpMethod, string, string, HttpStatusCode)
Configures a provider-neutral JSON response for a specific method and request URI or path.
Declaration
public static void WhenHttpRequestJson(this Mocker mocker, HttpMethod method, string requestUriOrPath, string json, HttpStatusCode statusCode = HttpStatusCode.OK)
Parameters
| Type | Name | Description |
|---|---|---|
| Mocker | mocker | The mocker. |
| HttpMethod | method | The expected HTTP method. |
| string | requestUriOrPath | The absolute URI or relative path to match. |
| string | json | The JSON payload returned to matching requests. |
| HttpStatusCode | statusCode | The response status code. |