test_exceptions.py
This file
Path
tests/test_exceptions.py
95 lines · 2.5 KiB · Python
Last changed by
Transport API (#1522)Tom Christiebefore adoption1a6e254
History of this fileTake it
The walk stopped after 200 commits: lines older than c089480 point at it.
Transport API (#1522)Tom Christiebefore adoption1a6e254
line 1why, one step back1from unittest import mockURL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480
lines 2–6why, one step back23import httpcore4import pytest56import httpxTransport API (#1522)Tom Christiebefore adoption1a6e254
line 7why, one step back7from httpx._transports.default import HTTPCORE_EXC_MAPURL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480
lines 8–26why, one step back8910def test_httpcore_all_exceptions_mapped() -> None:11 """12 All exception classes exposed by HTTPCore are properly mapped to an HTTPX-specific13 exception class.14 """15 not_mapped = [16 value17 for name, value in vars(httpcore).items()18 if isinstance(value, type)19 and issubclass(value, Exception)20 and value not in HTTPCORE_EXC_MAP21 ]2223 if not_mapped: # pragma: nocover24 pytest.fail(f"Unmapped httpcore exceptions: {not_mapped}")2526Map httpcore exceptions for `Response` read methods (#1190)Joebefore adoption03cd88c
line 27why, one step back27def test_httpcore_exception_mapping(server) -> None:URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480
lines 28–31why, one step back28 """29 HTTPCore exception mapping works as expected.30 """31Transport API (#1522)Tom Christiebefore adoption1a6e254
lines 32–33why, one step back32 def connect_failed(*args, **kwargs):33 raise httpcore.ConnectError()Map httpcore exceptions for `Response` read methods (#1190)Joebefore adoption03cd88c
line 34why, one step back34Transport API (#1522)Tom Christiebefore adoption1a6e254
lines 35–37why, one step back35 class TimeoutStream:36 def __iter__(self):37 raise httpcore.ReadTimeout()URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480
line 38why, one step back38Transport API (#1522)Tom Christiebefore adoption1a6e254
lines 39–40why, one step back39 def close(self):40 passURL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480
line 41why, one step back41Transport API (#1522)Tom Christiebefore adoption1a6e254
lines 42–44why, one step back42 class CloseFailedStream:43 def __iter__(self):44 yield b""URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480
line 45why, one step back45Transport API (#1522)Tom Christiebefore adoption1a6e254
lines 46–47why, one step back46 def close(self):47 raise httpcore.CloseError()URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480
line 48why, one step back48Transport API (#1522)Tom Christiebefore adoption1a6e254
lines 49–67why, one step back49 with mock.patch("httpcore.SyncConnectionPool.request", side_effect=connect_failed):50 with pytest.raises(httpx.ConnectError):51 httpx.get(server.url)5253 with mock.patch(54 "httpcore.SyncConnectionPool.request",55 return_value=(200, [], TimeoutStream(), {}),56 ):57 with pytest.raises(httpx.ReadTimeout):58 httpx.get(server.url)5960 with mock.patch(61 "httpcore.SyncConnectionPool.request",62 return_value=(200, [], CloseFailedStream(), {}),63 ):64 with pytest.raises(httpx.CloseError):65 httpx.get(server.url)6667URL.port becomes Optional[int] (#1080)Tom Christiebefore adoptionc089480
lines 68–83why, one step back68def test_httpx_exceptions_exposed() -> None:69 """70 All exception classes defined in `httpx._exceptions`71 are exposed as public API.72 """7374 not_exposed = [75 value76 for name, value in vars(httpx._exceptions).items()77 if isinstance(value, type)78 and issubclass(value, Exception)79 and not hasattr(httpx, name)80 ]8182 if not_exposed: # pragma: nocover83 pytest.fail(f"Unexposed HTTPX exceptions: {not_exposed}")Transport API (#1522)Tom Christiebefore adoption1a6e254
lines 84–95why, one step back848586def test_request_attribute() -> None:87 # Exception without request attribute88 exc = httpx.ReadTimeout("Read operation timed out")89 with pytest.raises(RuntimeError):90 exc.request9192 # Exception with request attribute93 request = httpx.Request("GET", "https://www.example.com")94 exc = httpx.ReadTimeout("Read operation timed out", request=request)95 assert exc.request == request