|
9 | 9 | from werkzeug import Request, Response |
10 | 10 |
|
11 | 11 | from apify_client import ApifyClient, ApifyClientAsync |
| 12 | +from apify_client._consts import MIN_COMPRESSION_SIZE |
12 | 13 |
|
13 | 14 | if TYPE_CHECKING: |
14 | 15 | from collections.abc import Callable |
|
20 | 21 | _MOCKED_KVS_ID = 'test_kvs_id' |
21 | 22 | _RECORD_PATH = f'/v2/key-value-stores/{_MOCKED_KVS_ID}/records/f' |
22 | 23 |
|
| 24 | +# The client compresses only bodies of at least `MIN_COMPRESSION_SIZE` bytes, so the value is padded past that |
| 25 | +# to keep the compression axis meaningful. A shorter one would go out uncompressed under either algorithm. |
| 26 | +_TEXT_VALUE = 'buffer data' + '.' * MIN_COMPRESSION_SIZE |
| 27 | +_BYTES_VALUE = _TEXT_VALUE.encode('utf-8') |
| 28 | + |
23 | 29 |
|
24 | 30 | class DuckTypedReader: |
25 | 31 | """A file-like object that is not an `io.IOBase`, so only duck-typed detection picks it up.""" |
26 | 32 |
|
27 | 33 | def read(self) -> bytes: |
28 | | - return b'buffer data' |
| 34 | + return _BYTES_VALUE |
29 | 35 |
|
30 | 36 |
|
31 | 37 | # The values are built by a factory because reading consumes them, and each case runs once per compression |
32 | 38 | # algorithm. Each case is (value factory, expected uploaded body, expected content type). |
33 | 39 | _FILE_LIKE_VALUE_CASES = [ |
34 | | - pytest.param(lambda: io.BytesIO(b'buffer data'), b'buffer data', 'application/octet-stream', id='bytes io'), |
35 | | - pytest.param(lambda: io.StringIO('buffer data'), b'buffer data', 'text/plain; charset=utf-8', id='string io'), |
36 | | - pytest.param(DuckTypedReader, b'buffer data', 'application/octet-stream', id='duck-typed reader'), |
| 40 | + pytest.param(lambda: io.BytesIO(_BYTES_VALUE), _BYTES_VALUE, 'application/octet-stream', id='bytes io'), |
| 41 | + pytest.param(lambda: io.StringIO(_TEXT_VALUE), _BYTES_VALUE, 'text/plain; charset=utf-8', id='string io'), |
| 42 | + pytest.param(DuckTypedReader, _BYTES_VALUE, 'application/octet-stream', id='duck-typed reader'), |
37 | 43 | ] |
38 | 44 |
|
39 | 45 |
|
|
0 commit comments