Motivation
Today HttpMock can only hand a client out: GetClient(), GetClientFactory() and GetMessageHandler(). There is no supported way to push the mock into an existing DI container.
That makes the most common integration-test scenario awkward. If the system under test resolves a typed client through IHttpClientFactory, or lives behind a WebApplicationFactory, you have to hand-roll handler replacement yourself.
Proposed API
A separate package (e.g. Mockly.Extensions.Http) so the core package stays dependency-free and net472-friendly:
// Replace the primary handler of a single named or typed client
services.AddHttpClient<IGitHubClient, GitHubClient>().AddMocklyHandler(mock);
services.AddHttpClient("github").AddMocklyHandler(mock);
// Replace the primary handler of every registered client
services.AddMockly(mock);
// ASP.NET Core integration tests
var factory = new WebApplicationFactory<Program>().WithMockly(mock);
Notes
- Should keep any
DelegatingHandlers registered by the application in place, so resilience/retry pipelines (Microsoft.Extensions.Http.Resilience, Polly) are still exercised. Only the primary handler is swapped.
- Needs to work with both typed and named clients.
- Belongs in a separate package: it takes a dependency on
Microsoft.Extensions.Http (and optionally Microsoft.AspNetCore.Mvc.Testing), neither of which should leak into the core package.
Motivation
Today
HttpMockcan only hand a client out:GetClient(),GetClientFactory()andGetMessageHandler(). There is no supported way to push the mock into an existing DI container.That makes the most common integration-test scenario awkward. If the system under test resolves a typed client through
IHttpClientFactory, or lives behind aWebApplicationFactory, you have to hand-roll handler replacement yourself.Proposed API
A separate package (e.g.
Mockly.Extensions.Http) so the core package stays dependency-free andnet472-friendly:Notes
DelegatingHandlers registered by the application in place, so resilience/retry pipelines (Microsoft.Extensions.Http.Resilience, Polly) are still exercised. Only the primary handler is swapped.Microsoft.Extensions.Http(and optionallyMicrosoft.AspNetCore.Mvc.Testing), neither of which should leak into the core package.