From 4e4d26d3f998e9f4c2717b00ace53ff9eb96b06a Mon Sep 17 00:00:00 2001 From: muneer320 Date: Fri, 4 Dec 2026 20:14:09 +0530 Subject: [PATCH] Require matching request extensions to be present --- CHANGELOG.md | 1 + pytest_httpx/_request_matcher.py | 3 ++- tests/test_httpx_async.py | 19 +++++++++++++++++++ tests/test_httpx_sync.py | 28 ++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9c5015b..1c043e0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Explicit support for python `3.15`. ### Fixed +- `match_extensions` no longer matches an absent extension when its expected value is `None`. - `match_params` now preserves `unittest.mock.ANY` inside tuple values, as it already does for lists. - Sending a non UTF8 body while having a `json` matcher registered will not raise a `UnicodeDecodeError` anymore. - `match_params` is now handling `bool`, `int` and `float` values in addition to `str`. Only str values were previously expected. diff --git a/pytest_httpx/_request_matcher.py b/pytest_httpx/_request_matcher.py index b73a01f..08e86d5 100644 --- a/pytest_httpx/_request_matcher.py +++ b/pytest_httpx/_request_matcher.py @@ -244,7 +244,8 @@ def _extensions_match(self, request: httpx.Request) -> bool: return True return all( - request.extensions.get(extension_name) == extension_value + extension_name in request.extensions + and request.extensions[extension_name] == extension_value for extension_name, extension_value in self.extensions.items() ) diff --git a/tests/test_httpx_async.py b/tests/test_httpx_async.py index 8dfa039..3b7fcb6 100644 --- a/tests/test_httpx_async.py +++ b/tests/test_httpx_async.py @@ -2783,6 +2783,25 @@ async def test_timeout_not_matching(httpx_mock: HTTPXMock) -> None: ) +@pytest.mark.asyncio +async def test_extensions_matching_none(httpx_mock: HTTPXMock) -> None: + httpx_mock.add_response(match_extensions={"test": None}) + + async with httpx.AsyncClient() as client: + response = await client.get("https://test_url", extensions={"test": None}) + assert response.status_code == 200 + + +@pytest.mark.asyncio +@pytest.mark.httpx_mock(assert_all_requests_were_expected=False) +async def test_missing_extension_not_matching_none(httpx_mock: HTTPXMock) -> None: + httpx_mock.add_response(match_extensions={"test": None}, is_optional=True) + + async with httpx.AsyncClient() as client: + with pytest.raises(httpx.TimeoutException): + await client.get("https://test_url") + + @pytest.mark.asyncio async def test_extensions_matching(httpx_mock: HTTPXMock) -> None: httpx_mock.add_response(match_extensions={"test": "value"}) diff --git a/tests/test_httpx_sync.py b/tests/test_httpx_sync.py index da52f61..b3013be 100644 --- a/tests/test_httpx_sync.py +++ b/tests/test_httpx_sync.py @@ -2446,6 +2446,34 @@ def test_timeout_not_matching(httpx_mock: HTTPXMock) -> None: ) +def test_extensions_matching_none(httpx_mock: HTTPXMock) -> None: + httpx_mock.add_response(match_extensions={"test": None}) + + with httpx.Client() as client: + response = client.get("https://test_url", extensions={"test": None}) + assert response.status_code == 200 + + +@pytest.mark.httpx_mock(assert_all_requests_were_expected=False) +def test_missing_extension_not_matching_none(httpx_mock: HTTPXMock) -> None: + httpx_mock.add_response(match_extensions={"test": None}, is_optional=True) + + with httpx.Client() as client, pytest.raises(httpx.TimeoutException): + client.get("https://test_url") + + +def test_get_requests_matching_none_extension(httpx_mock: HTTPXMock) -> None: + httpx_mock.add_response(is_reusable=True) + + with httpx.Client() as client: + client.get("https://test_url") + client.get("https://test_url", extensions={"test": None}) + + requests = httpx_mock.get_requests(match_extensions={"test": None}) + assert len(requests) == 1 + assert "test" in requests[0].extensions + + def test_extensions_matching(httpx_mock: HTTPXMock) -> None: httpx_mock.add_response(match_extensions={"test": "value"})