Skip to content
Myr

test_decoders.py

This file

Path

tests/test_decoders.py

312 lines · 8.5 KiB · Python

Last changed by

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

History of this file

Take it

1import zlib23import brotli4import pytest56import httpx7from httpx._decoders import (8    BrotliDecoder,9    DeflateDecoder,10    GZipDecoder,11    IdentityDecoder,12    LineDecoder,13    TextDecoder,14)151617def test_deflate():18    """19    Deflate encoding may use either 'zlib' or 'deflate' in the wild.2021    https://stackoverflow.com/questions/1838699/how-can-i-decompress-a-gzip-stream-with-zlib#answer-2231129722    """23    body = b"test 123"24    compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)25    compressed_body = compressor.compress(body) + compressor.flush()2627    headers = [(b"Content-Encoding", b"deflate")]28    response = httpx.Response(29        200,30        headers=headers,31        content=compressed_body,32    )33    assert response.content == body343536def test_zlib():37    """38    Deflate encoding may use either 'zlib' or 'deflate' in the wild.3940    https://stackoverflow.com/questions/1838699/how-can-i-decompress-a-gzip-stream-with-zlib#answer-2231129741    """42    body = b"test 123"43    compressed_body = zlib.compress(body)4445    headers = [(b"Content-Encoding", b"deflate")]46    response = httpx.Response(47        200,48        headers=headers,49        content=compressed_body,50    )51    assert response.content == body525354def test_gzip():55    body = b"test 123"56    compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)57    compressed_body = compressor.compress(body) + compressor.flush()5859    headers = [(b"Content-Encoding", b"gzip")]60    response = httpx.Response(61        200,62        headers=headers,63        content=compressed_body,64    )65    assert response.content == body666768def test_brotli():69    body = b"test 123"70    compressed_body = brotli.compress(body)7172    headers = [(b"Content-Encoding", b"br")]73    response = httpx.Response(74        200,75        headers=headers,76        content=compressed_body,77    )78    assert response.content == body798081def test_multi():82    body = b"test 123"8384    deflate_compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)85    compressed_body = deflate_compressor.compress(body) + deflate_compressor.flush()8687    gzip_compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)88    compressed_body = (89        gzip_compressor.compress(compressed_body) + gzip_compressor.flush()90    )9192    headers = [(b"Content-Encoding", b"deflate, gzip")]93    response = httpx.Response(94        200,95        headers=headers,96        content=compressed_body,97    )98    assert response.content == body99100101def test_multi_with_identity():102    body = b"test 123"103    compressed_body = brotli.compress(body)104105    headers = [(b"Content-Encoding", b"br, identity")]106    response = httpx.Response(107        200,108        headers=headers,109        content=compressed_body,110    )111    assert response.content == body112113    headers = [(b"Content-Encoding", b"identity, br")]114    response = httpx.Response(115        200,116        headers=headers,117        content=compressed_body,118    )119    assert response.content == body120121122@pytest.mark.asyncio123async def test_streaming():124    body = b"test 123"125    compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)126127    async def compress(body):128        yield compressor.compress(body)129        yield compressor.flush()130131    headers = [(b"Content-Encoding", b"gzip")]132    response = httpx.Response(133        200,134        headers=headers,135        content=compress(body),136    )137    assert not hasattr(response, "body")138    assert await response.aread() == body139140141@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br", b"identity"))142def test_empty_content(header_value):143    headers = [(b"Content-Encoding", header_value)]144    response = httpx.Response(145        200,146        headers=headers,147        content=b"",148    )149    assert response.content == b""150151152@pytest.mark.parametrize(153    "decoder", (BrotliDecoder, DeflateDecoder, GZipDecoder, IdentityDecoder)154)155def test_decoders_empty_cases(decoder):156    response = httpx.Response(content=b"", status_code=200)157    instance = decoder()158    assert instance.decode(response.content) == b""159    assert instance.flush() == b""160161162@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))163def test_decoding_errors(header_value):164    headers = [(b"Content-Encoding", header_value)]165    body = b"test 123"166    compressed_body = brotli.compress(body)[3:]167    with pytest.raises(httpx.DecodingError):168        request = httpx.Request("GET", "https://example.org")169        httpx.Response(200, headers=headers, content=compressed_body, request=request)170171    with pytest.raises(ValueError):172        httpx.Response(200, headers=headers, content=compressed_body)173174175@pytest.mark.parametrize(176    ["data", "encoding"],177    [178        ((b"Hello,", b" world!"), "ascii"),179        ((b"\xe3\x83", b"\x88\xe3\x83\xa9", b"\xe3", b"\x83\x99\xe3\x83\xab"), "utf-8"),180        ((b"Euro character: \x88!", b""), "cp1252"),181        ((b"Accented: \xd6sterreich", b""), "iso-8859-1"),182    ],183)184@pytest.mark.asyncio185async def test_text_decoder(data, encoding):186    async def iterator():187        nonlocal data188        for chunk in data:189            yield chunk190191    # Accessing `.text` on a read response.192    response = httpx.Response(193        200,194        content=iterator(),195    )196    await response.aread()197    assert response.text == (b"".join(data)).decode(encoding)198199    # Streaming `.aiter_text` iteratively.200    response = httpx.Response(201        200,202        content=iterator(),203    )204    text = "".join([part async for part in response.aiter_text()])205    assert text == (b"".join(data)).decode(encoding)206207208@pytest.mark.asyncio209async def test_text_decoder_known_encoding():210    async def iterator():211        yield b"\x83g"212        yield b"\x83"213        yield b"\x89\x83x\x83\x8b"214215    response = httpx.Response(216        200,217        headers=[(b"Content-Type", b"text/html; charset=shift-jis")],218        content=iterator(),219    )220221    await response.aread()222    assert "".join(response.text) == "トラベル"223224225def test_text_decoder_empty_cases():226    decoder = TextDecoder()227    assert decoder.flush() == ""228229    decoder = TextDecoder()230    assert decoder.decode(b"") == ""231    assert decoder.flush() == ""232233234def test_line_decoder_nl():235    decoder = LineDecoder()236    assert decoder.decode("") == []237    assert decoder.decode("a\n\nb\nc") == ["a\n", "\n", "b\n"]238    assert decoder.flush() == ["c"]239240    decoder = LineDecoder()241    assert decoder.decode("") == []242    assert decoder.decode("a\n\nb\nc\n") == ["a\n", "\n", "b\n", "c\n"]243    assert decoder.flush() == []244245    # Issue #1033246    decoder = LineDecoder()247    assert decoder.decode("") == []248    assert decoder.decode("12345\n") == ["12345\n"]249    assert decoder.decode("foo ") == []250    assert decoder.decode("bar ") == []251    assert decoder.decode("baz\n") == ["foo bar baz\n"]252    assert decoder.flush() == []253254255def test_line_decoder_cr():256    decoder = LineDecoder()257    assert decoder.decode("") == []258    assert decoder.decode("a\r\rb\rc") == ["a\n", "\n", "b\n"]259    assert decoder.flush() == ["c"]260261    decoder = LineDecoder()262    assert decoder.decode("") == []263    assert decoder.decode("a\r\rb\rc\r") == ["a\n", "\n", "b\n"]264    assert decoder.flush() == ["c\n"]265266    # Issue #1033267    decoder = LineDecoder()268    assert decoder.decode("") == []269    assert decoder.decode("12345\r") == []270    assert decoder.decode("foo ") == ["12345\n"]271    assert decoder.decode("bar ") == []272    assert decoder.decode("baz\r") == []273    assert decoder.flush() == ["foo bar baz\n"]274275276def test_line_decoder_crnl():277    decoder = LineDecoder()278    assert decoder.decode("") == []279    assert decoder.decode("a\r\n\r\nb\r\nc") == ["a\n", "\n", "b\n"]280    assert decoder.flush() == ["c"]281282    decoder = LineDecoder()283    assert decoder.decode("") == []284    assert decoder.decode("a\r\n\r\nb\r\nc\r\n") == ["a\n", "\n", "b\n", "c\n"]285    assert decoder.flush() == []286287    decoder = LineDecoder()288    assert decoder.decode("") == []289    assert decoder.decode("a\r") == []290    assert decoder.decode("\n\r\nb\r\nc") == ["a\n", "\n", "b\n"]291    assert decoder.flush() == ["c"]292293    # Issue #1033294    decoder = LineDecoder()295    assert decoder.decode("") == []296    assert decoder.decode("12345\r\n") == ["12345\n"]297    assert decoder.decode("foo ") == []298    assert decoder.decode("bar ") == []299    assert decoder.decode("baz\r\n") == ["foo bar baz\n"]300    assert decoder.flush() == []301302303def test_invalid_content_encoding_header():304    headers = [(b"Content-Encoding", b"invalid-header")]305    body = b"test 123"306307    response = httpx.Response(308        200,309        headers=headers,310        content=body,311    )312    assert response.content == body