test_exceptions.py
This file
Path
tests/test_exceptions.py
61 lines · 1.6 KiB · Python
Last changed by
Drop deprecated API (#1057)Tom Christiebefore adoption3f96e74
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–43why, one step back24 pytest.fail(f"Unmapped httpcore exceptions: {not_mapped}")252627def test_httpcore_exception_mapping() -> None:28 """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")3536 # Make sure it also works with custom transports.37 class MockTransport(httpcore.SyncHTTPTransport):38 def request(self, *args: Any, **kwargs: Any) -> Any:39 raise httpcore.ProtocolError()4041 client = httpx.Client(transport=MockTransport())42 with pytest.raises(httpx.ProtocolError):43 client.get("http://testserver")Add exceptions missing from top-level package (#1045)Florimond Mancabefore adoptionfab4279
lines 44–59why, one step back444546def test_httpx_exceptions_exposed() -> None:47 """48 All exception classes defined in `httpx._exceptions`49 are exposed as public API.50 """5152 not_exposed = [53 value54 for name, value in vars(httpx._exceptions).items()55 if isinstance(value, type)56 and issubclass(value, Exception)57 and not hasattr(httpx, name)58 ]59Drop deprecated API (#1057)Tom Christiebefore adoption3f96e74
line 60why, one step back60 if not_exposed: # pragma: nocoverAdd exceptions missing from top-level package (#1045)Florimond Mancabefore adoptionfab4279
line 61why, one step back61 pytest.fail(f"Unexposed HTTPX exceptions: {not_exposed}")