{"id":2533,"library":"httpretty","title":"HTTPretty","description":"HTTPretty is an HTTP client mocking tool for Python (current version 1.1.4) that works by monkey-patching the standard library's `socket` and `ssl` modules. This allows it to intercept HTTP requests at a low level, faking responses for any HTTP client that relies on these modules, such as `requests` or `urllib3`. It is suitable for testing API integrations and handling external service dependencies. Releases are somewhat irregular but include bug fixes and Python version support updates.","status":"active","version":"1.1.4","language":"en","source_language":"en","source_url":"https://github.com/gabrielfalcao/HTTPretty","tags":["mocking","http","testing","api","network"],"install":[{"cmd":"pip install httpretty","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Commonly used HTTP client library in examples and real-world usage with httpretty.","package":"requests","optional":true},{"reason":"HTTPretty interacts at the socket level, and urllib3 is a fundamental HTTP client that many libraries (including requests) build upon. Recent versions (>=2.3.0) have known incompatibilities.","package":"urllib3","optional":false},{"reason":"Used in some official examples for fluent assertions.","package":"sure","optional":true}],"imports":[{"symbol":"httpretty","correct":"import httpretty"}],"quickstart":{"code":"import requests\nimport httpretty\n\n# httpretty monkey-patches the socket module.\n# The @httpretty.activate decorator ensures it's enabled for the test function\n# and disabled afterwards, isolating the mock.\n@httpretty.activate(verbose=False, allow_net_connect=False)\ndef quickstart_example():\n    test_url = \"http://example.com/api/data\"\n    mock_body = '{\"message\": \"Hello from mock!\"}'\n    \n    # Register a URI to mock with GET method and a JSON body\n    httpretty.register_uri(\n        httpretty.GET,\n        test_url,\n        body=mock_body,\n        status=200,\n        content_type=\"application/json\"\n    )\n\n    # Make a request using a common HTTP client (e.g., requests)\n    response = requests.get(test_url)\n\n    # Assertions on the response\n    assert response.status_code == 200\n    assert response.json() == {\"message\": \"Hello from mock!\"}\n    \n    # Assertions on the intercepted request itself\n    assert httpretty.last_request().method == \"GET\"\n    assert httpretty.last_request().path == \"/api/data\"\n    print(\"Mocked request successful!\")\n\n# To run the example (e.g., in a script or interactive session)\nif __name__ == \"__main__\":\n    quickstart_example()","lang":"python","description":"This example demonstrates how to use the `@httpretty.activate` decorator to mock an HTTP GET request to `http://example.com/api/data`. It registers a URI with a specific JSON response body and status code, then uses the `requests` library to make the call. Assertions verify both the received response and properties of the intercepted request. The `allow_net_connect=False` argument prevents accidental real network connections during tests."},"warnings":[{"fix":"Upgrade to Python 3 or pin httpretty to a version less than 1.0.0.","message":"HTTPretty dropped support for Python 2 in version 1.0.0. Projects still on Python 2 must use an older version.","severity":"breaking","affected_versions":"<1.0.0 to 1.0.0+"},{"fix":"Use the `@httpretty.activate` decorator for unit tests or ensure `httpretty.enable()` is paired with `httpretty.disable()` in a controlled scope.","message":"Failing to disable HTTPretty after tests can lead to unexpected behavior in subsequent code or tests, as it monkey-patches the global socket module. Always use the `@httpretty.activate` decorator for test functions or explicitly call `httpretty.enable()` and `httpretty.disable()` in a `try...finally` block.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure exact URL matching or use regular expressions (e.g., `re.compile(r'http://example.com/api/data/?')`) for more flexible matching.","message":"URL matching with `httpretty.register_uri` can be very strict. Minor differences, such as a missing trailing slash, can prevent the mock from being matched, leading to an `httpretty.errors.UnmockedError` if `allow_net_connect` is `False`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Pin `urllib3` to a version less than 2.3.0 (e.g., `<2.3.0`) or consider alternative mocking libraries if this is a blocker.","message":"HTTPretty has known compatibility issues with `urllib3` versions 2.3.0 and higher when `allow_net_connect=False`. This can lead to `UnmockedError` or other unexpected networking issues, as `httpretty` may fail to intercept requests correctly.","severity":"breaking","affected_versions":"1.1.x with urllib3>=2.3.0"},{"fix":"Verify that your specific HTTP client library and usage pattern are compatible with httpretty's monkey-patching. This may require reviewing `httpretty`'s issue tracker for known client-specific incompatibilities.","message":"In certain scenarios, HTTPretty may not correctly intercept requests made using Python's `http.client.HTTPConnection`, which can lead to `socket.gaierror` (getaddrinfo error) if the real network connection is attempted and fails.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-10T00:00:00.000Z","next_check":"2026-07-09T00:00:00.000Z"}