test_exceptions.py
This file
Path
tests/test_exceptions.py
68 lines · 1.9 KiB · Python
Last changed by
Map httpcore exceptions for `Response` read methods (#1190)Joebefore adoption03cd88c
History of this fileTake it
Map HTTPCore exceptions (#1044)Florimond Mancabefore adoptionbacc2d1
lines 1–3why, one step back1from typing import Any23import httpcoreAdd exceptions missing from top-level package (#1045)Florimond Mancabefore adoptionfab4279
lines 4–6why, one step back4import pytest56import httpxMap HTTPCore exceptions (#1044)Florimond Mancabefore adoptionbacc2d1
lines 7–22why, one step back7from httpx._exceptions import HTTPCORE_EXC_MAP8910def 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 ]22Drop deprecated API (#1057)Tom Christiebefore adoption3f96e74
line 23why, one step back23 if not_mapped: # pragma: nocoverMap HTTPCore exceptions (#1044)Florimond Mancabefore adoptionbacc2d1
lines 24–26why, one step back24 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:Map HTTPCore exceptions (#1044)Florimond Mancabefore adoptionbacc2d1
lines 28–34why, one step back28 """29 HTTPCore exception mapping works as expected.30 """3132 # Make sure we don't just map to `NetworkError`.33 with pytest.raises(httpx.ConnectError):34 httpx.get("http://doesnotexist")Map httpcore exceptions for `Response` read methods (#1190)Joebefore adoption03cd88c
lines 35–41why, one step back3536 # Make sure streaming methods also map exceptions.37 url = server.url.copy_with(path="/slow_stream_response")38 timeout = httpx.Timeout(None, read=0.1)39 with httpx.stream("GET", url, timeout=timeout) as stream:40 with pytest.raises(httpx.ReadTimeout):41 stream.read()Map HTTPCore exceptions (#1044)Florimond Mancabefore adoptionbacc2d1
lines 42–50why, one step back4243 # Make sure it also works with custom transports.44 class MockTransport(httpcore.SyncHTTPTransport):45 def request(self, *args: Any, **kwargs: Any) -> Any:46 raise httpcore.ProtocolError()4748 client = httpx.Client(transport=MockTransport())49 with pytest.raises(httpx.ProtocolError):50 client.get("http://testserver")Add exceptions missing from top-level package (#1045)Florimond Mancabefore adoptionfab4279
lines 51–66why, one step back515253def test_httpx_exceptions_exposed() -> None:54 """55 All exception classes defined in `httpx._exceptions`56 are exposed as public API.57 """5859 not_exposed = [60 value61 for name, value in vars(httpx._exceptions).items()62 if isinstance(value, type)63 and issubclass(value, Exception)64 and not hasattr(httpx, name)65 ]66Drop deprecated API (#1057)Tom Christiebefore adoption3f96e74
line 67why, one step back67 if not_exposed: # pragma: nocoverAdd exceptions missing from top-level package (#1045)Florimond Mancabefore adoptionfab4279
line 68why, one step back68 pytest.fail(f"Unexposed HTTPX exceptions: {not_exposed}")