Skip to content
Myr

Drop private imports from test_exceptions.py (#2571)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Florimond Manca · committed by GitHub · parent 2f0c291 · Browse files · 2 files+23 added−52 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+23 added−52 removed

  • tests:+23 added−52 removed

2 files+23 added−52 removed

modified:tests/conftest.py+1 added-1 removed
95 unmodified lines
96 await send({"type": "http.response.body", "body": b'{"Hello": "world!"}'})979899async def slow_response(scope: _Scope, receive: "_Receive", send: "_Send") -> None:100 await sleep(1.0)100 await send(101 {102 "type": "http.response.start",103 "status": 200,104 "headers": [[b"content-type", b"text/plain"]],105 }106 )107 await sleep(1.0) # Allow triggering a read timeout.108 await send({"type": "http.response.body", "body": b"Hello, world!"})109110111async def status_code(scope: _Scope, receive: "_Receive", send: "_Send") -> None:
194 unmodified lines
modified:tests/test_exceptions.py+22 added-51 removed
1import typing2from unittest import mock23import httpcore4import pytest56import httpx8from httpx._transports.default import HTTPCORE_EXC_MAP78if typing.TYPE_CHECKING: # pragma: no cover9 from conftest import TestServer10
2 unmodified lines
13 """14 All exception classes exposed by HTTPCore are properly mapped to an HTTPX-specific15 exception class.16 """19 not_mapped = [20 value21 for name, value in vars(httpcore).items()17 expected_mapped_httpcore_exceptions = {18 value.__name__19 for _, value in vars(httpcore).items()20 if isinstance(value, type)21 and issubclass(value, Exception)24 and value not in HTTPCORE_EXC_MAP22 and value is not httpcore.ConnectionNotAvailable26 ]23 }2428 if not_mapped: # pragma: no cover29 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 }3031 unmapped_exceptions = expected_mapped_httpcore_exceptions - httpx_exceptions3233 if unmapped_exceptions: # pragma: no cover34 pytest.fail(f"Unmapped httpcore exceptions: {unmapped_exceptions}")353637def test_httpcore_exception_mapping(server: "TestServer") -> None:38 """39 HTTPCore exception mapping works as expected.40 """41 impossible_port = 12345642 with pytest.raises(httpx.ConnectError):43 httpx.get(server.url.copy_with(port=impossible_port))4437 def connect_failed(*args, **kwargs):38 raise httpcore.ConnectError()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 )5040 class TimeoutStream:41 def __iter__(self):42 raise httpcore.ReadTimeout()5144 def close(self):45 pass4647 with mock.patch(48 "httpcore.ConnectionPool.handle_request", side_effect=connect_failed49 ):50 with pytest.raises(httpx.ConnectError):51 httpx.get(server.url)5253 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)616263def test_httpx_exceptions_exposed() -> None:64 """65 All exception classes defined in `httpx._exceptions`66 are exposed as public API.67 """6869 not_exposed = [70 value71 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 ]7677 if not_exposed: # pragma: no cover78 pytest.fail(f"Unexposed HTTPX exceptions: {not_exposed}")798052def test_request_attribute() -> None:53 # Exception without request attribute54 exc = httpx.ReadTimeout("Read operation timed out")55 with pytest.raises(RuntimeError):
6 unmodified lines