Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 2 additions & 1 deletion pytest_httpx/_request_matcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
)

Expand Down
19 changes: 19 additions & 0 deletions tests/test_httpx_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"})
Expand Down
28 changes: 28 additions & 0 deletions tests/test_httpx_sync.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"})

Expand Down