Skip to content
Myr

test_exceptions.py

This file

Path

tests/test_exceptions.py

61 lines · 1.6 KiB · Python

Last changed by

Drop deprecated API (#1057)Tom Christiebefore adoption3f96e74

History of this file

Take it

1from typing import Any23import httpcore4import pytest56import httpx7from httpx._exceptions import HTTPCORE_EXC_MAP8910def test_httpcore_all_exceptions_mapped() -> None:11    """12    All exception classes exposed by HTTPCore are properly mapped to an HTTPX-specific13    exception class.14    """15    not_mapped = [16        value17        for name, value in vars(httpcore).items()18        if isinstance(value, type)19        and issubclass(value, Exception)20        and value not in HTTPCORE_EXC_MAP21    ]2223    if not_mapped:  # pragma: nocover24        pytest.fail(f"Unmapped httpcore exceptions: {not_mapped}")252627def test_httpcore_exception_mapping() -> None:28    """29    HTTPCore exception mapping works as expected.30    """3132    # Make sure we don't just map to `NetworkError`.33    with pytest.raises(httpx.ConnectError):34        httpx.get("http://doesnotexist")3536    # Make sure it also works with custom transports.37    class MockTransport(httpcore.SyncHTTPTransport):38        def request(self, *args: Any, **kwargs: Any) -> Any:39            raise httpcore.ProtocolError()4041    client = httpx.Client(transport=MockTransport())42    with pytest.raises(httpx.ProtocolError):43        client.get("http://testserver")444546def test_httpx_exceptions_exposed() -> None:47    """48    All exception classes defined in `httpx._exceptions`49    are exposed as public API.50    """5152    not_exposed = [53        value54        for name, value in vars(httpx._exceptions).items()55        if isinstance(value, type)56        and issubclass(value, Exception)57        and not hasattr(httpx, name)58    ]5960    if not_exposed:  # pragma: nocover61        pytest.fail(f"Unexposed HTTPX exceptions: {not_exposed}")