test_decoders.py
This file
Path
tests/test_decoders.py
246 lines · 7.3 KiB · Python
Last changed by
Bump coverage (#705)Tom Christiebefore adoptionb8394f1
History of this fileTake it
1import zlib23import brotli4import pytest56import httpx7from httpx.content_streams import AsyncIteratorStream8from httpx.decoders import (9 BrotliDecoder,10 DeflateDecoder,11 GZipDecoder,12 IdentityDecoder,13 LineDecoder,14 TextDecoder,15)1617REQUEST = httpx.Request("GET", "https://example.org")181920def test_deflate():21 body = b"test 123"22 compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)23 compressed_body = compressor.compress(body) + compressor.flush()2425 headers = [(b"Content-Encoding", b"deflate")]26 response = httpx.Response(27 200, headers=headers, content=compressed_body, request=REQUEST28 )29 assert response.content == body303132def test_gzip():33 body = b"test 123"34 compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)35 compressed_body = compressor.compress(body) + compressor.flush()3637 headers = [(b"Content-Encoding", b"gzip")]38 response = httpx.Response(39 200, headers=headers, content=compressed_body, request=REQUEST40 )41 assert response.content == body424344def test_brotli():45 body = b"test 123"46 compressed_body = brotli.compress(body)4748 headers = [(b"Content-Encoding", b"br")]49 response = httpx.Response(50 200, headers=headers, content=compressed_body, request=REQUEST51 )52 assert response.content == body535455def test_multi():56 body = b"test 123"5758 deflate_compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)59 compressed_body = deflate_compressor.compress(body) + deflate_compressor.flush()6061 gzip_compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)62 compressed_body = (63 gzip_compressor.compress(compressed_body) + gzip_compressor.flush()64 )6566 headers = [(b"Content-Encoding", b"deflate, gzip")]67 response = httpx.Response(68 200, headers=headers, content=compressed_body, request=REQUEST69 )70 assert response.content == body717273def test_multi_with_identity():74 body = b"test 123"75 compressed_body = brotli.compress(body)7677 headers = [(b"Content-Encoding", b"br, identity")]78 response = httpx.Response(79 200, headers=headers, content=compressed_body, request=REQUEST80 )81 assert response.content == body8283 headers = [(b"Content-Encoding", b"identity, br")]84 response = httpx.Response(85 200, headers=headers, content=compressed_body, request=REQUEST86 )87 assert response.content == body888990@pytest.mark.asyncio91async def test_streaming():92 body = b"test 123"93 compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)9495 async def compress(body):96 yield compressor.compress(body)97 yield compressor.flush()9899 headers = [(b"Content-Encoding", b"gzip")]100 stream = AsyncIteratorStream(aiterator=compress(body))101 response = httpx.Response(200, headers=headers, stream=stream, request=REQUEST)102 assert not hasattr(response, "body")103 assert await response.aread() == body104105106@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br", b"identity"))107def test_empty_content(header_value):108 headers = [(b"Content-Encoding", header_value)]109 response = httpx.Response(200, headers=headers, content=b"", request=REQUEST)110 assert response.content == b""111112113@pytest.mark.parametrize(114 "decoder", (BrotliDecoder, DeflateDecoder, GZipDecoder, IdentityDecoder)115)116def test_decoders_empty_cases(decoder):117 instance = decoder()118 assert instance.decode(b"") == b""119 assert instance.flush() == b""120121122@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))123def test_decoding_errors(header_value):124 headers = [(b"Content-Encoding", header_value)]125 body = b"test 123"126 compressed_body = brotli.compress(body)[3:]127 with pytest.raises(httpx.DecodingError):128 response = httpx.Response(129 200, headers=headers, content=compressed_body, request=REQUEST130 )131 response.content132133134@pytest.mark.parametrize(135 ["data", "encoding"],136 [137 ((b"Hello,", b" world!"), "ascii"),138 ((b"\xe3\x83", b"\x88\xe3\x83\xa9", b"\xe3", b"\x83\x99\xe3\x83\xab"), "utf-8"),139 ((b"\x83g\x83\x89\x83x\x83\x8b",) * 64, "shift-jis"),140 ((b"\x83g\x83\x89\x83x\x83\x8b",) * 600, "shift-jis"),141 (142 (b"\xcb\xee\xf0\xe5\xec \xe8\xef\xf1\xf3\xec \xe4\xee\xeb\xee\xf0",) * 64,143 "MacCyrillic",144 ),145 (146 (b"\xa5\xa6\xa5\xa7\xa5\xd6\xa4\xce\xb9\xf1\xba\xdd\xb2\xbd",) * 512,147 "euc-jp",148 ),149 ],150)151@pytest.mark.asyncio152async def test_text_decoder(data, encoding):153 async def iterator():154 nonlocal data155 for chunk in data:156 yield chunk157158 # Accessing `.text` on a read response.159 stream = AsyncIteratorStream(aiterator=iterator())160 response = httpx.Response(200, stream=stream, request=REQUEST)161 await response.aread()162 assert response.text == (b"".join(data)).decode(encoding)163164 # Streaming `.aiter_text` iteratively.165 stream = AsyncIteratorStream(aiterator=iterator())166 response = httpx.Response(200, stream=stream, request=REQUEST)167 text = "".join([part async for part in response.aiter_text()])168 assert text == (b"".join(data)).decode(encoding)169170171@pytest.mark.asyncio172async def test_text_decoder_known_encoding():173 async def iterator():174 yield b"\x83g"175 yield b"\x83"176 yield b"\x89\x83x\x83\x8b"177178 stream = AsyncIteratorStream(aiterator=iterator())179 response = httpx.Response(180 200,181 headers=[(b"Content-Type", b"text/html; charset=shift-jis")],182 stream=stream,183 request=REQUEST,184 )185186 await response.aread()187 assert "".join(response.text) == "トラベル"188189190def test_text_decoder_empty_cases():191 decoder = TextDecoder()192 assert decoder.flush() == ""193194 decoder = TextDecoder()195 assert decoder.decode(b"") == ""196 assert decoder.flush() == ""197198199def test_line_decoder_nl():200 decoder = LineDecoder()201 assert decoder.decode("") == []202 assert decoder.decode("a\n\nb\nc") == ["a\n", "\n", "b\n"]203 assert decoder.flush() == ["c"]204205 decoder = LineDecoder()206 assert decoder.decode("") == []207 assert decoder.decode("a\n\nb\nc\n") == ["a\n", "\n", "b\n", "c\n"]208 assert decoder.flush() == []209210211def test_line_decoder_cr():212 decoder = LineDecoder()213 assert decoder.decode("") == []214 assert decoder.decode("a\r\rb\rc") == ["a\n", "\n", "b\n"]215 assert decoder.flush() == ["c"]216217 decoder = LineDecoder()218 assert decoder.decode("") == []219 assert decoder.decode("a\r\rb\rc\r") == ["a\n", "\n", "b\n"]220 assert decoder.flush() == ["c\n"]221222223def test_line_decoder_crnl():224 decoder = LineDecoder()225 assert decoder.decode("") == []226 assert decoder.decode("a\r\n\r\nb\r\nc") == ["a\n", "\n", "b\n"]227 assert decoder.flush() == ["c"]228229 decoder = LineDecoder()230 assert decoder.decode("") == []231 assert decoder.decode("a\r\n\r\nb\r\nc\r\n") == ["a\n", "\n", "b\n", "c\n"]232 assert decoder.flush() == []233234 decoder = LineDecoder()235 assert decoder.decode("") == []236 assert decoder.decode("a\r") == []237 assert decoder.decode("\n\r\nb\r\nc") == ["a\n", "\n", "b\n"]238 assert decoder.flush() == ["c"]239240241def test_invalid_content_encoding_header():242 headers = [(b"Content-Encoding", b"invalid-header")]243 body = b"test 123"244245 response = httpx.Response(200, headers=headers, content=body, request=REQUEST)246 assert response.content == body