1
1
from __future__ import annotations
2
2
3
3
import json
4
+ import time
4
5
from typing import TYPE_CHECKING
5
6
6
- import httpx
7
7
import pytest
8
- import respx
8
+ from werkzeug import Response
9
9
10
10
from apify_client ._errors import ApifyApiError
11
11
from apify_client ._http_client import HTTPClient , HTTPClientAsync
12
12
13
13
if TYPE_CHECKING :
14
- from collections .abc import AsyncIterator , Iterator
14
+ from collections .abc import Iterator
15
15
16
16
from pytest_httpserver import HTTPServer
17
+ from werkzeug import Request
17
18
18
19
_TEST_PATH = '/errors'
19
20
_EXPECTED_MESSAGE = 'some_message'
@@ -41,6 +42,22 @@ def test_endpoint(httpserver: HTTPServer) -> str:
41
42
return str (httpserver .url_for (_TEST_PATH ))
42
43
43
44
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
+
44
61
def test_client_apify_api_error_with_data (test_endpoint : str ) -> None :
45
62
"""Test that client correctly throws ApifyApiError with error data from response."""
46
63
client = HTTPClient ()
@@ -65,51 +82,33 @@ async def test_async_client_apify_api_error_with_data(test_endpoint: str) -> Non
65
82
assert e .value .data == _EXPECTED_DATA
66
83
67
84
68
- def test_client_apify_api_error_streamed () -> None :
85
+ def test_client_apify_api_error_streamed (httpserver : HTTPServer ) -> None :
69
86
"""Test that client correctly throws ApifyApiError when the response has stream."""
70
87
71
88
error = json .loads (RAW_ERROR .decode ())
72
89
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
-
82
90
client = HTTPClient ()
83
91
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 )
88
96
89
97
assert e .value .message == error ['error' ]['message' ]
90
98
assert e .value .type == error ['error' ]['type' ]
91
99
92
100
93
- async def test_async_client_apify_api_error_streamed () -> None :
101
+ async def test_async_client_apify_api_error_streamed (httpserver : HTTPServer ) -> None :
94
102
"""Test that async client correctly throws ApifyApiError when the response has stream."""
95
103
96
104
error = json .loads (RAW_ERROR .decode ())
97
105
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
-
107
106
client = HTTPClientAsync ()
108
107
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 )
113
112
114
113
assert e .value .message == error ['error' ]['message' ]
115
114
assert e .value .type == error ['error' ]['type' ]
0 commit comments