Skip to content
Myr

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 file

Take it

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 1–14why, one step back
1import zlib
23import 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 back
15 response = httpcore.Response(200, headers=headers, content=compressed_body)
16 assert response.content == body

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 17–24why, one step back
17
1819def 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 back
25 response = httpcore.Response(200, headers=headers, content=compressed_body)
26 assert response.content == body

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 27–33why, one step back
27
2829def 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 back
34 response = httpcore.Response(200, headers=headers, content=compressed_body)
35 assert response.content == body

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 36–49why, one step back
36
3738def 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 back
50 response = httpcore.Response(200, headers=headers, content=compressed_body)
51 assert response.content == body

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 52–58why, one step back
52
5354def 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 back
59 response = httpcore.Response(200, headers=headers, content=compressed_body)
60 assert response.content == body

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 61–62why, one step back
61
62 headers = [(b"Content-Encoding", b"identity, br")]

Response.contentTom Christiebefore adoption4c55113

lines 63–64why, one step back
63 response = httpcore.Response(200, headers=headers, content=compressed_body)
64 assert response.content == body

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 65–76why, one step back
65
6667@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 back
77 response = httpcore.Response(200, headers=headers, content=compress(body))

Rejig test and tighten up models APITom Christiebefore adoptiondea4d42

lines 78–87why, one step back
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):

Response.contentTom Christiebefore adoption4c55113

lines 88–89why, one step back
88 response = httpcore.Response(200, headers=headers, content=compressed_body)
89 response.content