|
| 1 | +"""Tests for the AsyncClient internal terminal's exception mapping.""" |
| 2 | + |
| 3 | +from http import HTTPStatus |
| 4 | + |
| 5 | +import httpx2 |
| 6 | +import pytest |
| 7 | + |
| 8 | +from httpware import ( |
| 9 | + AsyncClient, |
| 10 | + BadRequestError, |
| 11 | + ClientStatusError, |
| 12 | + InternalServerError, |
| 13 | + NotFoundError, |
| 14 | + RateLimitedError, |
| 15 | + ServerStatusError, |
| 16 | + StatusError, |
| 17 | + TimeoutError, # noqa: A004 |
| 18 | + TransportError, |
| 19 | +) |
| 20 | + |
| 21 | + |
| 22 | +def _client_with_handler(handler) -> AsyncClient: # noqa: ANN001 |
| 23 | + transport = httpx2.MockTransport(handler) |
| 24 | + return AsyncClient(httpx2_client=httpx2.AsyncClient(transport=transport)) |
| 25 | + |
| 26 | + |
| 27 | +async def test_terminal_returns_response_on_2xx() -> None: |
| 28 | + client = _client_with_handler(lambda req: httpx2.Response(HTTPStatus.OK, json={"ok": True}, request=req)) |
| 29 | + response = await client.send(httpx2.Request("GET", "https://example.test/x")) |
| 30 | + assert response.status_code == HTTPStatus.OK |
| 31 | + assert response.json() == {"ok": True} |
| 32 | + |
| 33 | + |
| 34 | +@pytest.mark.parametrize( |
| 35 | + ("status", "exc_type"), |
| 36 | + [ |
| 37 | + (HTTPStatus.BAD_REQUEST, BadRequestError), |
| 38 | + (HTTPStatus.NOT_FOUND, NotFoundError), |
| 39 | + (HTTPStatus.TOO_MANY_REQUESTS, RateLimitedError), |
| 40 | + (HTTPStatus.INTERNAL_SERVER_ERROR, InternalServerError), |
| 41 | + ], |
| 42 | +) |
| 43 | +async def test_known_status_codes_raise_typed_subclass(status: int, exc_type: type[StatusError]) -> None: |
| 44 | + client = _client_with_handler(lambda req: httpx2.Response(status, request=req)) |
| 45 | + with pytest.raises(exc_type) as info: |
| 46 | + await client.send(httpx2.Request("GET", "https://example.test/x")) |
| 47 | + assert info.value.response.status_code == status |
| 48 | + |
| 49 | + |
| 50 | +async def test_unknown_4xx_falls_back_to_client_status_error() -> None: |
| 51 | + client = _client_with_handler(lambda req: httpx2.Response(HTTPStatus.IM_A_TEAPOT, request=req)) |
| 52 | + with pytest.raises(ClientStatusError) as info: |
| 53 | + await client.send(httpx2.Request("GET", "https://example.test/x")) |
| 54 | + assert info.value.response.status_code == HTTPStatus.IM_A_TEAPOT |
| 55 | + assert type(info.value) is ClientStatusError |
| 56 | + |
| 57 | + |
| 58 | +async def test_unknown_5xx_falls_back_to_server_status_error() -> None: |
| 59 | + client = _client_with_handler(lambda req: httpx2.Response(599, request=req)) |
| 60 | + with pytest.raises(ServerStatusError) as info: |
| 61 | + await client.send(httpx2.Request("GET", "https://example.test/x")) |
| 62 | + assert info.value.response.status_code == 599 # noqa: PLR2004 |
| 63 | + assert type(info.value) is ServerStatusError |
| 64 | + |
| 65 | + |
| 66 | +async def test_3xx_does_not_raise() -> None: |
| 67 | + client = _client_with_handler( |
| 68 | + lambda req: httpx2.Response(HTTPStatus.MOVED_PERMANENTLY, request=req, headers={"location": "/y"}) |
| 69 | + ) |
| 70 | + response = await client.send(httpx2.Request("GET", "https://example.test/x")) |
| 71 | + assert response.status_code == HTTPStatus.MOVED_PERMANENTLY |
| 72 | + |
| 73 | + |
| 74 | +async def test_httpx2_timeout_maps_to_httpware_timeout() -> None: |
| 75 | + def handler(request: httpx2.Request) -> httpx2.Response: # noqa: ARG001 |
| 76 | + msg = "read timeout" |
| 77 | + raise httpx2.ReadTimeout(msg) |
| 78 | + |
| 79 | + client = _client_with_handler(handler) |
| 80 | + with pytest.raises(TimeoutError, match="read timeout"): |
| 81 | + await client.send(httpx2.Request("GET", "https://example.test/x")) |
| 82 | + |
| 83 | + |
| 84 | +async def test_httpx2_connect_error_maps_to_transport_error() -> None: |
| 85 | + def handler(request: httpx2.Request) -> httpx2.Response: # noqa: ARG001 |
| 86 | + msg = "connect refused" |
| 87 | + raise httpx2.ConnectError(msg) |
| 88 | + |
| 89 | + client = _client_with_handler(handler) |
| 90 | + with pytest.raises(TransportError, match="connect refused"): |
| 91 | + await client.send(httpx2.Request("GET", "https://example.test/x")) |
| 92 | + |
| 93 | + |
| 94 | +async def test_send_on_closed_client_raises_transport_error() -> None: |
| 95 | + transport = httpx2.MockTransport(lambda req: httpx2.Response(HTTPStatus.OK, request=req)) |
| 96 | + underlying = httpx2.AsyncClient(transport=transport) |
| 97 | + client = AsyncClient(httpx2_client=underlying) |
| 98 | + await underlying.aclose() |
| 99 | + with pytest.raises(TransportError): |
| 100 | + await client.send(httpx2.Request("GET", "https://example.test/x")) |
0 commit comments