Ad

gock: HTTP traffic mocking and testing made easy in Go

gock allows you to easily mock and test HTTP traffic in Go. It intercepts outgoing HTTP requests and allows you to define mock responses for various scenarios, simplifying testing and runtime scenarios.
Screenshot of h2non/gock homepage

gock intercepts outgoing HTTP requests via the standard net/[http](/wg/wrk) implementation and compares them against a predefined list of mock expectations. It then resolves the request using the first matching mock. gock is designed for both unit testing and integration testing, and it provides a fluent and expressive API for defining mocks. It's inspired by nock and offers features like regular expression matching, header/body matching, and support for various HTTP methods and parameters.

gock offers a simple and expressive fluent API for defining mock HTTP responses. It handles JSON/XML response mocking with built-in helpers. Supports persistent and volatile mocks with TTL limitations. Provides full regular expression matching for HTTP requests. Integrates seamlessly with existing net/http clients and transports. Is dependency-free and highly extensible.

  • Request Matching: Matches requests based on HTTP method, URL parameters, headers, and request body content.
  • Response Definition: Defines mock responses using JSON, XML, or custom responses with status codes and bodies.
  • HTTP Interception: Intercepts outgoing HTTP requests at the net/http level, compatible with any http.Client.
  • Testability: Enables easy mocking for unit and integration tests, isolating dependencies.
  • Extensibility: Supports custom HTTP matching rules and pluggable behavior for advanced scenarios.

gock is a mature and actively maintained project with a stable API. It has a good release history, an active issue tracker, and comprehensive documentation with numerous examples. The project demonstrates good reliability, with a strong community and regular updates.

gock is valuable for developers who need to test applications that make HTTP requests. It simplifies the process of mocking external services, enabling faster and more reliable tests. This is particularly useful for isolating components, testing error handling, and simulating different network conditions. It reduces the reliance on real external dependencies during testing.

Languages:
Summarize:
Share:
Stars
2,216
Forks
109
Issues
49
Created
10 years ago
Commit
1 year ago
License
MIT
Archived
No
Updated 4 days ago

Similar Repositories