urllib3-mock

0.3.3 · abandoned · verified Fri Apr 17

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.

Common errors

Warnings

Install

Imports

Quickstart

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()

view raw JSON →