Skip to content
Merged
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 @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Fixed
- URL with more than one value for the same parameter were not matched properly (matching was performed on the first value).
- `httpx_mock.add_exception` is now properly documented (accepts `BaseException` instead of `Exception`).

## [0.35.0] - 2024-11-28
### Changed
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<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-274 passed-blue"></a>
<a href="https://github.com/Colin-b/pytest_httpx/actions"><img alt="Number of tests" src="https://img.shields.io/badge/tests-295 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>

Expand Down
2 changes: 1 addition & 1 deletion pytest_httpx/_httpx_mock.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ def add_callback(
"""
self._callbacks.append((_RequestMatcher(self._options, **matchers), callback))

def add_exception(self, exception: Exception, **matchers: Any) -> None:
def add_exception(self, exception: BaseException, **matchers: Any) -> None:
"""
Raise an exception if a request match.

Expand Down
27 changes: 21 additions & 6 deletions tests/test_httpx_async.py
Original file line number Diff line number Diff line change
Expand Up @@ -1052,15 +1052,30 @@ async def test_request_exception_raising(httpx_mock: HTTPXMock) -> None:


@pytest.mark.asyncio
async def test_non_request_exception_raising(httpx_mock: HTTPXMock) -> None:
httpx_mock.add_exception(
httpx.HTTPError("Unable to read within 5.0"), url="https://test_url"
)
@pytest.mark.parametrize(
("exception_type", "message"),
[
# httpx exception without request context
pytest.param(
httpx.HTTPError, "Unable to read within 5.0", id="non_request_exception"
),
# BaseException derived exception
pytest.param(
asyncio.CancelledError,
"Request was cancelled",
id="cancelled_exception",
),
],
)
async def test_non_request_exception_raising(
httpx_mock: HTTPXMock, exception_type: type, message: str
) -> None:
httpx_mock.add_exception(exception_type(message), url="https://test_url")

async with httpx.AsyncClient() as client:
with pytest.raises(httpx.HTTPError) as exception_info:
with pytest.raises(exception_type) as exception_info:
await client.get("https://test_url")
assert str(exception_info.value) == "Unable to read within 5.0"
assert str(exception_info.value) == message


@pytest.mark.asyncio
Expand Down