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
The walk stopped after 200 commits: lines older than fa8d296 point at it.
Update READMETom Christiebefore adoptionfa8d296
lines 1–5why, one step back1import zlib23import brotli4import pytest5Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 6why, one step back6import httpxadded control to calling decode for empty contents (#237)Can Sarıgölbefore adoption79425b2
lines 7–13why, one step back7from httpx.decoders import (8 BrotliDecoder,9 DeflateDecoder,10 GZipDecoder,11 IdentityDecoder,12 TextDecoder,13)Update READMETom Christiebefore adoptionfa8d296
lines 14–21why, one step back141516def 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")]Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 22why, one step back22 response = httpx.Response(200, headers=headers, content=compressed_body)Update READMETom Christiebefore adoptionfa8d296
lines 23–31why, one step back23 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")]Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 32why, one step back32 response = httpx.Response(200, headers=headers, content=compressed_body)Update READMETom Christiebefore adoptionfa8d296
lines 33–40why, one step back33 assert response.content == body343536def test_brotli():37 body = b"test 123"38 compressed_body = brotli.compress(body)3940 headers = [(b"Content-Encoding", b"br")]Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 41why, one step back41 response = httpx.Response(200, headers=headers, content=compressed_body)Update READMETom Christiebefore adoptionfa8d296
lines 42–56why, one step back42 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")]Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 57why, one step back57 response = httpx.Response(200, headers=headers, content=compressed_body)Update READMETom Christiebefore adoptionfa8d296
lines 58–65why, one step back58 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")]Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 66why, one step back66 response = httpx.Response(200, headers=headers, content=compressed_body)Update READMETom Christiebefore adoptionfa8d296
lines 67–69why, one step back67 assert response.content == body6869 headers = [(b"Content-Encoding", b"identity, br")]Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 70why, one step back70 response = httpx.Response(200, headers=headers, content=compressed_body)Update READMETom Christiebefore adoptionfa8d296
lines 71–73why, one step back71 assert response.content == body7273Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 74why, one step back74def test_streaming():Update READMETom Christiebefore adoptionfa8d296
lines 75–77why, one step back75 body = b"test 123"76 compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)77Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 78why, one step back78 def compress(body):Update READMETom Christiebefore adoptionfa8d296
lines 79–82why, one step back79 yield compressor.compress(body)80 yield compressor.flush()8182 headers = [(b"Content-Encoding", b"gzip")]Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
line 83why, one step back83 response = httpx.Response(200, headers=headers, content=compress(body))Update READMETom Christiebefore adoptionfa8d296
line 84why, one step back84 assert not hasattr(response, "body")Sync or Async dispatch (#83)Tom Christiebefore adoption0cbf3c7
line 85why, one step back85 assert response.read() == bodyadded control to calling decode for empty contents (#237)Can Sarıgölbefore adoption79425b2
lines 86–101why, one step back868788@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""Update READMETom Christiebefore adoptionfa8d296
lines 102–108why, one step back102103104@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:]Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
lines 109–110why, one step back109 with pytest.raises(httpx.exceptions.DecodingError):110 response = httpx.Response(200, headers=headers, content=compressed_body)Update READMETom Christiebefore adoptionfa8d296
line 111why, one step back111 response.contentImplement Response.stream_text() (#183)Seth Michael Larsonbefore adoptionaf907e8
lines 112–162why, one step back112113114@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() == ""Ignore Content-Encoding headers that are invalid (#196)Andreas Bernaccabefore adoptionde91fdf
lines 163–164why, one step back163164Fix invalid Content-Encoding header test (#197)Quentin Pradetbefore adoptionebbc003
lines 165–166why, one step back165def test_invalid_content_encoding_header():166 headers = [(b"Content-Encoding", b"invalid-header")]Ignore Content-Encoding headers that are invalid (#196)Andreas Bernaccabefore adoptionde91fdf
lines 167–171why, one step back167 body = b"test 123"168169 response = httpx.Response(200, headers=headers, content=body)170171 assert response.read() == body