|
6 | 6 | from apify_client.http_clients import HttpResponse |
7 | 7 |
|
8 | 8 | if TYPE_CHECKING: |
| 9 | + from typing import Any |
| 10 | + |
9 | 11 | from pytest_httpserver import HTTPServer |
10 | 12 |
|
11 | 13 | from apify_client.http_clients import HttpClient, HttpClientAsync |
@@ -87,3 +89,41 @@ async def test_key_value_store_stream_record_async( |
87 | 89 | response = record['value'] |
88 | 90 | assert isinstance(response, HttpResponse) |
89 | 91 | assert await response.aread() == STREAM_CONTENT |
| 92 | + |
| 93 | + |
| 94 | +def test_protocol_check_leaves_stream_unread_sync( |
| 95 | + httpserver: HTTPServer, |
| 96 | + http_client_class: type[HttpClient], |
| 97 | +) -> None: |
| 98 | + """Checking a streaming response against the protocol inspects it without pulling the body off the wire.""" |
| 99 | + httpserver.expect_request(f'/v2/datasets/{DATASET_ID}/items').respond_with_data(STREAM_CONTENT) |
| 100 | + api_url = httpserver.url_for('/').removesuffix('/') |
| 101 | + client = ApifyClient.with_custom_http_client( |
| 102 | + api_url=api_url, |
| 103 | + http_client=http_client_class(), |
| 104 | + ) |
| 105 | + |
| 106 | + with client.dataset(DATASET_ID).stream_items(item_format='json') as response: |
| 107 | + assert isinstance(response, HttpResponse) |
| 108 | + # `is_stream_consumed` is transport state, not part of the protocol, but both built-in clients expose it. |
| 109 | + raw: Any = response |
| 110 | + assert raw.is_stream_consumed is False |
| 111 | + |
| 112 | + |
| 113 | +async def test_protocol_check_leaves_stream_unread_async( |
| 114 | + httpserver: HTTPServer, |
| 115 | + http_client_async_class: type[HttpClientAsync], |
| 116 | +) -> None: |
| 117 | + """Checking a streaming response against the protocol inspects it without pulling the body off the wire.""" |
| 118 | + httpserver.expect_request(f'/v2/datasets/{DATASET_ID}/items').respond_with_data(STREAM_CONTENT) |
| 119 | + api_url = httpserver.url_for('/').removesuffix('/') |
| 120 | + client = ApifyClientAsync.with_custom_http_client( |
| 121 | + api_url=api_url, |
| 122 | + http_client=http_client_async_class(), |
| 123 | + ) |
| 124 | + |
| 125 | + async with client.dataset(DATASET_ID).stream_items(item_format='json') as response: |
| 126 | + assert isinstance(response, HttpResponse) |
| 127 | + # `is_stream_consumed` is transport state, not part of the protocol, but both built-in clients expose it. |
| 128 | + raw: Any = response |
| 129 | + assert raw.is_stream_consumed is False |
0 commit comments