Skip to content
Myr

test_decoders.py

This file

Path

tests/test_decoders.py

358 lines · 9.9 KiB · Python

Last changed by

Support for `chunk_size` (#1277)Tom Christiebefore adoption27df5e4

History of this file

Take it

1import zlib23import brotli4import pytest56import httpx7from httpx._decoders import (8    BrotliDecoder,9    ByteChunker,10    DeflateDecoder,11    GZipDecoder,12    IdentityDecoder,13    LineDecoder,14    TextChunker,15    TextDecoder,16)171819def test_deflate():20    """21    Deflate encoding may use either 'zlib' or 'deflate' in the wild.2223    https://stackoverflow.com/questions/1838699/how-can-i-decompress-a-gzip-stream-with-zlib#answer-2231129724    """25    body = b"test 123"26    compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)27    compressed_body = compressor.compress(body) + compressor.flush()2829    headers = [(b"Content-Encoding", b"deflate")]30    response = httpx.Response(31        200,32        headers=headers,33        content=compressed_body,34    )35    assert response.content == body363738def test_zlib():39    """40    Deflate encoding may use either 'zlib' or 'deflate' in the wild.4142    https://stackoverflow.com/questions/1838699/how-can-i-decompress-a-gzip-stream-with-zlib#answer-2231129743    """44    body = b"test 123"45    compressed_body = zlib.compress(body)4647    headers = [(b"Content-Encoding", b"deflate")]48    response = httpx.Response(49        200,50        headers=headers,51        content=compressed_body,52    )53    assert response.content == body545556def test_gzip():57    body = b"test 123"58    compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)59    compressed_body = compressor.compress(body) + compressor.flush()6061    headers = [(b"Content-Encoding", b"gzip")]62    response = httpx.Response(63        200,64        headers=headers,65        content=compressed_body,66    )67    assert response.content == body686970def test_brotli():71    body = b"test 123"72    compressed_body = brotli.compress(body)7374    headers = [(b"Content-Encoding", b"br")]75    response = httpx.Response(76        200,77        headers=headers,78        content=compressed_body,79    )80    assert response.content == body818283def test_multi():84    body = b"test 123"8586    deflate_compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)87    compressed_body = deflate_compressor.compress(body) + deflate_compressor.flush()8889    gzip_compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)90    compressed_body = (91        gzip_compressor.compress(compressed_body) + gzip_compressor.flush()92    )9394    headers = [(b"Content-Encoding", b"deflate, gzip")]95    response = httpx.Response(96        200,97        headers=headers,98        content=compressed_body,99    )100    assert response.content == body101102103def test_multi_with_identity():104    body = b"test 123"105    compressed_body = brotli.compress(body)106107    headers = [(b"Content-Encoding", b"br, identity")]108    response = httpx.Response(109        200,110        headers=headers,111        content=compressed_body,112    )113    assert response.content == body114115    headers = [(b"Content-Encoding", b"identity, br")]116    response = httpx.Response(117        200,118        headers=headers,119        content=compressed_body,120    )121    assert response.content == body122123124@pytest.mark.asyncio125async def test_streaming():126    body = b"test 123"127    compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)128129    async def compress(body):130        yield compressor.compress(body)131        yield compressor.flush()132133    headers = [(b"Content-Encoding", b"gzip")]134    response = httpx.Response(135        200,136        headers=headers,137        content=compress(body),138    )139    assert not hasattr(response, "body")140    assert await response.aread() == body141142143@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br", b"identity"))144def test_empty_content(header_value):145    headers = [(b"Content-Encoding", header_value)]146    response = httpx.Response(147        200,148        headers=headers,149        content=b"",150    )151    assert response.content == b""152153154@pytest.mark.parametrize(155    "decoder", (BrotliDecoder, DeflateDecoder, GZipDecoder, IdentityDecoder)156)157def test_decoders_empty_cases(decoder):158    response = httpx.Response(content=b"", status_code=200)159    instance = decoder()160    assert instance.decode(response.content) == b""161    assert instance.flush() == b""162163164@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))165def test_decoding_errors(header_value):166    headers = [(b"Content-Encoding", header_value)]167    body = b"test 123"168    compressed_body = brotli.compress(body)[3:]169    with pytest.raises(httpx.DecodingError):170        request = httpx.Request("GET", "https://example.org")171        httpx.Response(200, headers=headers, content=compressed_body, request=request)172173    with pytest.raises(ValueError):174        httpx.Response(200, headers=headers, content=compressed_body)175176177@pytest.mark.parametrize(178    ["data", "encoding"],179    [180        ((b"Hello,", b" world!"), "ascii"),181        ((b"\xe3\x83", b"\x88\xe3\x83\xa9", b"\xe3", b"\x83\x99\xe3\x83\xab"), "utf-8"),182        ((b"Euro character: \x88!", b""), "cp1252"),183        ((b"Accented: \xd6sterreich", b""), "iso-8859-1"),184    ],185)186@pytest.mark.asyncio187async def test_text_decoder(data, encoding):188    async def iterator():189        nonlocal data190        for chunk in data:191            yield chunk192193    # Accessing `.text` on a read response.194    response = httpx.Response(195        200,196        content=iterator(),197    )198    await response.aread()199    assert response.text == (b"".join(data)).decode(encoding)200201    # Streaming `.aiter_text` iteratively.202    response = httpx.Response(203        200,204        content=iterator(),205    )206    text = "".join([part async for part in response.aiter_text()])207    assert text == (b"".join(data)).decode(encoding)208209210@pytest.mark.asyncio211async def test_text_decoder_known_encoding():212    async def iterator():213        yield b"\x83g"214        yield b"\x83"215        yield b"\x89\x83x\x83\x8b"216217    response = httpx.Response(218        200,219        headers=[(b"Content-Type", b"text/html; charset=shift-jis")],220        content=iterator(),221    )222223    await response.aread()224    assert "".join(response.text) == "トラベル"225226227def test_text_decoder_empty_cases():228    decoder = TextDecoder()229    assert decoder.flush() == ""230231    decoder = TextDecoder()232    assert decoder.decode(b"") == ""233    assert decoder.flush() == ""234235236def test_line_decoder_nl():237    decoder = LineDecoder()238    assert decoder.decode("") == []239    assert decoder.decode("a\n\nb\nc") == ["a\n", "\n", "b\n"]240    assert decoder.flush() == ["c"]241242    decoder = LineDecoder()243    assert decoder.decode("") == []244    assert decoder.decode("a\n\nb\nc\n") == ["a\n", "\n", "b\n", "c\n"]245    assert decoder.flush() == []246247    # Issue #1033248    decoder = LineDecoder()249    assert decoder.decode("") == []250    assert decoder.decode("12345\n") == ["12345\n"]251    assert decoder.decode("foo ") == []252    assert decoder.decode("bar ") == []253    assert decoder.decode("baz\n") == ["foo bar baz\n"]254    assert decoder.flush() == []255256257def test_line_decoder_cr():258    decoder = LineDecoder()259    assert decoder.decode("") == []260    assert decoder.decode("a\r\rb\rc") == ["a\n", "\n", "b\n"]261    assert decoder.flush() == ["c"]262263    decoder = LineDecoder()264    assert decoder.decode("") == []265    assert decoder.decode("a\r\rb\rc\r") == ["a\n", "\n", "b\n"]266    assert decoder.flush() == ["c\n"]267268    # Issue #1033269    decoder = LineDecoder()270    assert decoder.decode("") == []271    assert decoder.decode("12345\r") == []272    assert decoder.decode("foo ") == ["12345\n"]273    assert decoder.decode("bar ") == []274    assert decoder.decode("baz\r") == []275    assert decoder.flush() == ["foo bar baz\n"]276277278def test_line_decoder_crnl():279    decoder = LineDecoder()280    assert decoder.decode("") == []281    assert decoder.decode("a\r\n\r\nb\r\nc") == ["a\n", "\n", "b\n"]282    assert decoder.flush() == ["c"]283284    decoder = LineDecoder()285    assert decoder.decode("") == []286    assert decoder.decode("a\r\n\r\nb\r\nc\r\n") == ["a\n", "\n", "b\n", "c\n"]287    assert decoder.flush() == []288289    decoder = LineDecoder()290    assert decoder.decode("") == []291    assert decoder.decode("a\r") == []292    assert decoder.decode("\n\r\nb\r\nc") == ["a\n", "\n", "b\n"]293    assert decoder.flush() == ["c"]294295    # Issue #1033296    decoder = LineDecoder()297    assert decoder.decode("") == []298    assert decoder.decode("12345\r\n") == ["12345\n"]299    assert decoder.decode("foo ") == []300    assert decoder.decode("bar ") == []301    assert decoder.decode("baz\r\n") == ["foo bar baz\n"]302    assert decoder.flush() == []303304305def test_byte_chunker():306    decoder = ByteChunker()307    assert decoder.decode(b"1234567") == [b"1234567"]308    assert decoder.decode(b"89") == [b"89"]309    assert decoder.flush() == []310311    decoder = ByteChunker(chunk_size=3)312    assert decoder.decode(b"1234567") == [b"123", b"456"]313    assert decoder.decode(b"89") == [b"789"]314    assert decoder.flush() == []315316    decoder = ByteChunker(chunk_size=3)317    assert decoder.decode(b"123456") == [b"123", b"456"]318    assert decoder.decode(b"789") == [b"789"]319    assert decoder.flush() == []320321    decoder = ByteChunker(chunk_size=3)322    assert decoder.decode(b"123456") == [b"123", b"456"]323    assert decoder.decode(b"78") == []324    assert decoder.flush() == [b"78"]325326327def test_text_chunker():328    decoder = TextChunker()329    assert decoder.decode("1234567") == ["1234567"]330    assert decoder.decode("89") == ["89"]331    assert decoder.flush() == []332333    decoder = TextChunker(chunk_size=3)334    assert decoder.decode("1234567") == ["123", "456"]335    assert decoder.decode("89") == ["789"]336    assert decoder.flush() == []337338    decoder = TextChunker(chunk_size=3)339    assert decoder.decode("123456") == ["123", "456"]340    assert decoder.decode("789") == ["789"]341    assert decoder.flush() == []342343    decoder = TextChunker(chunk_size=3)344    assert decoder.decode("123456") == ["123", "456"]345    assert decoder.decode("78") == []346    assert decoder.flush() == ["78"]347348349def test_invalid_content_encoding_header():350    headers = [(b"Content-Encoding", b"invalid-header")]351    body = b"test 123"352353    response = httpx.Response(354        200,355        headers=headers,356        content=body,357    )358    assert response.content == body