|
| 1 | +from __future__ import annotations |
| 2 | + |
1 | 3 | import json |
2 | | -from collections.abc import Generator |
| 4 | +import time |
| 5 | +from typing import TYPE_CHECKING |
3 | 6 |
|
4 | | -import httpx |
5 | 7 | import pytest |
6 | | -import respx |
| 8 | +from werkzeug import Response |
7 | 9 |
|
8 | 10 | from apify_client._errors import ApifyApiError |
9 | 11 | from apify_client._http_client import HTTPClient, HTTPClientAsync |
10 | 12 |
|
11 | | -_TEST_URL = 'http://example.com' |
| 13 | +if TYPE_CHECKING: |
| 14 | + from collections.abc import Iterator |
| 15 | + |
| 16 | + from pytest_httpserver import HTTPServer |
| 17 | + from werkzeug import Request |
| 18 | + |
| 19 | +_TEST_PATH = '/errors' |
12 | 20 | _EXPECTED_MESSAGE = 'some_message' |
13 | 21 | _EXPECTED_TYPE = 'some_type' |
14 | 22 | _EXPECTED_DATA = { |
15 | 23 | 'invalidItems': {'0': ["should have required property 'name'"], '1': ["should have required property 'name'"]} |
16 | 24 | } |
17 | 25 |
|
| 26 | +RAW_ERROR = ( |
| 27 | + b'{\n' |
| 28 | + b' "error": {\n' |
| 29 | + b' "type": "insufficient-permissions",\n' |
| 30 | + b' "message": "Insufficient permissions for the Actor run. Make sure you\'' |
| 31 | + b're passing a correct API token and that it has the required permissions."\n' |
| 32 | + b' }\n' |
| 33 | + b'}' |
| 34 | +) |
| 35 | + |
| 36 | + |
| 37 | +@pytest.fixture |
| 38 | +def test_endpoint(httpserver: HTTPServer) -> str: |
| 39 | + httpserver.expect_request(_TEST_PATH).respond_with_json( |
| 40 | + {'error': {'message': _EXPECTED_MESSAGE, 'type': _EXPECTED_TYPE, 'data': _EXPECTED_DATA}}, status=400 |
| 41 | + ) |
| 42 | + return str(httpserver.url_for(_TEST_PATH)) |
| 43 | + |
18 | 44 |
|
19 | | -@pytest.fixture(autouse=True) |
20 | | -def mocked_response() -> Generator[respx.MockRouter]: |
21 | | - response_content = json.dumps( |
22 | | - {'error': {'message': _EXPECTED_MESSAGE, 'type': _EXPECTED_TYPE, 'data': _EXPECTED_DATA}} |
| 45 | +def streaming_handler(_request: Request) -> Response: |
| 46 | + """Handler for streaming log requests.""" |
| 47 | + |
| 48 | + def generate_response() -> Iterator[bytes]: |
| 49 | + for i in range(len(RAW_ERROR)): |
| 50 | + yield RAW_ERROR[i : i + 1] |
| 51 | + time.sleep(0.01) |
| 52 | + |
| 53 | + return Response( |
| 54 | + response=(RAW_ERROR[i : i + 1] for i in range(len(RAW_ERROR))), |
| 55 | + status=403, |
| 56 | + mimetype='application/octet-stream', |
| 57 | + headers={'Content-Length': str(len(RAW_ERROR))}, |
23 | 58 | ) |
24 | | - with respx.mock() as respx_mock: |
25 | | - respx_mock.get(_TEST_URL).mock(return_value=httpx.Response(400, content=response_content)) |
26 | | - yield respx_mock |
27 | 59 |
|
28 | 60 |
|
29 | | -def test_client_apify_api_error_with_data() -> None: |
| 61 | +def test_client_apify_api_error_with_data(test_endpoint: str) -> None: |
30 | 62 | """Test that client correctly throws ApifyApiError with error data from response.""" |
31 | 63 | client = HTTPClient() |
32 | 64 |
|
33 | 65 | with pytest.raises(ApifyApiError) as e: |
34 | | - client.call(method='GET', url=_TEST_URL) |
| 66 | + client.call(method='GET', url=test_endpoint) |
35 | 67 |
|
36 | 68 | assert e.value.message == _EXPECTED_MESSAGE |
37 | 69 | assert e.value.type == _EXPECTED_TYPE |
38 | 70 | assert e.value.data == _EXPECTED_DATA |
39 | 71 |
|
40 | 72 |
|
41 | | -async def test_async_client_apify_api_error_with_data() -> None: |
| 73 | +async def test_async_client_apify_api_error_with_data(test_endpoint: str) -> None: |
42 | 74 | """Test that async client correctly throws ApifyApiError with error data from response.""" |
43 | 75 | client = HTTPClientAsync() |
44 | 76 |
|
45 | 77 | with pytest.raises(ApifyApiError) as e: |
46 | | - await client.call(method='GET', url=_TEST_URL) |
| 78 | + await client.call(method='GET', url=test_endpoint) |
47 | 79 |
|
48 | 80 | assert e.value.message == _EXPECTED_MESSAGE |
49 | 81 | assert e.value.type == _EXPECTED_TYPE |
50 | 82 | assert e.value.data == _EXPECTED_DATA |
| 83 | + |
| 84 | + |
| 85 | +def test_client_apify_api_error_streamed(httpserver: HTTPServer) -> None: |
| 86 | + """Test that client correctly throws ApifyApiError when the response has stream.""" |
| 87 | + |
| 88 | + error = json.loads(RAW_ERROR.decode()) |
| 89 | + |
| 90 | + client = HTTPClient() |
| 91 | + |
| 92 | + httpserver.expect_request('/stream_error').respond_with_handler(streaming_handler) |
| 93 | + |
| 94 | + with pytest.raises(ApifyApiError) as e: |
| 95 | + client.call(method='GET', url=httpserver.url_for('/stream_error'), stream=True, parse_response=False) |
| 96 | + |
| 97 | + assert e.value.message == error['error']['message'] |
| 98 | + assert e.value.type == error['error']['type'] |
| 99 | + |
| 100 | + |
| 101 | +async def test_async_client_apify_api_error_streamed(httpserver: HTTPServer) -> None: |
| 102 | + """Test that async client correctly throws ApifyApiError when the response has stream.""" |
| 103 | + |
| 104 | + error = json.loads(RAW_ERROR.decode()) |
| 105 | + |
| 106 | + client = HTTPClientAsync() |
| 107 | + |
| 108 | + httpserver.expect_request('/stream_error').respond_with_handler(streaming_handler) |
| 109 | + |
| 110 | + with pytest.raises(ApifyApiError) as e: |
| 111 | + await client.call(method='GET', url=httpserver.url_for('/stream_error'), stream=True, parse_response=False) |
| 112 | + |
| 113 | + assert e.value.message == error['error']['message'] |
| 114 | + assert e.value.type == error['error']['type'] |
0 commit comments