A Go library for easy mocking of HTTP responses from external resources in tests.
httpmock is a Go library for mocking HTTP responses from external APIs and services in tests. It allows developers to isolate their code from network dependencies, enabling faster and more reliable unit tests by simulating API interactions.
Go developers writing unit tests for applications that make HTTP requests to external services, such as REST APIs or third-party integrations.
Developers choose httpmock for its simplicity, intuitive API, and seamless integration with Go's standard testing package, go-testdeep/tdsuite, and Ginkgo, without adding unnecessary complexity.
HTTP mocking for Golang
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Supports both exact URLs and regex patterns, enabling precise or broad mocking scenarios as shown in the simple example with `=~^https://api\.mybiz\.com/articles/id/\d+\z`.
Provides helpers like NewStringResponder and NewJsonResponder to quickly mock responses with custom status codes, headers, and bodies, reducing boilerplate in tests.
Integrates naturally with Go's standard testing package, go-testdeep/tdsuite, and Ginkgo, with clear examples for each framework in the README.
Allows monitoring of HTTP call counts and details via GetCallCountInfo, useful for assertions in unit tests to verify external API interactions.
Requires additional setup for custom HTTP clients like Resty, as shown in the Ginkgo example with ActivateNonDefault, adding complexity and potential for errors.
Only handles HTTP requests, so projects using other protocols such as gRPC must find separate mocking solutions, increasing dependency management.
The README doesn't address thread-safety or handling concurrent requests, which could lead to flaky tests in parallel testing environments.