Skip to content

Commit 7488b15

Browse files
Drop private imports from test_exceptions.py (#2571)
1 parent 2f0c291 commit 7488b15

File tree

2 files changed

+24
-53
lines changed

2 files changed

+24
-53
lines changed

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,14 +97,14 @@ async def hello_world_json(scope: _Scope, receive: "_Receive", send: "_Send") ->
9797

9898

9999
async def slow_response(scope: _Scope, receive: "_Receive", send: "_Send") -> None:
100-
await sleep(1.0)
101100
await send(
102101
{
103102
"type": "http.response.start",
104103
"status": 200,
105104
"headers": [[b"content-type", b"text/plain"]],
106105
}
107106
)
107+
await sleep(1.0) # Allow triggering a read timeout.
108108
await send({"type": "http.response.body", "body": b"Hello, world!"})
109109

110110

tests/test_exceptions.py

Lines changed: 23 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
11
import typing
2-
from unittest import mock
32

43
import httpcore
54
import pytest
65

76
import httpx
8-
from httpx._transports.default import HTTPCORE_EXC_MAP
97

108
if 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

8152
def test_request_attribute() -> None:

0 commit comments

Comments
 (0)