Skip to content

Commit 8a35315

Browse files
committed
update test for error stream
1 parent c279f5d commit 8a35315

File tree

1 file changed

+30
-31
lines changed

1 file changed

+30
-31
lines changed

tests/unit/test_client_errors.py

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,20 @@
11
from __future__ import annotations
22

33
import json
4+
import time
45
from typing import TYPE_CHECKING
56

6-
import httpx
77
import pytest
8-
import respx
8+
from werkzeug import Response
99

1010
from apify_client._errors import ApifyApiError
1111
from apify_client._http_client import HTTPClient, HTTPClientAsync
1212

1313
if TYPE_CHECKING:
14-
from collections.abc import AsyncIterator, Iterator
14+
from collections.abc import Iterator
1515

1616
from pytest_httpserver import HTTPServer
17+
from werkzeug import Request
1718

1819
_TEST_PATH = '/errors'
1920
_EXPECTED_MESSAGE = 'some_message'
@@ -41,6 +42,22 @@ def test_endpoint(httpserver: HTTPServer) -> str:
4142
return str(httpserver.url_for(_TEST_PATH))
4243

4344

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))},
58+
)
59+
60+
4461
def test_client_apify_api_error_with_data(test_endpoint: str) -> None:
4562
"""Test that client correctly throws ApifyApiError with error data from response."""
4663
client = HTTPClient()
@@ -65,51 +82,33 @@ async def test_async_client_apify_api_error_with_data(test_endpoint: str) -> Non
6582
assert e.value.data == _EXPECTED_DATA
6683

6784

68-
def test_client_apify_api_error_streamed() -> None:
85+
def test_client_apify_api_error_streamed(httpserver: HTTPServer) -> None:
6986
"""Test that client correctly throws ApifyApiError when the response has stream."""
7087

7188
error = json.loads(RAW_ERROR.decode())
7289

73-
class ByteStream(httpx._types.SyncByteStream):
74-
def __iter__(self) -> Iterator[bytes]:
75-
yield RAW_ERROR
76-
77-
def close(self) -> None:
78-
pass
79-
80-
stream_url = 'http://some-stream-url.com'
81-
8290
client = HTTPClient()
8391

84-
with respx.mock() as respx_mock:
85-
respx_mock.get(url=stream_url).mock(return_value=httpx.Response(stream=ByteStream(), status_code=403))
86-
with pytest.raises(ApifyApiError) as e:
87-
client.call(method='GET', url=stream_url, stream=True, parse_response=False)
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)
8896

8997
assert e.value.message == error['error']['message']
9098
assert e.value.type == error['error']['type']
9199

92100

93-
async def test_async_client_apify_api_error_streamed() -> None:
101+
async def test_async_client_apify_api_error_streamed(httpserver: HTTPServer) -> None:
94102
"""Test that async client correctly throws ApifyApiError when the response has stream."""
95103

96104
error = json.loads(RAW_ERROR.decode())
97105

98-
class AsyncByteStream(httpx._types.AsyncByteStream):
99-
async def __aiter__(self) -> AsyncIterator[bytes]:
100-
yield RAW_ERROR
101-
102-
async def aclose(self) -> None:
103-
pass
104-
105-
stream_url = 'http://some-stream-url.com'
106-
107106
client = HTTPClientAsync()
108107

109-
with respx.mock() as respx_mock:
110-
respx_mock.get(url=stream_url).mock(return_value=httpx.Response(stream=AsyncByteStream(), status_code=403))
111-
with pytest.raises(ApifyApiError) as e:
112-
await client.call(method='GET', url=stream_url, stream=True, parse_response=False)
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)
113112

114113
assert e.value.message == error['error']['message']
115114
assert e.value.type == error['error']['type']

0 commit comments

Comments
 (0)