|
| 1 | +import json |
| 2 | + |
| 3 | +import httpx |
| 4 | +import pytest |
| 5 | +import respx |
| 6 | +from respx import MockRouter |
| 7 | + |
| 8 | +from apify_client._errors import ApifyApiError |
| 9 | +from apify_client._http_client import HTTPClient, HTTPClientAsync |
| 10 | + |
| 11 | +_TEST_URL = 'http://example.com' |
| 12 | +_EXPECTED_MESSAGE = 'some_message' |
| 13 | +_EXPECTED_TYPE = 'some_type' |
| 14 | +_EXPECTED_DATA = { |
| 15 | + 'invalidItems': {'0': ["should have required property 'name'"], '1': ["should have required property 'name'"]} |
| 16 | +} |
| 17 | + |
| 18 | + |
| 19 | +@respx.mock |
| 20 | +@pytest.fixture(autouse=True) |
| 21 | +def mocked_response(respx_mock: MockRouter) -> None: |
| 22 | + response_content = json.dumps( |
| 23 | + {'error': {'message': _EXPECTED_MESSAGE, 'type': _EXPECTED_TYPE, 'data': _EXPECTED_DATA}} |
| 24 | + ) |
| 25 | + respx_mock.get(_TEST_URL).mock(return_value=httpx.Response(400, content=response_content)) |
| 26 | + |
| 27 | + |
| 28 | +def test_client_apify_api_error_with_data() -> None: |
| 29 | + """Test that client correctly throws ApifyApiError with error data from response.""" |
| 30 | + client = HTTPClient() |
| 31 | + |
| 32 | + with pytest.raises(ApifyApiError) as e: |
| 33 | + client.call(method='GET', url=_TEST_URL) |
| 34 | + |
| 35 | + assert e.value.message == _EXPECTED_MESSAGE |
| 36 | + assert e.value.type == _EXPECTED_TYPE |
| 37 | + assert e.value.data == _EXPECTED_DATA |
| 38 | + |
| 39 | + |
| 40 | +async def test_async_client_apify_api_error_with_data() -> None: |
| 41 | + """Test that async client correctly throws ApifyApiError with error data from response.""" |
| 42 | + client = HTTPClientAsync() |
| 43 | + |
| 44 | + with pytest.raises(ApifyApiError) as e: |
| 45 | + await client.call(method='GET', url=_TEST_URL) |
| 46 | + |
| 47 | + assert e.value.message == _EXPECTED_MESSAGE |
| 48 | + assert e.value.type == _EXPECTED_TYPE |
| 49 | + assert e.value.data == _EXPECTED_DATA |
0 commit comments