11import typing
2- from unittest import mock
32
43import httpcore
54import pytest
65
76import httpx
8- from httpx ._transports .default import HTTPCORE_EXC_MAP
97
108if typing .TYPE_CHECKING : # pragma: no cover
119 from conftest import TestServer
@@ -16,66 +14,39 @@ def test_httpcore_all_exceptions_mapped() -> None:
1614 All exception classes exposed by HTTPCore are properly mapped to an HTTPX-specific
1715 exception class.
1816 """
19- not_mapped = [
20- value
21- for name , value in vars (httpcore ).items ()
17+ expected_mapped_httpcore_exceptions = {
18+ value . __name__
19+ for _ , value in vars (httpcore ).items ()
2220 if isinstance (value , type )
2321 and issubclass (value , Exception )
24- and value not in HTTPCORE_EXC_MAP
2522 and value is not httpcore .ConnectionNotAvailable
26- ]
23+ }
2724
28- if not_mapped : # pragma: no cover
29- pytest .fail (f"Unmapped httpcore exceptions: { not_mapped } " )
25+ httpx_exceptions = {
26+ value .__name__
27+ for _ , value in vars (httpx ).items ()
28+ if isinstance (value , type ) and issubclass (value , Exception )
29+ }
3030
31+ unmapped_exceptions = expected_mapped_httpcore_exceptions - httpx_exceptions
3132
32- def test_httpcore_exception_mapping (server : "TestServer" ) -> None :
33- """
34- HTTPCore exception mapping works as expected.
35- """
36-
37- def connect_failed (* args , ** kwargs ):
38- raise httpcore .ConnectError ()
39-
40- class TimeoutStream :
41- def __iter__ (self ):
42- raise httpcore .ReadTimeout ()
43-
44- def close (self ):
45- pass
46-
47- with mock .patch (
48- "httpcore.ConnectionPool.handle_request" , side_effect = connect_failed
49- ):
50- with pytest .raises (httpx .ConnectError ):
51- httpx .get (server .url )
33+ if unmapped_exceptions : # pragma: no cover
34+ pytest .fail (f"Unmapped httpcore exceptions: { unmapped_exceptions } " )
5235
53- with mock .patch (
54- "httpcore.ConnectionPool.handle_request" ,
55- return_value = httpcore .Response (
56- 200 , headers = [], content = TimeoutStream (), extensions = {}
57- ),
58- ):
59- with pytest .raises (httpx .ReadTimeout ):
60- httpx .get (server .url )
6136
62-
63- def test_httpx_exceptions_exposed () -> None :
37+ def test_httpcore_exception_mapping (server : "TestServer" ) -> None :
6438 """
65- All exception classes defined in `httpx._exceptions`
66- are exposed as public API.
39+ HTTPCore exception mapping works as expected.
6740 """
68-
69- not_exposed = [
70- value
71- for name , value in vars (httpx ._exceptions ).items ()
72- if isinstance (value , type )
73- and issubclass (value , Exception )
74- and not hasattr (httpx , name )
75- ]
76-
77- if not_exposed : # pragma: no cover
78- pytest .fail (f"Unexposed HTTPX exceptions: { not_exposed } " )
41+ impossible_port = 123456
42+ with pytest .raises (httpx .ConnectError ):
43+ httpx .get (server .url .copy_with (port = impossible_port ))
44+
45+ with pytest .raises (httpx .ReadTimeout ):
46+ httpx .get (
47+ server .url .copy_with (path = "/slow_response" ),
48+ timeout = httpx .Timeout (5 , read = 0.01 ),
49+ )
7950
8051
8152def test_request_attribute () -> None :
0 commit comments