test_exceptions.py
This file
Path
tests/test_exceptions.py
63 lines · 1.8 KiB · Python
Last changed by
Use `__future__.annotations` (#3068)Kar Petrosyanbefore adoption371b6e9
History of this fileTake it
The walk stopped after 200 commits: lines older than a682f6f point at it.
Use `__future__.annotations` (#3068)Kar Petrosyanbefore adoption371b6e9
lines 1–2why, one step back1from __future__ import annotations2Fix exception suppression in asgi transport (#2669)Nikbefore adoptiona682f6f
lines 3–38why, one step back3import typing45import httpcore6import pytest78import httpx910if typing.TYPE_CHECKING: # pragma: no cover11 from conftest import TestServer121314def test_httpcore_all_exceptions_mapped() -> None:15 """16 All exception classes exposed by HTTPCore are properly mapped to an HTTPX-specific17 exception class.18 """19 expected_mapped_httpcore_exceptions = {20 value.__name__21 for _, value in vars(httpcore).items()22 if isinstance(value, type)23 and issubclass(value, Exception)24 and value is not httpcore.ConnectionNotAvailable25 }2627 httpx_exceptions = {28 value.__name__29 for _, value in vars(httpx).items()30 if isinstance(value, type) and issubclass(value, Exception)31 }3233 unmapped_exceptions = expected_mapped_httpcore_exceptions - httpx_exceptions3435 if unmapped_exceptions: # pragma: no cover36 pytest.fail(f"Unmapped httpcore exceptions: {unmapped_exceptions}")3738Use `__future__.annotations` (#3068)Kar Petrosyanbefore adoption371b6e9
line 39why, one step back39def test_httpcore_exception_mapping(server: TestServer) -> None:Fix exception suppression in asgi transport (#2669)Nikbefore adoptiona682f6f
lines 40–63why, one step back40 """41 HTTPCore exception mapping works as expected.42 """43 impossible_port = 12345644 with pytest.raises(httpx.ConnectError):45 httpx.get(server.url.copy_with(port=impossible_port))4647 with pytest.raises(httpx.ReadTimeout):48 httpx.get(49 server.url.copy_with(path="/slow_response"),50 timeout=httpx.Timeout(5, read=0.01),51 )525354def test_request_attribute() -> None:55 # Exception without request attribute56 exc = httpx.ReadTimeout("Read operation timed out")57 with pytest.raises(RuntimeError):58 exc.request # noqa: B0185960 # Exception with request attribute61 request = httpx.Request("GET", "https://www.example.com")62 exc = httpx.ReadTimeout("Read operation timed out", request=request)63 assert exc.request == request