Skip to content

Adds fluent HttpClient mocking support#434

Merged
Keboo merged 5 commits intomasterfrom
httpClientSupport
Feb 13, 2026
Merged

Adds fluent HttpClient mocking support#434
Keboo merged 5 commits intomasterfrom
httpClientSupport

Conversation

@Keboo
Copy link
Collaborator

@Keboo Keboo commented Feb 13, 2026

Summary

Introduces comprehensive and fluent support for mocking HttpClient and HttpMessageHandler within Moq.AutoMock, significantly simplifying the testing of HTTP-dependent code.

Changes Made

  • Automatic HttpClient Resolution: An HttpClientResolver has been added to AutoMocker to automatically provide HttpClient instances backed by a configurable HttpMessageHandler.
  • Fluent API for HTTP Verbs: New extension methods (e.g., SetupHttpGet, SetupHttpPost, SetupHttpPut, SetupHttpDelete, SetupHttpHead) allow for easy setup of specific HTTP requests based on URL, request content, or custom predicates.
  • Flexible Response Configuration: Extended ReturnsResponse methods support various response types (plain text, JSON, byte arrays, streams) with custom status codes and headers. Stream-based responses are handled to ensure reusability across multiple requests.
  • Request Verification: VerifyHttpGet, VerifyHttpPost, VerifyHttpPut, VerifyHttpDelete, VerifyHttpHead extensions are included for verifying specific HTTP request invocations.
  • Sequence Setup: Supports defining a sequence of responses for repeated HTTP calls.
  • Advanced Request Matching: Introduced RequestMatcher and RequestPredicate for more robust and efficient matching of HttpRequestMessage objects.

Why These Changes

These changes aim to streamline the unit testing experience for applications consuming external HTTP services. By providing a rich, fluent, and integrated mocking API for HttpClient and HttpMessageHandler, developers can more easily isolate and test their code without making actual network calls, leading to faster and more reliable tests. The automatic resolution of HttpClient further enhances the seamless integration with AutoMocker's dependency injection capabilities.

How to Test

The added unit tests in Moq.AutoMock.Tests/DescribeHttpClient.cs and Moq.AutoMock.Tests/DescribeHttpMessageHandler.cs demonstrate the usage and verify the functionality of the new HTTP mocking capabilities. These tests can be run to confirm the feature works as expected.

Checklist

  • Unit tests added/updated
  • Documentation added/updated
  • Breaking changes
  • Release notes updated

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant