|
| 1 | +"""Static-typing tests for AsyncClient overloads. |
| 2 | +
|
| 3 | +These assert overload selection at runtime via isinstance checks. ty/mypy |
| 4 | +catches the static-typing variant during `just lint`. |
| 5 | +""" |
| 6 | + |
| 7 | +from http import HTTPStatus |
| 8 | + |
| 9 | +import httpx2 |
| 10 | +import pydantic |
| 11 | + |
| 12 | +from httpware import AsyncClient |
| 13 | + |
| 14 | + |
| 15 | +class _User(pydantic.BaseModel): |
| 16 | + id: int |
| 17 | + name: str |
| 18 | + |
| 19 | + |
| 20 | +async def test_get_without_response_model_returns_response() -> None: |
| 21 | + transport = httpx2.MockTransport( |
| 22 | + lambda req: httpx2.Response(HTTPStatus.OK, request=req, json={"id": 1, "name": "a"}) |
| 23 | + ) |
| 24 | + client = AsyncClient(httpx2_client=httpx2.AsyncClient(transport=transport)) |
| 25 | + result = await client.get("https://example.test/x") |
| 26 | + assert isinstance(result, httpx2.Response) |
| 27 | + |
| 28 | + |
| 29 | +async def test_get_with_response_model_returns_typed() -> None: |
| 30 | + transport = httpx2.MockTransport( |
| 31 | + lambda req: httpx2.Response(HTTPStatus.OK, request=req, json={"id": 1, "name": "a"}) |
| 32 | + ) |
| 33 | + client = AsyncClient(httpx2_client=httpx2.AsyncClient(transport=transport)) |
| 34 | + result = await client.get("https://example.test/x", response_model=_User) |
| 35 | + assert isinstance(result, _User) |
| 36 | + |
| 37 | + |
| 38 | +async def test_send_without_response_model_returns_response() -> None: |
| 39 | + transport = httpx2.MockTransport( |
| 40 | + lambda req: httpx2.Response(HTTPStatus.OK, request=req, json={"id": 1, "name": "a"}) |
| 41 | + ) |
| 42 | + client = AsyncClient(httpx2_client=httpx2.AsyncClient(transport=transport)) |
| 43 | + result = await client.send(httpx2.Request("GET", "https://example.test/x")) |
| 44 | + assert isinstance(result, httpx2.Response) |
| 45 | + |
| 46 | + |
| 47 | +async def test_send_with_response_model_returns_typed() -> None: |
| 48 | + transport = httpx2.MockTransport( |
| 49 | + lambda req: httpx2.Response(HTTPStatus.OK, request=req, json={"id": 1, "name": "a"}) |
| 50 | + ) |
| 51 | + client = AsyncClient(httpx2_client=httpx2.AsyncClient(transport=transport)) |
| 52 | + result = await client.send(httpx2.Request("GET", "https://example.test/x"), response_model=_User) |
| 53 | + assert isinstance(result, _User) |
0 commit comments