test_exceptions.py
This file
Path
tests/test_exceptions.py
97 lines · 2.5 KiB · Python
Last changed by
Support HTTPCore 0.13 (#1588)Tom Christiebefore adoptiond98e9e7
History of this fileTake it
The walk stopped after 200 commits: lines older than db4e417 point at it.
Transport API (#1522)Tom Christiebefore adoption1a6e254
line 1why, one step back1from unittest import mockVersion 0.14.1 (#1164)Tom Christiebefore adoptiondb4e417
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_MAPVersion 0.14.1 (#1164)Tom Christiebefore adoptiondb4e417
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:Version 0.14.1 (#1164)Tom Christiebefore adoptiondb4e417
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()Version 0.14.1 (#1164)Tom Christiebefore adoptiondb4e417
line 38why, one step back38Transport API (#1522)Tom Christiebefore adoption1a6e254
lines 39–40why, one step back39 def close(self):40 passVersion 0.14.1 (#1164)Tom Christiebefore adoptiondb4e417
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""Version 0.14.1 (#1164)Tom Christiebefore adoptiondb4e417
line 45why, one step back45Transport API (#1522)Tom Christiebefore adoption1a6e254
lines 46–47why, one step back46 def close(self):47 raise httpcore.CloseError()Version 0.14.1 (#1164)Tom Christiebefore adoptiondb4e417
line 48why, one step back48Support HTTPCore 0.13 (#1588)Tom Christiebefore adoptiond98e9e7
lines 49–51why, one step back49 with mock.patch(50 "httpcore.SyncConnectionPool.handle_request", side_effect=connect_failed51 ):Transport API (#1522)Tom Christiebefore adoption1a6e254
lines 52–55why, one step back52 with pytest.raises(httpx.ConnectError):53 httpx.get(server.url)5455 with mock.patch(Support HTTPCore 0.13 (#1588)Tom Christiebefore adoptiond98e9e7
line 56why, one step back56 "httpcore.SyncConnectionPool.handle_request",Transport API (#1522)Tom Christiebefore adoption1a6e254
lines 57–62why, one step back57 return_value=(200, [], TimeoutStream(), {}),58 ):59 with pytest.raises(httpx.ReadTimeout):60 httpx.get(server.url)6162 with mock.patch(Support HTTPCore 0.13 (#1588)Tom Christiebefore adoptiond98e9e7
line 63why, one step back63 "httpcore.SyncConnectionPool.handle_request",Transport API (#1522)Tom Christiebefore adoption1a6e254
lines 64–69why, one step back64 return_value=(200, [], CloseFailedStream(), {}),65 ):66 with pytest.raises(httpx.CloseError):67 httpx.get(server.url)6869Version 0.14.1 (#1164)Tom Christiebefore adoptiondb4e417
lines 70–85why, one step back70def test_httpx_exceptions_exposed() -> None:71 """72 All exception classes defined in `httpx._exceptions`73 are exposed as public API.74 """7576 not_exposed = [77 value78 for name, value in vars(httpx._exceptions).items()79 if isinstance(value, type)80 and issubclass(value, Exception)81 and not hasattr(httpx, name)82 ]8384 if not_exposed: # pragma: nocover85 pytest.fail(f"Unexposed HTTPX exceptions: {not_exposed}")Transport API (#1522)Tom Christiebefore adoption1a6e254
lines 86–97why, one step back868788def test_request_attribute() -> None:89 # Exception without request attribute90 exc = httpx.ReadTimeout("Read operation timed out")91 with pytest.raises(RuntimeError):92 exc.request9394 # Exception with request attribute95 request = httpx.Request("GET", "https://www.example.com")96 exc = httpx.ReadTimeout("Read operation timed out", request=request)97 assert exc.request == request