A testing layer for Microsoft's HttpClient library that creates canned responses using a fluent API.
MockHttp is a testing library for Microsoft's HttpClient that provides a mock HttpMessageHandler with a fluent API for configuring canned HTTP responses. It solves the problem of testing code that makes HTTP calls without requiring actual network connections or external services.
.NET developers writing unit or integration tests for code that uses HttpClient, particularly those testing service layers, API clients, or HTTP-dependent components.
Developers choose MockHttp because it offers a comprehensive, fluent API for mocking HttpClient responses with precise request matching, verification capabilities, and seamless integration into existing test suites without modifying application code.
Testing layer for Microsoft's HttpClient library. Create canned responses using a fluent API.
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
The library offers a chainable, expressive syntax for defining mock responses, as shown in examples like mockHttp.When("http://localhost/api/user/*").Respond("application/json", "{'name' : 'Test McGee'}"), making test setup intuitive and readable.
Provides built-in matchers for URLs, query strings, headers, JSON content, and custom logic, enabling precise control over which HTTP requests are mocked, as detailed in the Matchers table in the README.
Includes methods like VerifyNoOutstandingExpectation() and GetMatchCount() to assert that expected requests were made, enhancing test reliability, as demonstrated in the verification examples.
Acts as a drop-in replacement for HttpMessageHandler without modifying application code, allowing easy injection into HttpClient, as highlighted in the 'How?' section of the README.
It only works with Microsoft's HttpClient, so projects using other HTTP libraries or legacy WebClient-based code cannot leverage MockHttp, restricting its applicability.
The distinction between When (backend definitions) and Expect (request expectations), along with match behavior logic, can be confusing and error-prone in complex test scenarios, requiring careful study of the documentation.
Version 7.0.0 and later require .NET 6 or higher, dropping support for older frameworks like .NET Framework 4.x, which may force upgrades for teams on legacy systems, as noted in the Platform Support section.