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
Provenance
- Commit
7488b15- Tree
5a18da3- Author
Florimond Manca <florimond.manca@protonmail.com>- Committer
GitHub <noreply@github.com>- Parent
2f0c291
2 files changed+23 added−52 removed
modified:tests/conftest.py+1 added-1 removed
95 unmodified lines96 await send({"type": "http.response.body", "body": b'{"Hello": "world!"}'})97 98 99 async 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!"})109 110 111 async def status_code(scope: _Scope, receive: "_Receive", send: "_Send") -> None:194 unmodified linesmodified:tests/test_exceptions.py+22 added-51 removed
1 import typing2-from unittest import mock2 3 import httpcore4 import pytest5 6 import httpx8-from httpx._transports.default import HTTPCORE_EXC_MAP7 8 if typing.TYPE_CHECKING: # pragma: no cover9 from conftest import TestServer10 2 unmodified lines13 """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+ }24 28- 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+ }30 31+ unmapped_exceptions = expected_mapped_httpcore_exceptions - httpx_exceptions32 33+ if unmapped_exceptions: # pragma: no cover34+ pytest.fail(f"Unmapped httpcore exceptions: {unmapped_exceptions}")35+36+37 def 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))44 37- 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+ )50 40- class TimeoutStream:41- def __iter__(self):42- raise httpcore.ReadTimeout()51 44- def close(self):45- pass46-47- with mock.patch(48- "httpcore.ConnectionPool.handle_request", side_effect=connect_failed49- ):50- with pytest.raises(httpx.ConnectError):51- httpx.get(server.url)52-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)61-62-63-def test_httpx_exceptions_exposed() -> None:64- """65- All exception classes defined in `httpx._exceptions`66- are exposed as public API.67- """68-69- 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- ]76-77- if not_exposed: # pragma: no cover78- pytest.fail(f"Unexposed HTTPX exceptions: {not_exposed}")79-80-52 def test_request_attribute() -> None:53 # Exception without request attribute54 exc = httpx.ReadTimeout("Read operation timed out")55 with pytest.raises(RuntimeError):6 unmodified lines