test_decoders.py
This file
Path
tests/test_decoders.py
89 lines · 2.8 KiB · Python
Last changed by
Response.contentTom Christiebefore adoption4c55113
History of this fileTake it
Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 1–14why, one step back1import 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")]Response.contentTom Christiebefore adoption4c55113
lines 15–16why, one step back15 response = httpcore.Response(200, headers=headers, content=compressed_body)16 assert response.content == bodyRejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 17–24why, one step back171819def 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")]Response.contentTom Christiebefore adoption4c55113
lines 25–26why, one step back25 response = httpcore.Response(200, headers=headers, content=compressed_body)26 assert response.content == bodyRejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 27–33why, one step back272829def test_brotli():30 body = b"test 123"31 compressed_body = brotli.compress(body)3233 headers = [(b"Content-Encoding", b"br")]Response.contentTom Christiebefore adoption4c55113
lines 34–35why, one step back34 response = httpcore.Response(200, headers=headers, content=compressed_body)35 assert response.content == bodyRejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 36–49why, one step back363738def 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")]Response.contentTom Christiebefore adoption4c55113
lines 50–51why, one step back50 response = httpcore.Response(200, headers=headers, content=compressed_body)51 assert response.content == bodyRejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 52–58why, one step back525354def test_multi_with_identity():55 body = b"test 123"56 compressed_body = brotli.compress(body)5758 headers = [(b"Content-Encoding", b"br, identity")]Response.contentTom Christiebefore adoption4c55113
lines 59–60why, one step back59 response = httpcore.Response(200, headers=headers, content=compressed_body)60 assert response.content == bodyRejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 61–62why, one step back6162 headers = [(b"Content-Encoding", b"identity, br")]Response.contentTom Christiebefore adoption4c55113
lines 63–64why, one step back63 response = httpcore.Response(200, headers=headers, content=compressed_body)64 assert response.content == bodyRejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 65–76why, one step back656667@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")]Response.contentTom Christiebefore adoption4c55113
line 77why, one step back77 response = httpcore.Response(200, headers=headers, content=compress(body))Rejig test and tighten up models APITom Christiebefore adoptiondea4d42
lines 78–87why, one step back78 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):Response.contentTom Christiebefore adoption4c55113
lines 88–89why, one step back88 response = httpcore.Response(200, headers=headers, content=compressed_body)89 response.content