Skip to content
Myr

test_decoders.py

This file

Path

tests/test_decoders.py

218 lines · 6.4 KiB · Python

Last changed by

Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b

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    body = b"test 123"19    compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)20    compressed_body = compressor.compress(body) + compressor.flush()2122    headers = [(b"Content-Encoding", b"deflate")]23    response = httpx.Response(200, headers=headers, content=compressed_body)24    assert response.content == body252627def test_gzip():28    body = b"test 123"29    compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)30    compressed_body = compressor.compress(body) + compressor.flush()3132    headers = [(b"Content-Encoding", b"gzip")]33    response = httpx.Response(200, headers=headers, content=compressed_body)34    assert response.content == body353637def test_brotli():38    body = b"test 123"39    compressed_body = brotli.compress(body)4041    headers = [(b"Content-Encoding", b"br")]42    response = httpx.Response(200, headers=headers, content=compressed_body)43    assert response.content == body444546def test_multi():47    body = b"test 123"4849    deflate_compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)50    compressed_body = deflate_compressor.compress(body) + deflate_compressor.flush()5152    gzip_compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)53    compressed_body = (54        gzip_compressor.compress(compressed_body) + gzip_compressor.flush()55    )5657    headers = [(b"Content-Encoding", b"deflate, gzip")]58    response = httpx.Response(200, headers=headers, content=compressed_body)59    assert response.content == body606162def test_multi_with_identity():63    body = b"test 123"64    compressed_body = brotli.compress(body)6566    headers = [(b"Content-Encoding", b"br, identity")]67    response = httpx.Response(200, headers=headers, content=compressed_body)68    assert response.content == body6970    headers = [(b"Content-Encoding", b"identity, br")]71    response = httpx.Response(200, headers=headers, content=compressed_body)72    assert response.content == body737475@pytest.mark.asyncio76async def test_streaming():77    body = b"test 123"78    compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)7980    async def compress(body):81        yield compressor.compress(body)82        yield compressor.flush()8384    headers = [(b"Content-Encoding", b"gzip")]85    response = httpx.Response(200, headers=headers, content=compress(body))86    assert not hasattr(response, "body")87    assert await response.read() == body888990@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br", b"identity"))91def test_empty_content(header_value):92    headers = [(b"Content-Encoding", header_value)]93    response = httpx.Response(200, headers=headers, content=b"")94    assert response.content == b""959697@pytest.mark.parametrize(98    "decoder", (BrotliDecoder, DeflateDecoder, GZipDecoder, IdentityDecoder)99)100def test_decoders_empty_cases(decoder):101    instance = decoder()102    assert instance.decode(b"") == b""103    assert instance.flush() == b""104105106@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))107def test_decoding_errors(header_value):108    headers = [(b"Content-Encoding", header_value)]109    body = b"test 123"110    compressed_body = brotli.compress(body)[3:]111    with pytest.raises(httpx.DecodingError):112        response = httpx.Response(200, headers=headers, content=compressed_body)113        response.content114115116@pytest.mark.parametrize(117    ["data", "encoding"],118    [119        ((b"Hello,", b" world!"), "ascii"),120        ((b"\xe3\x83", b"\x88\xe3\x83\xa9", b"\xe3", b"\x83\x99\xe3\x83\xab"), "utf-8"),121        ((b"\x83g\x83\x89\x83x\x83\x8b",) * 64, "shift-jis"),122        ((b"\x83g\x83\x89\x83x\x83\x8b",) * 600, "shift-jis"),123        (124            (b"\xcb\xee\xf0\xe5\xec \xe8\xef\xf1\xf3\xec \xe4\xee\xeb\xee\xf0",) * 64,125            "MacCyrillic",126        ),127        (128            (b"\xa5\xa6\xa5\xa7\xa5\xd6\xa4\xce\xb9\xf1\xba\xdd\xb2\xbd",) * 512,129            "euc-jp",130        ),131    ],132)133@pytest.mark.asyncio134async def test_text_decoder(data, encoding):135    async def iterator():136        nonlocal data137        for chunk in data:138            yield chunk139140    response = httpx.Response(200, content=iterator())141    await response.read()142    assert response.text == (b"".join(data)).decode(encoding)143144145@pytest.mark.asyncio146async def test_text_decoder_known_encoding():147    async def iterator():148        yield b"\x83g"149        yield b"\x83"150        yield b"\x89\x83x\x83\x8b"151152    response = httpx.Response(153        200,154        headers=[(b"Content-Type", b"text/html; charset=shift-jis")],155        content=iterator(),156    )157158    await response.read()159    assert "".join(response.text) == "トラベル"160161162def test_text_decoder_empty_cases():163    decoder = TextDecoder()164    assert decoder.flush() == ""165166    decoder = TextDecoder()167    assert decoder.decode(b"") == ""168    assert decoder.flush() == ""169170171def test_line_decoder_nl():172    decoder = LineDecoder()173    assert decoder.decode("") == []174    assert decoder.decode("a\n\nb\nc") == ["a\n", "\n", "b\n"]175    assert decoder.flush() == ["c"]176177    decoder = LineDecoder()178    assert decoder.decode("") == []179    assert decoder.decode("a\n\nb\nc\n") == ["a\n", "\n", "b\n", "c\n"]180    assert decoder.flush() == []181182183def test_line_decoder_cr():184    decoder = LineDecoder()185    assert decoder.decode("") == []186    assert decoder.decode("a\r\rb\rc") == ["a\n", "\n", "b\n"]187    assert decoder.flush() == ["c"]188189    decoder = LineDecoder()190    assert decoder.decode("") == []191    assert decoder.decode("a\r\rb\rc\r") == ["a\n", "\n", "b\n"]192    assert decoder.flush() == ["c\n"]193194195def test_line_decoder_crnl():196    decoder = LineDecoder()197    assert decoder.decode("") == []198    assert decoder.decode("a\r\n\r\nb\r\nc") == ["a\n", "\n", "b\n"]199    assert decoder.flush() == ["c"]200201    decoder = LineDecoder()202    assert decoder.decode("") == []203    assert decoder.decode("a\r\n\r\nb\r\nc\r\n") == ["a\n", "\n", "b\n", "c\n"]204    assert decoder.flush() == []205206    decoder = LineDecoder()207    assert decoder.decode("") == []208    assert decoder.decode("a\r") == []209    assert decoder.decode("\n\r\nb\r\nc") == ["a\n", "\n", "b\n"]210    assert decoder.flush() == ["c"]211212213def test_invalid_content_encoding_header():214    headers = [(b"Content-Encoding", b"invalid-header")]215    body = b"test 123"216217    response = httpx.Response(200, headers=headers, content=body)218    assert response.content == body