{"id":8437,"library":"pook","title":"pook - HTTP traffic mocking","description":"pook is an HTTP traffic mocking library for Python, enabling easy setup of expectations and interception of requests from popular HTTP clients like `requests`, `httpx`, and `aiohttp`. It supports various matching criteria for requests and provides flexible response definitions. The current version is 2.1.6, and it typically sees several patch releases per major version, though releases can sometimes be delayed.","status":"active","version":"2.1.6","language":"en","source_language":"en","source_url":"https://github.com/h2non/pook","tags":["http","mocking","testing","requests","httpx","aiohttp"],"install":[{"cmd":"pip install pook","lang":"bash","label":"Install pook"}],"dependencies":[],"imports":[{"symbol":"pook","correct":"import pook"},{"note":"Decorator to activate pook for a test function","symbol":"on","correct":"import pook\n\n@pook.on"},{"note":"Context manager to activate pook within a block","symbol":"use","correct":"import pook\n\nwith pook.use():"}],"quickstart":{"code":"import pook\nimport requests\nimport os\n\n# Configure a mock for a GET request\npook.get('https://api.example.com/data', reply=200, response_json={'status': 'ok', 'data': [1, 2, 3]})\n\n# Activate pook (can also use @pook.on decorator or with pook.use():)\npook.activate()\n\ntry:\n    # Make a request using requests, which pook will intercept\n    response = requests.get('https://api.example.com/data')\n\n    print(f\"Status Code: {response.status_code}\")\n    print(f\"Response JSON: {response.json()}\")\n\n    assert response.status_code == 200\n    assert response.json() == {'status': 'ok', 'data': [1, 2, 3]}\n\nfinally:\n    # Always deactivate pook and clear mocks to avoid leakage\n    pook.off()\n    pook.flush()\n","lang":"python","description":"This quickstart demonstrates how to mock an HTTP GET request using `pook` with the `requests` library. It sets up an expectation for a specific URL, activates the mock, makes the request, and then asserts the mocked response. Remember to deactivate and flush mocks to prevent state leakage between tests."},"warnings":[{"fix":"Remove the `binary` parameter from `pook.response().body()`. Ensure `chunked` is passed as a keyword argument (e.g., `body('content', chunked=True)`).","message":"The `Response::body`'s `binary` parameter was removed, and `chunked` is now an enforced keyword-only argument. Responses are now always byte-encoded.","severity":"breaking","affected_versions":">=2.0.0"},{"fix":"Upgrade your Python environment to 3.10 or newer. For projects requiring older Python versions, pin `pook<2.1.0` (for Python 3.8) or `pook<2.1.5` (for Python 3.9/PyPy 3.10).","message":"Support for Python 3.9 and PyPy 3.10 was dropped in version 2.1.5. Support for Python 3.8 was dropped in version 2.1.0.","severity":"breaking","affected_versions":">=2.1.0, >=2.1.5"},{"fix":"Upgrade to `pook` version 2.1.1 or newer. This version includes a fix to automatically flush mocks when `pook.activate` is used as a decorator or context manager. Always ensure `pook.flush()` is called after each test or test suite runs.","message":"Mocks might leak between test cases when `pook.activate()` was used as a wrapper, potentially leading to confusing test failures or unexpected behavior.","severity":"gotcha","affected_versions":"<2.1.1"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Carefully review your mock definition (`pook.get('url', ...)`) and the actual outgoing request to ensure all criteria (URL, method, headers, query, body) exactly match. Ensure `pook` is activated for the scope of the request.","cause":"The outgoing HTTP request did not match any of the currently active mocks. This can be due to discrepancies in URL, method, headers, query parameters, or request body.","error":"pook.errors.NoMatches: No matching mock for request: GET https://example.com/data"},{"fix":"Remove the `binary=True/False` argument from your `pook.response().body()` call. `pook` now handles byte encoding automatically.","cause":"You are using `pook` version 2.0.0 or higher, which removed the `binary` parameter from the `Response.body()` method.","error":"TypeError: Response.body() got an unexpected keyword argument 'binary'"},{"fix":"Ensure all mocks you define are actually hit by an outgoing HTTP request. If a mock is optional or should not trigger an error if uncalled, use `times(0)` or `persist()` as appropriate.","cause":"A mock expectation was set and `pook` was activated, but the corresponding HTTP request that would satisfy this mock was never made within the active scope.","error":"RuntimeError: There is an activated pook mock that has not been called: GET https://example.com/uncalled"},{"fix":"Install `pook` using pip: `pip install pook`. If using a virtual environment, ensure it is activated.","cause":"The `pook` library is not installed in the Python environment you are currently using.","error":"ModuleNotFoundError: No module named 'pook'"}]}