test_decoders.py
This file
Path
tests/test_decoders.py
243 lines · 7.2 KiB · Python
Last changed by
More coverage improvements (#711)Tom Christiebefore adoptionf5eaec7
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 httpx.Response(200, headers=headers, content=compressed_body, request=REQUEST)129130131@pytest.mark.parametrize(132 ["data", "encoding"],133 [134 ((b"Hello,", b" world!"), "ascii"),135 ((b"\xe3\x83", b"\x88\xe3\x83\xa9", b"\xe3", b"\x83\x99\xe3\x83\xab"), "utf-8"),136 ((b"\x83g\x83\x89\x83x\x83\x8b",) * 64, "shift-jis"),137 ((b"\x83g\x83\x89\x83x\x83\x8b",) * 600, "shift-jis"),138 (139 (b"\xcb\xee\xf0\xe5\xec \xe8\xef\xf1\xf3\xec \xe4\xee\xeb\xee\xf0",) * 64,140 "MacCyrillic",141 ),142 (143 (b"\xa5\xa6\xa5\xa7\xa5\xd6\xa4\xce\xb9\xf1\xba\xdd\xb2\xbd",) * 512,144 "euc-jp",145 ),146 ],147)148@pytest.mark.asyncio149async def test_text_decoder(data, encoding):150 async def iterator():151 nonlocal data152 for chunk in data:153 yield chunk154155 # Accessing `.text` on a read response.156 stream = AsyncIteratorStream(aiterator=iterator())157 response = httpx.Response(200, stream=stream, request=REQUEST)158 await response.aread()159 assert response.text == (b"".join(data)).decode(encoding)160161 # Streaming `.aiter_text` iteratively.162 stream = AsyncIteratorStream(aiterator=iterator())163 response = httpx.Response(200, stream=stream, request=REQUEST)164 text = "".join([part async for part in response.aiter_text()])165 assert text == (b"".join(data)).decode(encoding)166167168@pytest.mark.asyncio169async def test_text_decoder_known_encoding():170 async def iterator():171 yield b"\x83g"172 yield b"\x83"173 yield b"\x89\x83x\x83\x8b"174175 stream = AsyncIteratorStream(aiterator=iterator())176 response = httpx.Response(177 200,178 headers=[(b"Content-Type", b"text/html; charset=shift-jis")],179 stream=stream,180 request=REQUEST,181 )182183 await response.aread()184 assert "".join(response.text) == "トラベル"185186187def test_text_decoder_empty_cases():188 decoder = TextDecoder()189 assert decoder.flush() == ""190191 decoder = TextDecoder()192 assert decoder.decode(b"") == ""193 assert decoder.flush() == ""194195196def test_line_decoder_nl():197 decoder = LineDecoder()198 assert decoder.decode("") == []199 assert decoder.decode("a\n\nb\nc") == ["a\n", "\n", "b\n"]200 assert decoder.flush() == ["c"]201202 decoder = LineDecoder()203 assert decoder.decode("") == []204 assert decoder.decode("a\n\nb\nc\n") == ["a\n", "\n", "b\n", "c\n"]205 assert decoder.flush() == []206207208def test_line_decoder_cr():209 decoder = LineDecoder()210 assert decoder.decode("") == []211 assert decoder.decode("a\r\rb\rc") == ["a\n", "\n", "b\n"]212 assert decoder.flush() == ["c"]213214 decoder = LineDecoder()215 assert decoder.decode("") == []216 assert decoder.decode("a\r\rb\rc\r") == ["a\n", "\n", "b\n"]217 assert decoder.flush() == ["c\n"]218219220def test_line_decoder_crnl():221 decoder = LineDecoder()222 assert decoder.decode("") == []223 assert decoder.decode("a\r\n\r\nb\r\nc") == ["a\n", "\n", "b\n"]224 assert decoder.flush() == ["c"]225226 decoder = LineDecoder()227 assert decoder.decode("") == []228 assert decoder.decode("a\r\n\r\nb\r\nc\r\n") == ["a\n", "\n", "b\n", "c\n"]229 assert decoder.flush() == []230231 decoder = LineDecoder()232 assert decoder.decode("") == []233 assert decoder.decode("a\r") == []234 assert decoder.decode("\n\r\nb\r\nc") == ["a\n", "\n", "b\n"]235 assert decoder.flush() == ["c"]236237238def test_invalid_content_encoding_header():239 headers = [(b"Content-Encoding", b"invalid-header")]240 body = b"test 123"241242 response = httpx.Response(200, headers=headers, content=body, request=REQUEST)243 assert response.content == body