test_decoders.py
This file
Path
tests/test_decoders.py
171 lines · 5.0 KiB · Python
Last changed by
added control to calling decode for empty contents (#237)Can Sarıgölbefore adoption79425b2
History of this fileTake it
1import zlib23import brotli4import pytest56import httpx7from httpx.decoders import (8 BrotliDecoder,9 DeflateDecoder,10 GZipDecoder,11 IdentityDecoder,12 TextDecoder,13)141516def test_deflate():17 body = b"test 123"18 compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)19 compressed_body = compressor.compress(body) + compressor.flush()2021 headers = [(b"Content-Encoding", b"deflate")]22 response = httpx.Response(200, headers=headers, content=compressed_body)23 assert response.content == body242526def test_gzip():27 body = b"test 123"28 compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)29 compressed_body = compressor.compress(body) + compressor.flush()3031 headers = [(b"Content-Encoding", b"gzip")]32 response = httpx.Response(200, headers=headers, content=compressed_body)33 assert response.content == body343536def test_brotli():37 body = b"test 123"38 compressed_body = brotli.compress(body)3940 headers = [(b"Content-Encoding", b"br")]41 response = httpx.Response(200, headers=headers, content=compressed_body)42 assert response.content == body434445def test_multi():46 body = b"test 123"4748 deflate_compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)49 compressed_body = deflate_compressor.compress(body) + deflate_compressor.flush()5051 gzip_compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)52 compressed_body = (53 gzip_compressor.compress(compressed_body) + gzip_compressor.flush()54 )5556 headers = [(b"Content-Encoding", b"deflate, gzip")]57 response = httpx.Response(200, headers=headers, content=compressed_body)58 assert response.content == body596061def test_multi_with_identity():62 body = b"test 123"63 compressed_body = brotli.compress(body)6465 headers = [(b"Content-Encoding", b"br, identity")]66 response = httpx.Response(200, headers=headers, content=compressed_body)67 assert response.content == body6869 headers = [(b"Content-Encoding", b"identity, br")]70 response = httpx.Response(200, headers=headers, content=compressed_body)71 assert response.content == body727374def test_streaming():75 body = b"test 123"76 compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)7778 def compress(body):79 yield compressor.compress(body)80 yield compressor.flush()8182 headers = [(b"Content-Encoding", b"gzip")]83 response = httpx.Response(200, headers=headers, content=compress(body))84 assert not hasattr(response, "body")85 assert response.read() == body868788@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br", b"identity"))89def test_empty_content(header_value):90 headers = [(b"Content-Encoding", header_value)]91 response = httpx.Response(200, headers=headers, content=b"")92 assert response.content == b""939495@pytest.mark.parametrize(96 "decoder", (BrotliDecoder, DeflateDecoder, GZipDecoder, IdentityDecoder)97)98def test_decoders_empty_cases(decoder):99 instance = decoder()100 assert instance.decode(b"") == b""101 assert instance.flush() == b""102103104@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))105def test_decoding_errors(header_value):106 headers = [(b"Content-Encoding", header_value)]107 body = b"test 123"108 compressed_body = brotli.compress(body)[3:]109 with pytest.raises(httpx.exceptions.DecodingError):110 response = httpx.Response(200, headers=headers, content=compressed_body)111 response.content112113114@pytest.mark.parametrize(115 ["data", "encoding"],116 [117 ((b"Hello,", b" world!"), "ascii"),118 ((b"\xe3\x83", b"\x88\xe3\x83\xa9", b"\xe3", b"\x83\x99\xe3\x83\xab"), "utf-8"),119 ((b"\x83g\x83\x89\x83x\x83\x8b",) * 64, "shift-jis"),120 ((b"\x83g\x83\x89\x83x\x83\x8b",) * 600, "shift-jis"),121 (122 (b"\xcb\xee\xf0\xe5\xec \xe8\xef\xf1\xf3\xec \xe4\xee\xeb\xee\xf0",) * 64,123 "MacCyrillic",124 ),125 (126 (b"\xa5\xa6\xa5\xa7\xa5\xd6\xa4\xce\xb9\xf1\xba\xdd\xb2\xbd",) * 512,127 "euc-jp",128 ),129 ],130)131def test_text_decoder(data, encoding):132 def iterator():133 nonlocal data134 for chunk in data:135 yield chunk136137 response = httpx.Response(200, content=iterator())138 assert "".join(response.stream_text()) == (b"".join(data)).decode(encoding)139140141def test_text_decoder_known_encoding():142 def iterator():143 yield b"\x83g"144 yield b"\x83"145 yield b"\x89\x83x\x83\x8b"146147 response = httpx.Response(148 200,149 headers=[(b"Content-Type", b"text/html; charset=shift-jis")],150 content=iterator(),151 )152153 assert "".join(response.stream_text()) == "トラベル"154155156def test_text_decoder_empty_cases():157 decoder = TextDecoder()158 assert decoder.flush() == ""159160 decoder = TextDecoder()161 assert decoder.decode(b"") == ""162 assert decoder.flush() == ""163164165def test_invalid_content_encoding_header():166 headers = [(b"Content-Encoding", b"invalid-header")]167 body = b"test 123"168169 response = httpx.Response(200, headers=headers, content=body)170171 assert response.read() == body