|
| 1 | +import pytest |
| 2 | +import respx |
| 3 | +from httpx import ConnectTimeout, Response, codes |
| 4 | + |
| 5 | +from mpt_api_client.http.client import HTTPClientAsync |
| 6 | +from tests.conftest import API_TOKEN, API_URL |
| 7 | + |
| 8 | + |
| 9 | +def test_mpt_client_initialization(): |
| 10 | + client = HTTPClientAsync(base_url=API_URL, api_token=API_TOKEN) |
| 11 | + |
| 12 | + assert client.base_url == API_URL |
| 13 | + assert client.headers["Authorization"] == "Bearer test-token" |
| 14 | + assert client.headers["User-Agent"] == "swo-marketplace-client/1.0" |
| 15 | + |
| 16 | + |
| 17 | +def test_env_initialization(monkeypatch): |
| 18 | + monkeypatch.setenv("MPT_TOKEN", API_TOKEN) |
| 19 | + monkeypatch.setenv("MPT_URL", API_URL) |
| 20 | + |
| 21 | + client = HTTPClientAsync() |
| 22 | + |
| 23 | + assert client.base_url == API_URL |
| 24 | + assert client.headers["Authorization"] == f"Bearer {API_TOKEN}" |
| 25 | + |
| 26 | + |
| 27 | +def test_mpt_client_without_token(): |
| 28 | + with pytest.raises(ValueError): |
| 29 | + HTTPClientAsync(base_url=API_URL) |
| 30 | + |
| 31 | + |
| 32 | +def test_mpt_client_without_url(): |
| 33 | + with pytest.raises(ValueError): |
| 34 | + HTTPClientAsync(api_token=API_TOKEN) |
| 35 | + |
| 36 | + |
| 37 | +@respx.mock |
| 38 | +async def test_mock_call_success(http_client_async): |
| 39 | + success_route = respx.get(f"{API_URL}/").mock( |
| 40 | + return_value=Response(200, json={"message": "Hello, World!"}) |
| 41 | + ) |
| 42 | + |
| 43 | + success_response = await http_client_async.get("/") |
| 44 | + |
| 45 | + assert success_response.status_code == codes.OK |
| 46 | + assert success_response.json() == {"message": "Hello, World!"} |
| 47 | + assert success_route.called |
| 48 | + |
| 49 | + |
| 50 | +@respx.mock |
| 51 | +async def test_mock_call_failure(http_client_async): |
| 52 | + timeout_route = respx.get(f"{API_URL}/timeout").mock(side_effect=ConnectTimeout("Mock Timeout")) |
| 53 | + |
| 54 | + with pytest.raises(ConnectTimeout): |
| 55 | + await http_client_async.get("/timeout") |
| 56 | + |
| 57 | + assert timeout_route.called |
0 commit comments