Skip to content

Commit ced1c3a

Browse files
committed
Document the fact that add_exception can receive BaseException derived exception
1 parent c3dd346 commit ced1c3a

2 files changed

Lines changed: 12 additions & 9 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1111

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

1516
## [0.35.0] - 2024-11-28
1617
### Changed

tests/test_httpx_async.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -726,10 +726,10 @@ def instant_response(request: httpx.Request) -> httpx.Response:
726726

727727
@pytest.mark.asyncio
728728
async def test_async_callback_with_pattern_in_url(httpx_mock: HTTPXMock) -> None:
729-
def custom_response(request: httpx.Request) -> httpx.Response:
729+
async def custom_response(request: httpx.Request) -> httpx.Response:
730730
return httpx.Response(status_code=200, json={"url": str(request.url)})
731731

732-
def custom_response2(request: httpx.Request) -> httpx.Response:
732+
async def custom_response2(request: httpx.Request) -> httpx.Response:
733733
return httpx.Response(
734734
status_code=200,
735735
extensions={"http_version": b"HTTP/2.0"},
@@ -1055,17 +1055,19 @@ async def test_request_exception_raising(httpx_mock: HTTPXMock) -> None:
10551055
@pytest.mark.parametrize(
10561056
("exception_type", "message"),
10571057
[
1058+
# httpx exception without request context
10581059
pytest.param(
10591060
httpx.HTTPError, "Unable to read within 5.0", id="non_request_exception"
10601061
),
1062+
# BaseException derived exception
10611063
pytest.param(
10621064
asyncio.CancelledError,
10631065
"Request was cancelled",
10641066
id="cancelled_exception",
10651067
),
10661068
],
10671069
)
1068-
async def test_exception_raising(
1070+
async def test_non_request_exception_raising(
10691071
httpx_mock: HTTPXMock, exception_type: type, message: str
10701072
) -> None:
10711073
httpx_mock.add_exception(exception_type(message), url="https://test_url")
@@ -1091,7 +1093,7 @@ def custom_response(request: httpx.Request) -> httpx.Response:
10911093

10921094
@pytest.mark.asyncio
10931095
async def test_async_callback_returning_response(httpx_mock: HTTPXMock) -> None:
1094-
def custom_response(request: httpx.Request) -> httpx.Response:
1096+
async def custom_response(request: httpx.Request) -> httpx.Response:
10951097
return httpx.Response(status_code=200, json={"url": str(request.url)})
10961098

10971099
httpx_mock.add_callback(custom_response, url="https://test_url")
@@ -1121,7 +1123,7 @@ def custom_response(request: httpx.Request) -> httpx.Response:
11211123

11221124
@pytest.mark.asyncio
11231125
async def test_async_callback_executed_twice(httpx_mock: HTTPXMock) -> None:
1124-
def custom_response(request: httpx.Request) -> httpx.Response:
1126+
async def custom_response(request: httpx.Request) -> httpx.Response:
11251127
return httpx.Response(status_code=200, json=["content"])
11261128

11271129
httpx_mock.add_callback(custom_response, is_reusable=True)
@@ -1161,7 +1163,7 @@ def custom_response(request: httpx.Request) -> httpx.Response:
11611163

11621164
@pytest.mark.asyncio
11631165
async def test_async_callback_registered_after_response(httpx_mock: HTTPXMock) -> None:
1164-
def custom_response(request: httpx.Request) -> httpx.Response:
1166+
async def custom_response(request: httpx.Request) -> httpx.Response:
11651167
return httpx.Response(status_code=200, json=["content2"])
11661168

11671169
httpx_mock.add_response(json=["content1"])
@@ -1207,7 +1209,7 @@ def custom_response(request: httpx.Request) -> httpx.Response:
12071209

12081210
@pytest.mark.asyncio
12091211
async def test_response_registered_after_async_callback(httpx_mock: HTTPXMock) -> None:
1210-
def custom_response(request: httpx.Request) -> httpx.Response:
1212+
async def custom_response(request: httpx.Request) -> httpx.Response:
12111213
return httpx.Response(status_code=200, json=["content1"])
12121214

12131215
httpx_mock.add_callback(custom_response)
@@ -1247,7 +1249,7 @@ def custom_response(request: httpx.Request) -> httpx.Response:
12471249

12481250
@pytest.mark.asyncio
12491251
async def test_async_callback_matching_method(httpx_mock: HTTPXMock) -> None:
1250-
def custom_response(request: httpx.Request) -> httpx.Response:
1252+
async def custom_response(request: httpx.Request) -> httpx.Response:
12511253
return httpx.Response(status_code=200, json=["content"])
12521254

12531255
httpx_mock.add_callback(custom_response, method="GET", is_reusable=True)
@@ -2210,7 +2212,7 @@ async def test_elapsed_when_add_callback(httpx_mock: HTTPXMock) -> None:
22102212

22112213
@pytest.mark.asyncio
22122214
async def test_elapsed_when_add_async_callback(httpx_mock: HTTPXMock) -> None:
2213-
def custom_response(request: httpx.Request) -> httpx.Response:
2215+
async def custom_response(request: httpx.Request) -> httpx.Response:
22142216
return httpx.Response(status_code=200, json={"foo": "bar"})
22152217

22162218
httpx_mock.add_callback(custom_response)

0 commit comments

Comments
 (0)