Registry / testing / urllib3-mock

urllib3-mock

JSON →
library0.3.3pypypi✓ verified 87d ago

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-mock
INSTALL
IMPORT
SIG · URLLIB3-MOCK
U
urllib3-mock
testingpythonv0.3.3
Install
1.5s avg
Import
373ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.3.3 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.910 runs
installs and imports cleanly · install 0.0s · import 0.404s · 17.8MB
glibc
py 3.103.910 runs
installs and imports cleanly · install 1.5s · import 0.342s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Responses
from urllib3_mock import Responses

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.

from urllib3_mock import Responses import urllib3 # Create an instance of Responses responses = Responses() @responses.add('GET', 'http://example.com/', body='hello world') def test_mocked_request(): """This function contains the urllib3 call that will be mocked.""" http = urllib3.PoolManager() r = http.request('GET', 'http://example.com/') print(f"Received: {r.data.decode('utf-8')}") assert r.data == b'hello world' # It's crucial to call the decorated function to execute the mocked request test_mocked_request()
Debug
Known issues
deprecatedThe `urllib3-mock` library is no longer actively maintained, with the last commit and release (v0.3.3) dating back to 2017. Users should be aware of potential compatibility issues with newer Python versions or `urllib3` versions.
fix
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.
affects: 0.x
gotchaThis library specifically hooks into `urllib3` directly. While `requests` uses `urllib3` internally, `urllib3-mock` might not always intercept requests made via the `requests` library in the way you expect, especially with more complex `requests` features.
fix
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.
affects: 0.x
gotchaWhen using decorators (e.g., `@responses.add`), the decorated function *must* be explicitly called for the mock to become active and intercept requests. If the function is not called, the `urllib3` request will go to the actual network.
fix
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.
affects: 0.x
Errors
Common errors & fixes
urllib3.exceptions.MaxRetryError: HTTPConnectionPool(...) Max retries exceeded with url: ... (Caused by NewConnectionError('<urllib3.connection.HTTPConnection object at ...>: Failed to establish a new connection: [Errno 111] Connection refused'))
The `urllib3` request was not intercepted by `urllib3-mock` because no matching mock response was active or configured for the specific URL and HTTP method.
fix
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()`).
AssertionError: Expected the request to be mocked, but it went to the network.
The test or application code making the `urllib3` request did not execute within the active scope of the `urllib3-mock` `Responses` instance.
fix
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.
Upgrade
Version history
0.3.3latest on PyPI · released Apr 16, 2015
Audit
Dependencies
urllib3requiredRequired for the library to be functional, as its entire purpose is to mock `urllib3` requests. Although not a strict `install_requires` dependency, it is essential for practical use.
Agent activity
23 hits · last 30 days
node
20
Amazon
1
OpenAI (training)
1
Resources