Description
<h2 align="center">Send responses to HTTPX using pytest</h2> <p align="center"> <a href="https://pypi.org/project/pytest-httpx/"><img alt="pypi version" src="https://img.shields.io/pypi/v/pytest_httpx"></a> <a href="https://github.com/Colin-b/pytest_httpx/actions"><img alt="Build status" src="https://github.com/Colin-b/pytest_httpx/workflows/Release/badge.svg"></a> <a href="https://github.com/Colin-b/pytest_httpx/actions"><img alt="Coverage" src="https://img.shields.io/badge/coverage-100%25-brightgreen"></a> <a href="https://github.com/psf/black"><img alt="Code style: black" src="https://img.shields.io/badge/code%20style-black-000000.svg"></a> <a href="https://github.com/Colin-b/pytest_httpx/actions"><img alt="Number of tests" src="https://img.shields.io/badge/tests-298 passed-blue"></a> <a href="https://pypi.org/project/pytest-httpx/"><img alt="Number of downloads" src="https://img.shields.io/pypi/dm/pytest_httpx"></a> </p> > [!NOTE] > Version 1.0.0 will be released once httpx is considered as stable (release of 1.0.0). > > However, current state can be considered as stable. Once installed, `httpx_mock` [`pytest`](https://docs.pytest.org/en/latest/) fixture will make sure every [`httpx`](https://www.python-httpx.org) request will be replied to with user provided responses ([unless some hosts are explicitly skipped](#do-not-mock-some-requests)). - [Add responses](#add-responses) - [JSON body](#add-json-response) - [Custom body](#reply-with-custom-body) - [Multipart body (files, ...)](#add-multipart-response) - [HTTP status code](#add-non-200-response) - [HTTP headers](#reply-with-custom-headers) - [HTTP/2.0](#add-http/2.0-response) - [Add dynamic responses](#dynamic-responses) - [Raising exceptions](#raising-exceptions) - [Check requests](#check-sent-requests) - [Configuration](#configuring-httpx_mock) - [Register more responses than requested](#allow-to-register-more-responses-than-what-will-be-requested) - [Register less responses than requested](#allow-to-not-register-responses-for-every-request) - [Allow to register a response for more than one request](#allow-to-register-a-response-for-more-than-one-request) - [Do not mock some requests](#do-not-mock-some-requests) - [Migrating](#migrating-to-pytest-httpx) - [responses](#from-responses) - [aioresponses](#from-aioresponses) ## Add responses You can register responses for both sync and async [`HTTPX`](https://www.python-httpx.org) requests. ```python import pytest import httpx def test_something(httpx_mock): httpx_mock.add_response() with httpx.Client() as client: response = client.get("https://test_url") @pytest.mark.asyncio async def test_something_async(httpx_mock): httpx_mock.add_response() async with httpx.AsyncClient() as client: response = await client.get("https://test_url") ``` If all registered responses are not sent back during test execution, the test case will fail at teardown [(unless you turned `assert_all_responses_were_requested` option off)](#allow-to-register-more-responses-than-what-will-be-requested). Default response is a `HTTP/1.1` `200 (OK)` without any body. ### How response is selected In case more than one response match request, the first one not yet sent (according to the registration order) will be sent. In case all matching responses have been sent once, the request will [not be considered as matched](#in-case-no-response-can-be-found) [(unless you turned `can_send_already_matched_responses` option on)](#allow-to-register-a-response-for-more-than-one-request). You can add criteria so that response will be sent only in case of a more specific matching. #### Matching on URL `url` parameter can either be a string, a python [re.Pattern](https://docs.python.org/3/library/re.html) instance or a [httpx.URL](https://www.python-httpx.org/api/#url) instance. Matching is performed on the full URL, query parameters included. Order of parameters in the query string does not matter, however order of values do matter if the same parameter is provided more than once. ```python import httpx import re from pytest_httpx import HTTPXMock def test_url(httpx_mock: HTTPXMock): httpx_mock.add_response(url="https://test_url?a=1&b=2") with httpx.Client() as client: response1 = client.delete("https://test_url?a=1&b=2") response2 = client.get("https://test_url?b=2&a=1") def test_url_as_pattern(httpx_mock: HTTPXMock): httpx_mock.add_response(url=re.compile(".*test.*")) with httpx.Client() as client: response = client.get("https://test_url") def test_url_as_httpx_url(httpx_mock: HTTPXMock): httpx_mock.add_response(url=httpx.URL("https://test_url", params={"a": "1", "b": "2"})) with httpx.Client() as client: response = client.get("https://test_url?a=1&b=2") ``` ##### Ignoring query parameters Use a python [re.Pattern](https://docs.python.org/3/library/re.html) instance to ignore query parameters while matchi
Release History
| Version | Changes | Urgency | Date |
|---|---|---|---|
| 0.36.2 | Imported from PyPI (0.36.2) | Low | 4/21/2026 |
| v0.36.0 | ### Changed - `pytest` required version is now `9`. ### Added - Explicit support for python `3.14`. - `match_params` parameter is now available on responses and callbacks registration, as well as request(s) retrieval. Allowing to provide query parameters as a dict instead of being part of the matched URL. - This parameter allows to perform partial query params matching ([refer to documentation](README.md#matching-on-query-parameters) for more information). ### Fixed - URL with more | Low | 12/2/2025 |
| v0.35.0 | ### Changed - Requires [`httpx`](https://www.python-httpx.org)==0.28.\* | Low | 11/28/2024 |
| v0.34.0 | ### Added - `is_optional` parameter is now available on responses and callbacks registration. Allowing to add optional responses while keeping other responses as mandatory. Refer to documentation for more details. - `is_reusable` parameter is now available on responses and callbacks registration. Allowing to add multi-match responses while keeping other responses as single-match. Refer to documentation for more details. ### Fixed - `httpx_mock.get_request` will now also propose to refine f | Low | 11/18/2024 |
| v0.33.0 | ### Added - Explicit support for python `3.13`. - `should_mock` option (callable returning a boolean) is now available, defaulting to always returning `True`. Refer to documentation for more details. - Matching on the full multipart body can now be performed using `match_files` and `match_data` parameters. Refer to documentation for more details. - Matching on extensions (including timeout) can now be performed using `match_extensions` parameter. Refer to documentation for more details. # | Low | 10/28/2024 |
| v0.32.0 | ### Added - The following option is now available: - `can_send_already_matched_responses` (boolean), defaulting to `False`. - Assertion failure message in case of unmatched responses is now linking documentation on how to deactivate the check. - Assertion failure message in case of unmatched requests is now linking documentation on how to deactivate the check. - `httpx.TimeoutException` message issued in case of unmatched request is now linking documentation on how to reuse responses (in | Low | 9/27/2024 |
| v0.31.2 | ### Fixed - `httpx_mock` marker can now be defined at different levels for a single test. | Low | 9/23/2024 |
| v0.31.1 | ### Fixed - It is now possible to match on content provided as async iterable by the client. | Low | 9/22/2024 |
| v0.31.0 | ### Changed - Tests will now fail at teardown by default if some requests were issued but were not matched. - This behavior can be changed thanks to the new ``pytest.mark.httpx_mock(assert_all_requests_were_expected=False)`` option. - The `httpx_mock` fixture is now configured using a marker (many thanks to [`Frazer McLean`](https://github.com/RazerM)). ```python # Apply marker to whole module pytestmark = pytest.mark.httpx_mock(assert_all_responses_were_requested=False) # O | Low | 9/20/2024 |
| v0.30.0 | ### Changed - Requires [`httpx`](https://www.python-httpx.org)==0.27.\* ### Fixed - Switch from `setup.py` to `pyproject.toml` (many thanks to [`Felix Scherz`](https://github.com/felixscherz)). | Low | 2/21/2024 |
| v0.29.0 | ### Added - Add support for [`pytest`](https://docs.pytest.org)==8.\* ([`pytest`](https://docs.pytest.org)==7.\* is still supported for now). (many thanks to [`Yossi Rozantsev`](https://github.com/Apakottur)) | Low | 1/29/2024 |
| v0.28.0 | ### Changed - Requires [`httpx`](https://www.python-httpx.org)==0.26.\* | Low | 12/21/2023 |
| v0.27.0 | ### Added - Explicit support for python `3.12`. ### Fixed - Custom HTTP transport are now handled (parent call to `handle_async_request` or `handle_request`). ### Changed - Only HTTP transport are now mocked, this should not have any impact, however if it does, please feel free to open an issue describing your use case. | Low | 11/13/2023 |
| v0.26.0 | ### Added - Added `proxy_url` parameter which allows matching on proxy URL. | Low | 9/18/2023 |
| v0.25.0 | ### Changed - Requires [`httpx`](https://www.python-httpx.org)==0.25.\* ### Removed - `pytest` `6` is no longer supported. | Low | 9/11/2023 |
| v0.24.0 | ### Added - Added `match_json` parameter which allows matching on JSON decoded body (matching against python representation instead of bytes). ### Changed - Even if it was never documented as a feature, the `match_headers` parameter was not considering header names case when matching. - As this might have been considered a feature by some users, the fact that `match_headers` will now respect casing is documented as a breaking change. ### Fixed - Matching on headers does not ignore na | Low | 9/4/2023 |
| v0.23.1 | ### Fixed - Version `0.23.0` introduced a regression removing the support for mutating json content provided in `httpx_mock.add_response`. - This is fixed, you can now expect the JSON return being as it was when provided to `httpx_mock.add_response`: ```python mutating_json = {"content": "request 1"} # This will return {"content": "request 1"} httpx_mock.add_response(json=mutating_json) mutating_json["content"] = "request 2" # This will return {"content": "reques | Low | 8/2/2023 |
| v0.23.0 | ### Removed - Python `3.7` and `3.8` are no longer supported. ### Fixed - `httpx_mock.add_response` is now returning a new `httpx.Response` instance upon each matching request. Preventing unnecessary recursion in streams. | Low | 8/1/2023 |
| v0.22.0 | ### Changed - Requires [`httpx`](https://www.python-httpx.org)==0.24.\* | Low | 4/12/2023 |
| v0.21.3 | ### Fixed - Update version specifiers for `pytest` dependency to support `packaging` `23`. - Add explicit support for `python` `3.11`. | Low | 1/20/2023 |
| v0.21.2 | ### Fixed - URL containing non ASCII characters in query can now be matched. - Requests are now cleared when calling `httpx_mock.reset`. | Low | 11/3/2022 |
| v0.21.1 | ### Fixed - `httpx_mock.add_callback` now handles async callbacks. | Low | 10/20/2022 |
| v0.21.0 | ### Changed - Requires [`httpx`](https://www.python-httpx.org)==0.23.\* ### Removed - Python 3.6 is no longer supported. | Low | 5/24/2022 |
| v0.20.0 | ### Added - Add support for [`pytest`](https://docs.pytest.org)==7.\* ([`pytest`](https://docs.pytest.org)==6.\* is still supported for now). (many thanks to [`Craig Blaszczyk`](https://github.com/jakul)) | Low | 2/5/2022 |
| v0.19.0 | ### Changed - Requires [`httpx`](https://www.python-httpx.org)==0.22.\* ### Removed - Python 3.6 is no longer supported. | Low | 1/26/2022 |
| v0.18.0 | ### Fixed - Callback are now executed as expected when there is a matching already sent response. ### Changed - Registration order is now looking at responses and callbacks. Prior to this version, registration order was looking at responses before callbacks. ### Removed - `httpx_mock.add_response` `data`, `files` and `boundary` parameters have been removed. It was deprecated since `0.17.0`. Refer to this version changelog entry for more details on how to update your code. | Low | 1/17/2022 |
| v0.17.3 | ### Fixed - A callback can now raise an exception again (regression in mypy check since [0.16.0]). ### Added - An exception can now be raised without creating a callback by using `httpx_mock.add_exception` method. | Low | 12/27/2021 |
| v0.17.2 | ### Fixed - Do not consider a callback response as read, even if it is not a stream, before returning to `httpx`. Allowing any specific httpx handling to be triggered such as `httpx.Response.elapsed` computing. | Low | 12/23/2021 |
| v0.17.1 | ### Fixed - Do not consider a response as read, even if it is not a stream, before returning to `httpx`. Allowing any specific httpx handling to be triggered such as `httpx.Response.elapsed` computing. | Low | 12/20/2021 |
| v0.17.0 | ### Changed - `httpx_mock.add_response` `data` parameter is only used for multipart content. It was deprecated since `0.14.0`. Refer to this version changelog entry for more details on how to update your code. ### Removed - `pytest_httpx.to_response` function has been removed. It was deprecated since `0.14.0`. Refer to this version changelog entry for more details on how to update your code. ### Deprecated - `httpx_mock.add_response` `data`, `files` and `boundary` parameters that were o | Low | 12/20/2021 |
| v0.16.0 | ### Changed - Callbacks are now expected to have a single parameter, the request. The previously second parameter `extensions`, can still be accessed via `request.extensions`. ### Fixed - Allow for users to run `mypy --strict`. | Low | 12/20/2021 |
| v0.15.0 | ### Changed - Requires [`httpx`](https://www.python-httpx.org)==0.21.\* | Low | 11/16/2021 |
| v0.14.0 | ### Changed - Requires [`httpx`](https://www.python-httpx.org)==0.20.\* (many thanks to [`Terence Honles`](https://github.com/terencehonles)) - Callbacks are now expected to return a `httpx.Response` instance instead of the previous `httpcore.Response` tuple. As a consequence, `pytest_httpx.to_response` now returns a `httpx.Response` instance. ### Added - `httpx_mock.add_response` now allows to explicitly provide bytes using `content` parameter. - `httpx_mock.add_response` now allows to | Low | 10/22/2021 |
| v0.13.0 | ### Changed - Requires [`httpx`](https://www.python-httpx.org)==0.19.\* - `files` parameter of `httpx_mock.add_response` now expect dictionary values to be binary (as per [httpx new requirement](https://github.com/encode/httpx/blob/master/CHANGELOG.md#0190-19th-june-2021)). | Low | 8/19/2021 |
| v0.12.1 | ### Fixed - Type information is now provided following [PEP 561](https://www.python.org/dev/peps/pep-0561/) (many thanks to [`Caleb Ho`](https://github.com/calebho)). | Low | 8/11/2021 |
| v0.12.0 | ### Changed - Requires [`httpx`](https://www.python-httpx.org)==0.18.\* - `ext` callback parameter was renamed into `extensions`. | Low | 4/27/2021 |
| v0.11.0 | ### Changed - Requires [`httpx`](https://www.python-httpx.org)==0.17.\* | Low | 3/1/2021 |
| v0.10.1 | ### Fixed - Order of different parameters does not matters anymore for URL matching. It does however still matter for a same parameter. | Low | 11/25/2020 |
| v0.10.0 | ### Added - Document how to assert that no requests were issued. - Document how to send cookies. - Explicit support for python 3.9 ### Changed - Requires [`httpx`](https://www.python-httpx.org)==0.16.* - Update documentation to reflect the latest way of sending bytes using `httpx`. Via `content` parameter instead of `data`. - Code now follow `black==20.8b1` formatting instead of the git master version. - Sending a JSON response using `json` parameter will now set the `application/json` | Low | 10/6/2020 |
| v0.9.0 | ### Changed - Requires [`httpx`](https://www.python-httpx.org)==0.15.* - Callbacks are now called with `ext` dictionary instead of `timeout`. To follow `httpcore` design changes. You can still retrieve timeout by using ```ext['timeout']``` | Low | 9/22/2020 |
| v0.8.0 | ### Added - `non_mocked_hosts` fixture allowing to avoid mocking requests sent on some specific hosts. ### Changed - Display the matchers that were not matched instead of the responses that were not sent. | Low | 8/26/2020 |
| v0.7.0 | ### Changed - The `httpx.HTTPError` message issued in case no mock could be found is now a `httpx.TimeoutException` containing all information required to fix the test case (if needed). | Low | 8/13/2020 |
| v0.6.0 | ### Changed - Requires [`httpx`](https://www.python-httpx.org)==0.14.* | Low | 8/7/2020 |
| v0.5.0 | ### Changed - requires [`pytest`](https://docs.pytest.org/en/latest/) 6. - `assert_and_reset` mock method has been renamed to `reset` and now takes a boolean parameter to specify if assertion should be performed. ### Added - It is now possible to disable the assertion that all registered responses were requested thanks to the `assert_all_responses_were_requested` fixture. Refer to documentation for more details. ### Removed - It is not possible to provide an URL encoded response anymor | Low | 7/31/2020 |
| v0.4.0 | ### Changed - `httpx_mock` [`pytest`](https://docs.pytest.org/en/latest/) fixture does not need to be explicitly imported anymore (many thanks to [`Thomas LÉVEIL`](https://github.com/thomasleveil)). | Low | 6/4/2020 |
| v0.3.0 | ### Changed - Requires [`httpx`](https://www.python-httpx.org)==0.13.* - requires [`pytest`](https://docs.pytest.org/en/latest/) 5.4.0 (at least) - callbacks must now return a tuple as per `httpcore` specifications. Refer to documentation for more details. - callbacks timeout parameter is now a dict as per `httpcore` specifications. | Low | 5/24/2020 |
| v0.2.1 | ### Fixed - Handle the fact that some classes and functions we use are now part of internals within [`httpx`](https://www.python-httpx.org). | Low | 3/20/2020 |
| v0.2.0 | ### Changed - Requires [`httpx`](https://www.python-httpx.org)==0.12.* | Low | 3/9/2020 |
| v0.0.4 | ### Changed - url is not a mandatory parameter for response registration anymore. - url is not a mandatory parameter for callback registration anymore. - url is not a mandatory parameter for request retrieval anymore. - method does not have a default value for response registration anymore. - method does not have a default value for callback registration anymore. - method does not have a default value for request retrieval anymore. - url and methods are not positional arguments anymore. | Low | 2/7/2020 |
| v0.0.3 | ### Added - Allow to provide JSON response as python values. - Mock async httpx requests as well. - Allow to provide files and boundary for multipart response. - Allow to provide data as a dictionary for multipart response. - Allow to provide callbacks that are executed upon reception of a request. - Handle the fact that parameters may be introduced in httpx *Dispatcher.send method. - Allow to retrieve all matching requests with HTTPXMock.get_requests ### Changed - method can now be pro | Low | 2/6/2020 |
