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