Versatile HTTP traffic mocking and testing library for Go, compatible with any net/http based client.
gock is a comprehensive HTTP mocking library for Go that simplifies testing and runtime scenarios by intercepting and mocking HTTP requests. It provides a fluent API for declarative mock definitions and works seamlessly with any net/http compatible client, allowing developers to simulate network interactions without actual HTTP calls.
Go developers who need to test applications that make HTTP requests, particularly those writing unit or integration tests for HTTP clients, APIs, or services that rely on external network calls.
Developers choose gock for its expressive, fluent API that reduces boilerplate, its ability to intercept any net/http client via http.RoundTripper, and its flexibility in supporting both testing and runtime mocking with features like TTL-limited mocks and dynamic switching between mock and real networking modes.
HTTP traffic mocking and testing made easy in Go ༼ʘ̚ل͜ʘ̚༽
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Provides a simple, declarative DSL for mocking HTTP requests, reducing boilerplate code as shown in the README examples with chainable methods like gock.New().Get().Reply().
Supports full regex matching on method, URL, headers, params, and bodies, allowing precise mock definitions, such as matching JSON payloads or specific header values.
Enables dynamic switching between mock and real networking modes via gock.EnableNetworking(), useful for both testing and production debugging scenarios.
Has no external dependencies, keeping the library lightweight and easy to integrate into Go projects without bloating the dependency tree.
The README explicitly states that gock is not fully thread-safe, requiring careful handling in concurrent test scenarios to avoid race conditions, which can be a risk for parallel testing.
Requires explicit ordering of mocks (complex ones first) and cleanup with gock.Off() to prevent side effects, adding cognitive load and potential for errors in test suites.
Only mocks HTTP requests via net/http, making it unsuitable for projects using other transport layers like gRPC or WebSockets without additional tooling.