一组用 Moq 模拟 HttpClient 和 IHttpClientFactory 的扩展方法。
A set of extension methods for mocking HttpClient and IHttpClientFactory with Moq.
Mocking HttpClient has historically been surprisingly difficult, with the solution being to either create a wrapper to mock instead (at the cost of cluttering the code) or use a separate HTTP library entirely. This package provides extension methods for Moq that make handling HTTP requests as easy as mocking a service method.
Install-Package Moq.Contrib.HttpClient
or dotnet add package Moq.Contrib.HttpClient
The library adds request/response variants of the standard Moq methods:
All Setup and Verify helpers have the same overloads, abbreviated here:
SetupAnyRequest()
SetupRequest([HttpMethod method, ]Predicate match)
SetupRequest(string|Uri requestUrl[, Predicate match])
SetupRequest(HttpMethod method, string|Uri requestUrl[, Predicate match])requestUrl matches the exact request URL, while the match predicate allows
for more intricate matching, such as by query parameters or headers, and may be
async as well to inspect the request body.
The response helpers simplify sending a StringContent, JsonContent (using System.Text.Json), ByteArrayContent, StreamContent, or just a status code:
ReturnsResponse(HttpStatusCode statusCode[, HttpContent content], Action configure = null)
ReturnsResponse([HttpStatusCode statusCode, ]string content, string mediaType = null, Encoding encoding = null, Action configure = null))
ReturnsResponse([HttpStatusCode statusCode, ]byte[]|Stream content, string mediaType = null, Action configure = null)
ReturnsJsonResponse([HttpStatusCode statusCode, ]T value, JsonSerializerOptions options = null, Action configure = null)The statusCode defaults to 200 OK if omitted, and the configure action can
be used to set response headers.
…Why you should use MockBehavior.Strict for HttpClient
Consider the following:
handler.SetupRequest(HttpMethod.Get, "https://example.com/api/foos")
.ReturnsJsonResponse(expected);
List actual = await foosService.GetFoos();
actual.Should().BeEquivalentTo(expected);This test fails unexpectedly with the following exception:
System.InvalidOperationException : Handler did not return a response message.This is because Moq defaults to Loose mode which returns a default value if no setup matches, but HttpClient throws an InvalidOperationException if it receives null from the handler.
If we change it to MockBehavior.Strict:
- var handler = new Mock();
+ var handler = new Mock(MockBehavior.Strict);We get a more useful exception that also includes the request that was made (here we see the URL was typo'd as "foo" instead of "foos"):
Moq.MockException : HttpMessageHandler.SendAsync(Method: GET, RequestUri: 'https://example.com/api/foo', Version: 1.1, Content: , Headers:
{
}, System.Threading.CancellationToken) invocation failed with mock behavior Strict.
All invocations on the mock must have a corresponding setup.…The last example uses a URL builder library called Flurl to assist in checking the query string.
See "MatchesCustomPredicate" and "MatchesQueryParameters" in the request extension tests for further explanation, including various ways to inspect JSON requests.
Moq has two types of sequences:
SetupSequence() which creates one setup that returns values in sequence,
andInSequence().Setup() which creates multiple setups under When()
conditions to ensure that they only match in order.Both of these are supported; however, as with service methods, regular setups are generally most appropriate. The latter type can be useful, though, for cases where separate requests independent of each other (that is, not relying on information returned from the previous) must be made in a certain order.
See the sequence extensions tests for examples.
The normal Returns method can be used together with the request helpers for more complex responses:
handler.SetupRequest("https://example.com/hello")
.Returns(async (HttpRequestMessage request, CancellationToken _) => new HttpResponseMessage()
{
Content = new StringContent($"Hello, {await request.Content.ReadAsStringAsync()}")
});
var response = await client.PostAsync("https://example.com/hello", new StringContent("world"));
var body = await response.Content.ReadAsStringAsync(); // Hello, worldIt's common to see HttpClient wrapped in a using since it's IDisposable, but
this is, rather counterintuitively, incorrect and can lead to the application
eating up sockets. The standard advice is to reuse a single
HttpClient, yet this has the drawback of not responding to DNS changes.
ASP.NET Core introduces an IHttpClientFactory which "manages the pooling and lifetime of underlying HttpClientMessageHandler instances to avoid common DNS problems that occur when manually managing HttpClient lifetimes." As a bonus, it also makes HttpClient's ability to plug in middleware more accessible — for example, using Polly to automatically handle retries and failures.
If your classes simply receive an HttpClient injected via IHttpClientFactory, the tests don't need to do anything different. If the constructor takes the factory itself instead, this can be mocked the same way:
var handler = new Mock();
var factory = handler.CreateClientFactory();This factory can then be passed into the class or injected via
AutoMocker, and code calling factory.CreateClient() will receive
clients backed by the mock handler.
The CreateClientFactory() extension method returns a mock that's already set
up to return a default client. If you're using named clients, a setup can be
added like so:
// Configuring a named client (overriding the default)
Mock.Get(factory).Setup(x => x.CreateClient("api"))
.Returns(() =>
{
var client = handler.CreateClient();
client.BaseAddress = ApiBaseUrl;
return client;
});Note: If you're getting a "Extension methods (here: HttpClientFactoryExtensions.CreateClient) may not be used in setup / verification expressions." error, make sure you're passing a string where it says
"api"in the example.
For integration tests, rather than replace the IHttpClientFactory implementation in the service collection, it's possible to leverage the existing DI infrastructure and configure it to use a mock handler as the "primary" instead:
…This way, the integration tests use the same dependency injection and HttpClient
configurations from ConfigureServices() (or Program.cs) as would be used in
production.
See this sample ASP.NET Core app and its integration test for a working example.
The library's own unit tests have been written to serve as examples of the various helpers and different use cases:
MIT
暂无开放 Issues,或尚未同步最近议题。