Skip to content
Myr

test_exceptions.py

This file

Path

tests/test_exceptions.py

86 lines · 2.3 KiB · Python

Last changed by

Version 0.21 (#1935)Tom Christiebefore adoption61188fe

History of this file

Take it

The walk stopped after 200 commits: lines older than 0af6d9a point at it.

Transport API (#1522)Tom Christiebefore adoption1a6e254

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

Use relative links for interlinking markdown files in docs (#1390)Tom Christiebefore adoption0af6d9a

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

Use relative links for interlinking markdown files in docs (#1390)Tom Christiebefore adoption0af6d9a

lines 8–20why, 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_MAP

Version 0.21 (#1935)Tom Christiebefore adoption61188fe

line 21why, one step back
21 and value is not httpcore.ConnectionNotAvailable

Use relative links for interlinking markdown files in docs (#1390)Tom Christiebefore adoption0af6d9a

lines 22–32why, one step back
22 ]
2324 if not_mapped: # pragma: nocover25 pytest.fail(f"Unmapped httpcore exceptions: {not_mapped}")262728def test_httpcore_exception_mapping(server) -> None:29 """30 HTTPCore exception mapping works as expected.31 """32

Transport API (#1522)Tom Christiebefore adoption1a6e254

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

Use relative links for interlinking markdown files in docs (#1390)Tom Christiebefore adoption0af6d9a

line 35why, one step back
35

Transport API (#1522)Tom Christiebefore adoption1a6e254

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

Use relative links for interlinking markdown files in docs (#1390)Tom Christiebefore adoption0af6d9a

line 39why, one step back
39

Transport API (#1522)Tom Christiebefore adoption1a6e254

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

Use relative links for interlinking markdown files in docs (#1390)Tom Christiebefore adoption0af6d9a

line 42why, one step back
42

Support HTTPCore 0.13 (#1588)Tom Christiebefore adoptiond98e9e7

line 43why, one step back
43 with mock.patch(

Version 0.21 (#1935)Tom Christiebefore adoption61188fe

line 44why, one step back
44 "httpcore.ConnectionPool.handle_request", side_effect=connect_failed

Support HTTPCore 0.13 (#1588)Tom Christiebefore adoptiond98e9e7

line 45why, one step back
45 ):

Transport API (#1522)Tom Christiebefore adoption1a6e254

lines 46–49why, one step back
46 with pytest.raises(httpx.ConnectError):
47 httpx.get(server.url)4849 with mock.patch(

Version 0.21 (#1935)Tom Christiebefore adoption61188fe

lines 50–53why, one step back
50 "httpcore.ConnectionPool.handle_request",
51 return_value=httpcore.Response(52 200, headers=[], content=TimeoutStream(), extensions={}53 ),

Transport API (#1522)Tom Christiebefore adoption1a6e254

lines 54–58why, one step back
54 ):
55 with pytest.raises(httpx.ReadTimeout):56 httpx.get(server.url)5758

Use relative links for interlinking markdown files in docs (#1390)Tom Christiebefore adoption0af6d9a

lines 59–74why, one step back
59def test_httpx_exceptions_exposed() -> None:
60 """61 All exception classes defined in `httpx._exceptions`62 are exposed as public API.63 """6465 not_exposed = [66 value67 for name, value in vars(httpx._exceptions).items()68 if isinstance(value, type)69 and issubclass(value, Exception)70 and not hasattr(httpx, name)71 ]7273 if not_exposed: # pragma: nocover74 pytest.fail(f"Unexposed HTTPX exceptions: {not_exposed}")

Transport API (#1522)Tom Christiebefore adoption1a6e254

lines 75–86why, one step back
75
7677def test_request_attribute() -> None:78 # Exception without request attribute79 exc = httpx.ReadTimeout("Read operation timed out")80 with pytest.raises(RuntimeError):81 exc.request8283 # Exception with request attribute84 request = httpx.Request("GET", "https://www.example.com")85 exc = httpx.ReadTimeout("Read operation timed out", request=request)86 assert exc.request == request