|
11 | 11 | from base64 import b64decode |
12 | 12 | from http.client import IncompleteRead as httplib_IncompleteRead |
13 | 13 | from io import BufferedReader, BytesIO, TextIOWrapper |
14 | | -from test import onlyBrotli, onlyZstd |
| 14 | +from test import LONG_TIMEOUT, onlyBrotli, onlyZstd |
15 | 15 | from unittest import mock |
16 | 16 |
|
17 | 17 | import pytest |
@@ -813,6 +813,42 @@ def test_read_chunked_with_all_data_already_in_decompressor( |
813 | 813 | assert len(result[-1]) == len(original_data) % 10240 |
814 | 814 | assert b"".join(result) == original_data |
815 | 815 |
|
| 816 | + @pytest.mark.timeout(LONG_TIMEOUT) |
| 817 | + @pytest.mark.parametrize( |
| 818 | + "data", |
| 819 | + [d[1] for d in _test_compressor_params], |
| 820 | + ids=[d[0] for d in _test_compressor_params], |
| 821 | + ) |
| 822 | + def test_read_chunked_with_trailing_data_does_not_hang( |
| 823 | + self, |
| 824 | + request: pytest.FixtureRequest, |
| 825 | + data: tuple[str, typing.Callable[[bytes], bytes]] | None, |
| 826 | + ) -> None: |
| 827 | + if data is None: |
| 828 | + pytest.skip(f"Proper {request.node.callspec.id} decoder is not available") |
| 829 | + # The decoded body must fill multiple bounded reads so EOF is reached |
| 830 | + # while trailing data remains in the decoder's input buffer. |
| 831 | + original_data = b"A" * 100 |
| 832 | + content_encoding, compress_func = data |
| 833 | + compressed_data = compress_func(original_data) + b"tail" |
| 834 | + httplib_r = httplib.HTTPResponse(MockSock) # type: ignore[arg-type] |
| 835 | + httplib_r.fp = MockChunkedEncodingResponse([compressed_data]) # type: ignore[assignment] |
| 836 | + r = HTTPResponse( |
| 837 | + httplib_r, |
| 838 | + preload_content=False, |
| 839 | + headers={ |
| 840 | + "transfer-encoding": "chunked", |
| 841 | + "content-encoding": content_encoding, |
| 842 | + }, |
| 843 | + ) |
| 844 | + |
| 845 | + stream = r.stream(len(original_data) // 2, decode_content=True) |
| 846 | + if content_encoding in ("br", "zstd"): |
| 847 | + with pytest.raises(DecodeError): |
| 848 | + list(stream) |
| 849 | + else: |
| 850 | + assert b"".join(stream) == original_data |
| 851 | + |
816 | 852 | @pytest.mark.parametrize( |
817 | 853 | "delta", |
818 | 854 | ( |
|
0 commit comments