Skip to content
Myr

test_exceptions.py

This file

Path

tests/test_exceptions.py

63 lines · 1.8 KiB · Python

Last changed by

Use `__future__.annotations` (#3068)Kar Petrosyanbefore adoption371b6e9

History of this file

Take it

1from __future__ import annotations23import typing45import httpcore6import pytest78import httpx910if typing.TYPE_CHECKING:  # pragma: no cover11    from conftest import TestServer121314def test_httpcore_all_exceptions_mapped() -> None:15    """16    All exception classes exposed by HTTPCore are properly mapped to an HTTPX-specific17    exception class.18    """19    expected_mapped_httpcore_exceptions = {20        value.__name__21        for _, value in vars(httpcore).items()22        if isinstance(value, type)23        and issubclass(value, Exception)24        and value is not httpcore.ConnectionNotAvailable25    }2627    httpx_exceptions = {28        value.__name__29        for _, value in vars(httpx).items()30        if isinstance(value, type) and issubclass(value, Exception)31    }3233    unmapped_exceptions = expected_mapped_httpcore_exceptions - httpx_exceptions3435    if unmapped_exceptions:  # pragma: no cover36        pytest.fail(f"Unmapped httpcore exceptions: {unmapped_exceptions}")373839def test_httpcore_exception_mapping(server: TestServer) -> None:40    """41    HTTPCore exception mapping works as expected.42    """43    impossible_port = 12345644    with pytest.raises(httpx.ConnectError):45        httpx.get(server.url.copy_with(port=impossible_port))4647    with pytest.raises(httpx.ReadTimeout):48        httpx.get(49            server.url.copy_with(path="/slow_response"),50            timeout=httpx.Timeout(5, read=0.01),51        )525354def test_request_attribute() -> None:55    # Exception without request attribute56    exc = httpx.ReadTimeout("Read operation timed out")57    with pytest.raises(RuntimeError):58        exc.request  # noqa: B0185960    # Exception with request attribute61    request = httpx.Request("GET", "https://www.example.com")62    exc = httpx.ReadTimeout("Read operation timed out", request=request)63    assert exc.request == request