A utility library for mocking out the `urllib3` Python library. It allows intercepting HTTP requests made by `urllib3` and returning predefined responses for testing or development. The current version is 0.3.3, and the library appears to be abandoned, with no updates since 2017.
pip install urllib3-mockVerified import paths — ran on the pinned version, not inferred.
This example demonstrates how to use `urllib3-mock` with a decorator to intercept a GET request to `http://example.com/` and return a custom body. It highlights the necessity of explicitly calling the decorated function for the mock to activate and the request to be intercepted.
For actively maintained mocking libraries for HTTP requests, consider `responses` (which targets `requests` and `urllib3`) or `pytest-mock` for more general mocking capabilities within a testing framework.
If your primary library for HTTP requests is `requests`, consider using `responses` (https://github.com/getsentry/responses) which is designed to mock `requests` directly and is actively maintained.
Always ensure the function decorated with `@responses.add` or `@responses.activate` is invoked. Alternatively, use `responses.start()` and `responses.stop()` as a context manager (`with responses:`) or manually around the `urllib3` call.
Verify that the URL, HTTP method (GET, POST, PUT, etc.), and any required headers in your `@responses.add` configuration exactly match the outgoing `urllib3` request. Also, ensure the mock context is active (either via a called decorator or `responses.start()/stop()`).
If using decorators, ensure the decorated function is explicitly called. If using `responses.start()`/`responses.stop()`, verify that the `urllib3` request is made between these calls or within a `with responses:` block.