|
2 | 2 |
|
3 | 3 | from __future__ import annotations |
4 | 4 |
|
| 5 | +import gzip |
5 | 6 | import json |
6 | 7 | from collections.abc import AsyncIterator, Iterator |
7 | 8 | from datetime import timedelta |
@@ -601,6 +602,42 @@ async def get_record() -> dict | None: |
601 | 602 | await maybe_await(store_client.delete()) |
602 | 603 |
|
603 | 604 |
|
| 605 | +async def test_key_value_store_set_and_get_pre_compressed_record(client: ApifyClient | ApifyClientAsync) -> None: |
| 606 | + """A record uploaded with an explicit `content_encoding` is stored as sent and served back under that encoding.""" |
| 607 | + store_name = get_random_resource_name('kvs') |
| 608 | + created_store = await maybe_await(client.key_value_stores().get_or_create(name=store_name)) |
| 609 | + assert isinstance(created_store, KeyValueStore) |
| 610 | + store_client = client.key_value_store(created_store.id) |
| 611 | + |
| 612 | + try: |
| 613 | + # Past the size threshold and of a compressible content type, so without the explicit encoding the client |
| 614 | + # would compress the body itself - a second time, on top of the gzip the caller already applied. |
| 615 | + payload = b'compressible payload ' * 500 |
| 616 | + await maybe_await( |
| 617 | + store_client.set_record( |
| 618 | + 'pre-compressed', |
| 619 | + gzip.compress(payload), |
| 620 | + content_type='text/plain', |
| 621 | + content_encoding='gzip', |
| 622 | + ) |
| 623 | + ) |
| 624 | + |
| 625 | + # Poll until the record is visible (eventual consistency) |
| 626 | + async def get_record() -> dict | None: |
| 627 | + return await maybe_await(store_client.get_record_as_bytes('pre-compressed')) |
| 628 | + |
| 629 | + record = await poll_until_condition(get_record, lambda record: record is not None) |
| 630 | + assert isinstance(record, dict) |
| 631 | + |
| 632 | + # The record is served with the `Content-Encoding` it was stored under, so the transport decodes it back |
| 633 | + # to the original payload. A body compressed twice would arrive here as the gzip bytes instead, and one |
| 634 | + # stored without the encoding would not be decoded at all. |
| 635 | + assert record['value'] == payload |
| 636 | + assert 'text/plain' in record['content_type'] |
| 637 | + finally: |
| 638 | + await maybe_await(store_client.delete()) |
| 639 | + |
| 640 | + |
604 | 641 | async def test_key_value_store_get_record_public_url(client: ApifyClient | ApifyClientAsync) -> None: |
605 | 642 | """Test get_record_public_url returns a working signed URL.""" |
606 | 643 | store_name = get_random_resource_name('kvs') |
|
0 commit comments