Drop private imports from test_exceptions.py (#2571)
This commit is contained in:
parent
2f0c291435
commit
7488b15226
@ -97,7 +97,6 @@ async def hello_world_json(scope: _Scope, receive: "_Receive", send: "_Send") ->
|
||||
|
||||
|
||||
async def slow_response(scope: _Scope, receive: "_Receive", send: "_Send") -> None:
|
||||
await sleep(1.0)
|
||||
await send(
|
||||
{
|
||||
"type": "http.response.start",
|
||||
@ -105,6 +104,7 @@ async def slow_response(scope: _Scope, receive: "_Receive", send: "_Send") -> No
|
||||
"headers": [[b"content-type", b"text/plain"]],
|
||||
}
|
||||
)
|
||||
await sleep(1.0) # Allow triggering a read timeout.
|
||||
await send({"type": "http.response.body", "body": b"Hello, world!"})
|
||||
|
||||
|
||||
|
||||
@ -1,11 +1,9 @@
|
||||
import typing
|
||||
from unittest import mock
|
||||
|
||||
import httpcore
|
||||
import pytest
|
||||
|
||||
import httpx
|
||||
from httpx._transports.default import HTTPCORE_EXC_MAP
|
||||
|
||||
if typing.TYPE_CHECKING: # pragma: no cover
|
||||
from conftest import TestServer
|
||||
@ -16,66 +14,39 @@ def test_httpcore_all_exceptions_mapped() -> None:
|
||||
All exception classes exposed by HTTPCore are properly mapped to an HTTPX-specific
|
||||
exception class.
|
||||
"""
|
||||
not_mapped = [
|
||||
value
|
||||
for name, value in vars(httpcore).items()
|
||||
expected_mapped_httpcore_exceptions = {
|
||||
value.__name__
|
||||
for _, value in vars(httpcore).items()
|
||||
if isinstance(value, type)
|
||||
and issubclass(value, Exception)
|
||||
and value not in HTTPCORE_EXC_MAP
|
||||
and value is not httpcore.ConnectionNotAvailable
|
||||
]
|
||||
}
|
||||
|
||||
if not_mapped: # pragma: no cover
|
||||
pytest.fail(f"Unmapped httpcore exceptions: {not_mapped}")
|
||||
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))
|
||||
|
||||
def connect_failed(*args, **kwargs):
|
||||
raise httpcore.ConnectError()
|
||||
|
||||
class TimeoutStream:
|
||||
def __iter__(self):
|
||||
raise httpcore.ReadTimeout()
|
||||
|
||||
def close(self):
|
||||
pass
|
||||
|
||||
with mock.patch(
|
||||
"httpcore.ConnectionPool.handle_request", side_effect=connect_failed
|
||||
):
|
||||
with pytest.raises(httpx.ConnectError):
|
||||
httpx.get(server.url)
|
||||
|
||||
with mock.patch(
|
||||
"httpcore.ConnectionPool.handle_request",
|
||||
return_value=httpcore.Response(
|
||||
200, headers=[], content=TimeoutStream(), extensions={}
|
||||
),
|
||||
):
|
||||
with pytest.raises(httpx.ReadTimeout):
|
||||
httpx.get(server.url)
|
||||
|
||||
|
||||
def test_httpx_exceptions_exposed() -> None:
|
||||
"""
|
||||
All exception classes defined in `httpx._exceptions`
|
||||
are exposed as public API.
|
||||
"""
|
||||
|
||||
not_exposed = [
|
||||
value
|
||||
for name, value in vars(httpx._exceptions).items()
|
||||
if isinstance(value, type)
|
||||
and issubclass(value, Exception)
|
||||
and not hasattr(httpx, name)
|
||||
]
|
||||
|
||||
if not_exposed: # pragma: no cover
|
||||
pytest.fail(f"Unexposed HTTPX exceptions: {not_exposed}")
|
||||
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:
|
||||
|
||||
Loading…
Reference in New Issue
Block a user