Skip to content
Myr

test_decoding.py

This file

Path

tests/test_decoding.py

88 lines · 2.7 KiB · Python

Last changed by

Raise single DecodingErrorYeray Diaz Diazbefore adoption6b47423

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("header_value", (b"deflate", b"gzip", b"br"))83def test_decoding_errors(header_value):84    headers = [(b"Content-Encoding", header_value)]85    body = b"test 123"86    compressed_body = brotli.compress(body)[3:]87    with pytest.raises(httpcore.exceptions.DecodingError):88        response = httpcore.Response(200, headers=headers, body=compressed_body)