Skip to content
Myr

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 file

Take it

The walk stopped after 200 commits: lines older than 54f7708 point at it.

Transport API (#1522)Tom Christiebefore adoption1a6e254

line 1why, one step back
1from unittest import mock

Event hooks (#1246)Tom Christiebefore adoption54f7708

lines 2–6why, one step back
2
3import httpcore4import pytest56import httpx

Transport API (#1522)Tom Christiebefore adoption1a6e254

line 7why, one step back
7from httpx._transports.default import HTTPCORE_EXC_MAP

Event hooks (#1246)Tom Christiebefore adoption54f7708

lines 8–31why, one step back
8
910def 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}")252627def test_httpcore_exception_mapping(server) -> None:28 """29 HTTPCore exception mapping works as expected.30 """31

Transport API (#1522)Tom Christiebefore adoption1a6e254

lines 32–33why, one step back
32 def connect_failed(*args, **kwargs):
33 raise httpcore.ConnectError()

Event hooks (#1246)Tom Christiebefore adoption54f7708

line 34why, one step back
34

Transport API (#1522)Tom Christiebefore adoption1a6e254

lines 35–37why, one step back
35 class TimeoutStream:
36 def __iter__(self):37 raise httpcore.ReadTimeout()

Event hooks (#1246)Tom Christiebefore adoption54f7708

line 38why, one step back
38

Transport API (#1522)Tom Christiebefore adoption1a6e254

lines 39–40why, one step back
39 def close(self):
40 pass

Event hooks (#1246)Tom Christiebefore adoption54f7708

line 41why, one step back
41

Transport API (#1522)Tom Christiebefore adoption1a6e254

lines 42–44why, one step back
42 class CloseFailedStream:
43 def __iter__(self):44 yield b""

Event hooks (#1246)Tom Christiebefore adoption54f7708

line 45why, one step back
45

Transport API (#1522)Tom Christiebefore adoption1a6e254

lines 46–47why, one step back
46 def close(self):
47 raise httpcore.CloseError()

Event hooks (#1246)Tom Christiebefore adoption54f7708

line 48why, one step back
48

Support HTTPCore 0.13 (#1588)Tom Christiebefore adoptiond98e9e7

lines 49–51why, one step back
49 with mock.patch(
50 "httpcore.SyncConnectionPool.handle_request", side_effect=connect_failed51 ):

Transport API (#1522)Tom Christiebefore adoption1a6e254

lines 52–55why, one step back
52 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 back
56 "httpcore.SyncConnectionPool.handle_request",

Transport API (#1522)Tom Christiebefore adoption1a6e254

lines 57–62why, one step back
57 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 back
63 "httpcore.SyncConnectionPool.handle_request",

Transport API (#1522)Tom Christiebefore adoption1a6e254

lines 64–69why, one step back
64 return_value=(200, [], CloseFailedStream(), {}),
65 ):66 with pytest.raises(httpx.CloseError):67 httpx.get(server.url)6869

Event hooks (#1246)Tom Christiebefore adoption54f7708

lines 70–85why, one step back
70def 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 back
86
8788def 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