-
-
Notifications
You must be signed in to change notification settings - Fork 883
/
Copy pathtest_exceptions.py
80 lines (63 loc) · 2.54 KB
/
test_exceptions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
from __future__ import annotations
import pickle
import typing
import httpcore
import pytest
import httpx
if typing.TYPE_CHECKING: # pragma: no cover
from conftest import TestServer
def test_httpcore_all_exceptions_mapped() -> None:
"""
All exception classes exposed by HTTPCore are properly mapped to an HTTPX-specific
exception class.
"""
expected_mapped_httpcore_exceptions = {
value.__name__
for _, value in vars(httpcore).items()
if isinstance(value, type)
and issubclass(value, Exception)
and value is not httpcore.ConnectionNotAvailable
}
httpx_exceptions = {
value.__name__
for _, value in vars(httpx).items()
if isinstance(value, type) and issubclass(value, Exception)
}
unmapped_exceptions = expected_mapped_httpcore_exceptions - httpx_exceptions
if unmapped_exceptions: # pragma: no cover
pytest.fail(f"Unmapped httpcore exceptions: {unmapped_exceptions}")
def test_httpcore_exception_mapping(server: TestServer) -> None:
"""
HTTPCore exception mapping works as expected.
"""
impossible_port = 123456
with pytest.raises(httpx.ConnectError):
httpx.get(server.url.copy_with(port=impossible_port))
with pytest.raises(httpx.ReadTimeout):
httpx.get(
server.url.copy_with(path="/slow_response"),
timeout=httpx.Timeout(5, read=0.01),
)
def test_request_attribute() -> None:
# Exception without request attribute
exc = httpx.ReadTimeout("Read operation timed out")
with pytest.raises(RuntimeError):
exc.request # noqa: B018
# Exception with request attribute
request = httpx.Request("GET", "https://www.example.com")
exc = httpx.ReadTimeout("Read operation timed out", request=request)
assert exc.request == request
def test_pickle_error(server):
with httpx.Client() as client:
response = client.request("GET", server.url.copy_with(path="/status/404"))
with pytest.raises(httpx.HTTPStatusError) as exc_info:
response.raise_for_status()
error = exc_info.value
assert isinstance(error, httpx.HTTPStatusError)
pickled_error = pickle.dumps(error)
unpickled_error = pickle.loads(pickled_error)
# Note that the unpickled error will not be equal to the original error
# because requests and responses are compared by identity.
assert str(unpickled_error) == str(error)
assert unpickled_error.request is not None
assert unpickled_error.response is not None