Skip to content
Myr

test_decoding.py

This file

Path

tests/test_decoding.py

94 lines · 2.9 KiB · Python

Last changed by

Wrap decoding errors with custom exceptionsYeray Diaz Diazbefore adoptionbdc218f

History of this file

Take it

1import zlib23import brotli4import pytest56import httpcore789def test_deflate():10    body = b"test 123"11    compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)12    compressed_body = compressor.compress(body) + compressor.flush()1314    headers = [(b"Content-Encoding", b"deflate")]15    response = httpcore.Response(200, headers=headers, body=compressed_body)16    assert response.body == body171819def test_gzip():20    body = b"test 123"21    compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)22    compressed_body = compressor.compress(body) + compressor.flush()2324    headers = [(b"Content-Encoding", b"gzip")]25    response = httpcore.Response(200, headers=headers, body=compressed_body)26    assert response.body == body272829def test_brotli():30    body = b"test 123"31    compressed_body = brotli.compress(body)3233    headers = [(b"Content-Encoding", b"br")]34    response = httpcore.Response(200, headers=headers, body=compressed_body)35    assert response.body == body363738def test_multi():39    body = b"test 123"4041    deflate_compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)42    compressed_body = deflate_compressor.compress(body) + deflate_compressor.flush()4344    gzip_compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)45    compressed_body = (46        gzip_compressor.compress(compressed_body) + gzip_compressor.flush()47    )4849    headers = [(b"Content-Encoding", b"deflate, gzip")]50    response = httpcore.Response(200, headers=headers, body=compressed_body)51    assert response.body == body525354def test_multi_with_identity():55    body = b"test 123"56    compressed_body = brotli.compress(body)5758    headers = [(b"Content-Encoding", b"br, identity")]59    response = httpcore.Response(200, headers=headers, body=compressed_body)60    assert response.body == body6162    headers = [(b"Content-Encoding", b"identity, br")]63    response = httpcore.Response(200, headers=headers, body=compressed_body)64    assert response.body == body656667@pytest.mark.asyncio68async def test_streaming():69    body = b"test 123"70    compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)7172    async def compress(body):73        yield compressor.compress(body)74        yield compressor.flush()7576    headers = [(b"Content-Encoding", b"gzip")]77    response = httpcore.Response(200, headers=headers, body=compress(body))78    assert not hasattr(response, "body")79    assert await response.read() == body808182@pytest.mark.parametrize(83    'header_value, expected_exception',84    [85        (b"deflate", httpcore.DeflateDecodingError),86        (b"gzip", httpcore.GzipDecodingError),87        (b"br", httpcore.BrotliDecodingError),88    ])89def test_decoding_errors(header_value, expected_exception):90    headers = [(b"Content-Encoding", header_value)]91    body = b"test 123"92    compressed_body = brotli.compress(body)[3:]93    with pytest.raises(expected_exception):94        response = httpcore.Response(200, headers=headers, body=compressed_body)