|
2 | 2 |
|
3 | 3 | import asyncio |
4 | 4 | import gzip |
| 5 | +import io |
5 | 6 | import threading |
6 | 7 | import time |
7 | 8 | from datetime import UTC, datetime, timedelta |
8 | | -from typing import TYPE_CHECKING |
| 9 | +from typing import TYPE_CHECKING, cast |
9 | 10 | from unittest.mock import AsyncMock, Mock |
10 | 11 |
|
11 | 12 | import brotli |
@@ -461,6 +462,83 @@ def test_prepare_request_call_with_bytearray_data(compressor_case: tuple) -> Non |
461 | 462 | assert decompress(data) == b'test bytearray' |
462 | 463 |
|
463 | 464 |
|
| 465 | +@pytest.mark.parametrize( |
| 466 | + 'content_type', |
| 467 | + [ |
| 468 | + pytest.param('image/png', id='image'), |
| 469 | + pytest.param('video/mp4', id='video'), |
| 470 | + pytest.param('application/zip', id='archive'), |
| 471 | + ], |
| 472 | +) |
| 473 | +def test_prepare_request_call_skips_compression_for_already_compressed_content(content_type: str) -> None: |
| 474 | + """An already-compressed body is sent verbatim, carries no `Content-Encoding`, and keeps every other header.""" |
| 475 | + client = _ConcreteHttpClient(token='test_token', http_compressor=GzipHttpCompressor()) |
| 476 | + |
| 477 | + headers, _params, data = client._prepare_request_call( |
| 478 | + headers={'content-type': content_type}, |
| 479 | + data=b'\x89PNG binary', |
| 480 | + ) |
| 481 | + |
| 482 | + assert data == b'\x89PNG binary' |
| 483 | + assert not any(key.lower() == 'content-encoding' for key in headers) |
| 484 | + assert headers['Authorization'] == 'Bearer test_token' |
| 485 | + assert headers['content-type'] == content_type |
| 486 | + assert headers['User-Agent'] == client._headers['User-Agent'] |
| 487 | + |
| 488 | + |
| 489 | +def test_prepare_request_call_drops_caller_content_encoding_when_compression_is_skipped() -> None: |
| 490 | + """Skipping compression also strips a caller-supplied `Content-Encoding`, which would misdescribe the body.""" |
| 491 | + client = _ConcreteHttpClient(http_compressor=GzipHttpCompressor()) |
| 492 | + |
| 493 | + headers, _params, data = client._prepare_request_call( |
| 494 | + headers={'content-type': 'image/jpeg', 'content-encoding': 'br'}, |
| 495 | + data=b'jpeg binary', |
| 496 | + ) |
| 497 | + |
| 498 | + assert data == b'jpeg binary' |
| 499 | + assert not any(key.lower() == 'content-encoding' for key in headers) |
| 500 | + |
| 501 | + |
| 502 | +def test_prepare_request_call_drops_caller_content_encoding_for_a_streamed_body() -> None: |
| 503 | + """A body that is streamed rather than compressed, such as a file-like object, also loses `Content-Encoding`.""" |
| 504 | + client = _ConcreteHttpClient(http_compressor=GzipHttpCompressor()) |
| 505 | + stream = io.BytesIO(b'raw payload') |
| 506 | + |
| 507 | + headers, _params, data = client._prepare_request_call( |
| 508 | + headers={'content-encoding': 'gzip'}, |
| 509 | + data=cast('bytes', stream), |
| 510 | + ) |
| 511 | + |
| 512 | + assert data is stream |
| 513 | + assert not any(key.lower() == 'content-encoding' for key in headers) |
| 514 | + |
| 515 | + |
| 516 | +@pytest.mark.parametrize( |
| 517 | + 'content_type', |
| 518 | + [ |
| 519 | + pytest.param('image/svg+xml', id='structured xml suffix'), |
| 520 | + pytest.param('image/bmp', id='raw bitmap'), |
| 521 | + pytest.param('audio/wav', id='raw audio'), |
| 522 | + ], |
| 523 | +) |
| 524 | +def test_prepare_request_call_compresses_exceptions_to_compressed_prefixes( |
| 525 | + content_type: str, |
| 526 | + compressor_case: tuple, |
| 527 | +) -> None: |
| 528 | + """Types that are text or raw are compressed even when they sit under an already-compressed prefix.""" |
| 529 | + compressor, content_encoding, decompress = compressor_case |
| 530 | + client = _ConcreteHttpClient(http_compressor=compressor) |
| 531 | + |
| 532 | + headers, _params, data = client._prepare_request_call( |
| 533 | + headers={'content-type': content_type}, |
| 534 | + data=b'raw payload', |
| 535 | + ) |
| 536 | + |
| 537 | + assert headers['Content-Encoding'] == content_encoding |
| 538 | + assert isinstance(data, bytes) |
| 539 | + assert decompress(data) == b'raw payload' |
| 540 | + |
| 541 | + |
464 | 542 | def test_prepare_request_call_json_and_data_error() -> None: |
465 | 543 | """Test _prepare_request_call raises error when both json and data are provided.""" |
466 | 544 | client = _ConcreteHttpClient() |
@@ -520,7 +598,7 @@ def test_prepare_request_call_json_keeps_caller_content_type() -> None: |
520 | 598 |
|
521 | 599 |
|
522 | 600 | def test_prepare_request_call_replaces_caller_content_encoding() -> None: |
523 | | - """The Content-Encoding header always reflects the compressor actually applied, replacing any caller value.""" |
| 601 | + """A compressed body reports the compressor actually applied, replacing any caller-supplied Content-Encoding.""" |
524 | 602 | client = _ConcreteHttpClient(http_compressor=GzipHttpCompressor()) |
525 | 603 |
|
526 | 604 | headers, _params, _data = client._prepare_request_call(headers={'content-encoding': 'br'}, data='payload') |
|
0 commit comments