Skip to content
Myr

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 file

Take it

Map HTTPCore exceptions (#1044)Florimond Mancabefore adoptionbacc2d1

lines 1–3why, one step back
1from typing import Any
23import httpcore

Add exceptions missing from top-level package (#1045)Florimond Mancabefore adoptionfab4279

lines 4–6why, one step back
4import pytest
56import httpx

Map HTTPCore exceptions (#1044)Florimond Mancabefore adoptionbacc2d1

lines 7–22why, one step back
7from httpx._exceptions import HTTPCORE_EXC_MAP
8910def 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 ]22

Drop deprecated API (#1057)Tom Christiebefore adoption3f96e74

line 23why, one step back
23 if not_mapped: # pragma: nocover

Map HTTPCore exceptions (#1044)Florimond Mancabefore adoptionbacc2d1

lines 24–26why, one step back
24 pytest.fail(f"Unmapped httpcore exceptions: {not_mapped}")
2526

Map httpcore exceptions for `Response` read methods (#1190)Joebefore adoption03cd88c

line 27why, one step back
27def test_httpcore_exception_mapping(server) -> None:

Map HTTPCore exceptions (#1044)Florimond Mancabefore adoptionbacc2d1

lines 28–34why, one step back
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")

Map httpcore exceptions for `Response` read methods (#1190)Joebefore adoption03cd88c

lines 35–41why, one step back
35
36 # 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 back
42
43 # 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 back
51
5253def 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 ]66

Drop deprecated API (#1057)Tom Christiebefore adoption3f96e74

line 67why, one step back
67 if not_exposed: # pragma: nocover

Add exceptions missing from top-level package (#1045)Florimond Mancabefore adoptionfab4279

line 68why, one step back
68 pytest.fail(f"Unexposed HTTPX exceptions: {not_exposed}")