|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import gzip |
| 4 | +import json |
3 | 5 | import re |
4 | | -from typing import TYPE_CHECKING |
| 6 | +from typing import TYPE_CHECKING, Any |
5 | 7 |
|
| 8 | +import brotli |
6 | 9 | import pytest |
| 10 | +from werkzeug import Request, Response |
7 | 11 |
|
8 | 12 | from apify_client import ApifyClient, ApifyClientAsync |
9 | 13 | from apify_client.errors import ApifyApiError |
10 | 14 |
|
11 | 15 | if TYPE_CHECKING: |
| 16 | + from collections.abc import Callable |
| 17 | + |
12 | 18 | from pytest_httpserver import HTTPServer |
13 | 19 |
|
14 | 20 | from apify_client._typeddicts import RequestDraftDict |
@@ -117,3 +123,110 @@ def test_batch_processed_partially_sync(httpserver: HTTPServer) -> None: |
117 | 123 | assert requests[0]['unique_key'] in {request.unique_key for request in batch_response.processed_requests} |
118 | 124 | assert len(batch_response.unprocessed_requests) == 1 |
119 | 125 | assert batch_response.unprocessed_requests[0].unique_key == requests[1]['unique_key'] |
| 126 | + |
| 127 | + |
| 128 | +_ADD_REQUEST_RESPONSE_CONTENT = """{ |
| 129 | + "data": { |
| 130 | + "requestId": "YiKoxjkaS9gjGTqhF", |
| 131 | + "wasAlreadyPresent": false, |
| 132 | + "wasAlreadyHandled": false |
| 133 | + } |
| 134 | +}""" |
| 135 | + |
| 136 | +_FULLY_ADDED_BATCH_RESPONSE_CONTENT = """{ |
| 137 | + "data": { |
| 138 | + "processedRequests": [ |
| 139 | + { |
| 140 | + "requestId": "YiKoxjkaS9gjGTqhF", |
| 141 | + "uniqueKey": "http://example.com/1", |
| 142 | + "wasAlreadyPresent": false, |
| 143 | + "wasAlreadyHandled": false |
| 144 | + } |
| 145 | + ], |
| 146 | + "unprocessedRequests": [] |
| 147 | + } |
| 148 | +}""" |
| 149 | + |
| 150 | +_SNAKE_CASE_REQUEST: dict[str, Any] = { |
| 151 | + 'unique_key': 'http://example.com/1', |
| 152 | + 'url': 'http://example.com/1', |
| 153 | + 'user_data': {'label': 'DETAIL'}, |
| 154 | + 'no_retry': True, |
| 155 | +} |
| 156 | + |
| 157 | +_EXPECTED_CAMEL_CASE_REQUEST = { |
| 158 | + 'uniqueKey': 'http://example.com/1', |
| 159 | + 'url': 'http://example.com/1', |
| 160 | + 'userData': {'label': 'DETAIL'}, |
| 161 | + 'noRetry': True, |
| 162 | +} |
| 163 | + |
| 164 | + |
| 165 | +def _make_json_capture_handler(received_bodies: list[Any], response_content: str) -> Callable[[Request], Response]: |
| 166 | + def handler(request: Request) -> Response: |
| 167 | + body = request.get_data() |
| 168 | + encoding = request.headers.get('Content-Encoding') |
| 169 | + if encoding == 'br': |
| 170 | + body = brotli.decompress(body) |
| 171 | + elif encoding == 'gzip': |
| 172 | + body = gzip.decompress(body) |
| 173 | + received_bodies.append(json.loads(body)) |
| 174 | + return Response(status=201, response=response_content, content_type='application/json') |
| 175 | + |
| 176 | + return handler |
| 177 | + |
| 178 | + |
| 179 | +def test_add_request_camel_cases_fields_undeclared_on_model_sync(httpserver: HTTPServer) -> None: |
| 180 | + """Snake_case fields not declared on `RequestDraft` (e.g. `user_data`) are camelCased in the API payload.""" |
| 181 | + server_url = httpserver.url_for('/').removesuffix('/') |
| 182 | + client = ApifyClient(token='placeholder_token', api_url=server_url, api_public_url=server_url) |
| 183 | + |
| 184 | + received_bodies: list[Any] = [] |
| 185 | + httpserver.expect_oneshot_request(re.compile(r'.*'), method='POST').respond_with_handler( |
| 186 | + _make_json_capture_handler(received_bodies, _ADD_REQUEST_RESPONSE_CONTENT) |
| 187 | + ) |
| 188 | + |
| 189 | + client.request_queue(request_queue_id='whatever').add_request(_SNAKE_CASE_REQUEST) # ty: ignore[invalid-argument-type] |
| 190 | + assert received_bodies == [_EXPECTED_CAMEL_CASE_REQUEST] |
| 191 | + |
| 192 | + |
| 193 | +async def test_add_request_camel_cases_fields_undeclared_on_model_async(httpserver: HTTPServer) -> None: |
| 194 | + """Snake_case fields not declared on `RequestDraft` (e.g. `user_data`) are camelCased in the API payload.""" |
| 195 | + server_url = httpserver.url_for('/').removesuffix('/') |
| 196 | + client = ApifyClientAsync(token='placeholder_token', api_url=server_url, api_public_url=server_url) |
| 197 | + |
| 198 | + received_bodies: list[Any] = [] |
| 199 | + httpserver.expect_oneshot_request(re.compile(r'.*'), method='POST').respond_with_handler( |
| 200 | + _make_json_capture_handler(received_bodies, _ADD_REQUEST_RESPONSE_CONTENT) |
| 201 | + ) |
| 202 | + |
| 203 | + await client.request_queue(request_queue_id='whatever').add_request(_SNAKE_CASE_REQUEST) # ty: ignore[invalid-argument-type] |
| 204 | + assert received_bodies == [_EXPECTED_CAMEL_CASE_REQUEST] |
| 205 | + |
| 206 | + |
| 207 | +def test_batch_add_requests_camel_cases_fields_undeclared_on_model_sync(httpserver: HTTPServer) -> None: |
| 208 | + """Snake_case fields not declared on `RequestDraft` (e.g. `user_data`) are camelCased in the API payload.""" |
| 209 | + server_url = httpserver.url_for('/').removesuffix('/') |
| 210 | + client = ApifyClient(token='placeholder_token', api_url=server_url, api_public_url=server_url) |
| 211 | + |
| 212 | + received_bodies: list[Any] = [] |
| 213 | + httpserver.expect_oneshot_request(re.compile(r'.*'), method='POST').respond_with_handler( |
| 214 | + _make_json_capture_handler(received_bodies, _FULLY_ADDED_BATCH_RESPONSE_CONTENT) |
| 215 | + ) |
| 216 | + |
| 217 | + client.request_queue(request_queue_id='whatever').batch_add_requests(requests=[_SNAKE_CASE_REQUEST]) # ty: ignore[invalid-argument-type] |
| 218 | + assert received_bodies == [[_EXPECTED_CAMEL_CASE_REQUEST]] |
| 219 | + |
| 220 | + |
| 221 | +async def test_batch_add_requests_camel_cases_fields_undeclared_on_model_async(httpserver: HTTPServer) -> None: |
| 222 | + """Snake_case fields not declared on `RequestDraft` (e.g. `user_data`) are camelCased in the API payload.""" |
| 223 | + server_url = httpserver.url_for('/').removesuffix('/') |
| 224 | + client = ApifyClientAsync(token='placeholder_token', api_url=server_url, api_public_url=server_url) |
| 225 | + |
| 226 | + received_bodies: list[Any] = [] |
| 227 | + httpserver.expect_oneshot_request(re.compile(r'.*'), method='POST').respond_with_handler( |
| 228 | + _make_json_capture_handler(received_bodies, _FULLY_ADDED_BATCH_RESPONSE_CONTENT) |
| 229 | + ) |
| 230 | + |
| 231 | + await client.request_queue(request_queue_id='whatever').batch_add_requests(requests=[_SNAKE_CASE_REQUEST]) # ty: ignore[invalid-argument-type] |
| 232 | + assert received_bodies == [[_EXPECTED_CAMEL_CASE_REQUEST]] |
0 commit comments