Skip to content
Myr

test_exceptions.py

This file

Path

tests/test_exceptions.py

61 lines · 1.8 KiB · Python

Last changed by

Drop private imports from test_exceptions.py (#2571)Florimond Mancabefore adoption7488b15

History of this file

Take it

1import typing23import httpcore4import pytest56import httpx78if typing.TYPE_CHECKING:  # pragma: no cover9    from conftest import TestServer101112def test_httpcore_all_exceptions_mapped() -> None:13    """14    All exception classes exposed by HTTPCore are properly mapped to an HTTPX-specific15    exception class.16    """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)22        and value is not httpcore.ConnectionNotAvailable23    }2425    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))4445    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        )505152def test_request_attribute() -> None:53    # Exception without request attribute54    exc = httpx.ReadTimeout("Read operation timed out")55    with pytest.raises(RuntimeError):56        exc.request5758    # Exception with request attribute59    request = httpx.Request("GET", "https://www.example.com")60    exc = httpx.ReadTimeout("Read operation timed out", request=request)61    assert exc.request == request