A Crystal library for stubbing HTTP::Client requests in tests, inspired by the Ruby webmock gem.
webmock.cr is a Crystal library that mocks HTTP::Client requests for testing purposes. It intercepts HTTP requests made by Crystal applications and returns predefined responses, allowing developers to test code that depends on external APIs without making actual network calls. The library is specifically designed for the Crystal programming language's HTTP client.
Crystal developers writing tests for applications that make HTTP requests, particularly those needing to mock external API dependencies in unit or integration tests.
Developers choose webmock.cr because it provides a familiar, webmock-inspired API for Crystal, integrates seamlessly with Crystal's HTTP::Client, and offers fine-grained control over request matching and response generation without requiring external dependencies.
Mock HTTP::Client
Open-Awesome is built by the community, for the community. Submit a project, suggest an awesome list, or help improve the catalog on GitHub.
Directly hooks into Crystal's HTTP::Client and Spec testing framework, as shown in examples with HTTP::Client.get/post and Spec.before_each for easy test setup.
Allows using blocks to generate responses based on request data, enabling complex mocking logic like reversing the request body, as demonstrated in the README.
Blocks unregistered HTTP requests by default, raising WebMock::NetConnectNotAllowedError to prevent accidental real network calls and ensure test isolation.
Supports matching on method, URI, body, headers, and query strings, providing fine-grained control for stubbing specific API interactions.
The README notes a 'Todo' to bring more features from the Ruby webmock gem, limiting advanced mocking capabilities like request callbacks or detailed logging.
Requires explicit resetting of stubs between tests using WebMock.reset or wrap blocks, which can be error-prone if forgotten in complex test suites.
Only mocks requests made via Crystal's standard HTTP::Client, so it may not work with alternative HTTP libraries or low-level networking code in Crystal applications.