test_decoders.py
This file
Path
tests/test_decoders.py
285 lines · 8.3 KiB · Python
Last changed by
Fixed `iter_text` adding an empty string (#2998)James Brazabefore adoption1e11096
History of this fileTake it
The walk stopped after 200 commits: lines older than 8e5e3b8 point at it.
Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c
line 1why, one step back1import typingupdate requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
lines 2–62why, one step back2import zlib34import chardet5import pytest67import httpx8910def test_deflate():11 """12 Deflate encoding may use either 'zlib' or 'deflate' in the wild.1314 https://stackoverflow.com/questions/1838699/how-can-i-decompress-a-gzip-stream-with-zlib#answer-2231129715 """16 body = b"test 123"17 compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)18 compressed_body = compressor.compress(body) + compressor.flush()1920 headers = [(b"Content-Encoding", b"deflate")]21 response = httpx.Response(22 200,23 headers=headers,24 content=compressed_body,25 )26 assert response.content == body272829def test_zlib():30 """31 Deflate encoding may use either 'zlib' or 'deflate' in the wild.3233 https://stackoverflow.com/questions/1838699/how-can-i-decompress-a-gzip-stream-with-zlib#answer-2231129734 """35 body = b"test 123"36 compressed_body = zlib.compress(body)3738 headers = [(b"Content-Encoding", b"deflate")]39 response = httpx.Response(40 200,41 headers=headers,42 content=compressed_body,43 )44 assert response.content == body454647def test_gzip():48 body = b"test 123"49 compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)50 compressed_body = compressor.compress(body) + compressor.flush()5152 headers = [(b"Content-Encoding", b"gzip")]53 response = httpx.Response(54 200,55 headers=headers,56 content=compressed_body,57 )58 assert response.content == body596061def test_brotli():62 body = b"test 123"Inline Brotli samples in tests (#2935)Michał Górnybefore adoptionf653b2f
line 63why, one step back63 compressed_body = b"\x8b\x03\x80test 123\x03"update requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
lines 64–95why, one step back6465 headers = [(b"Content-Encoding", b"br")]66 response = httpx.Response(67 200,68 headers=headers,69 content=compressed_body,70 )71 assert response.content == body727374def test_multi():75 body = b"test 123"7677 deflate_compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)78 compressed_body = deflate_compressor.compress(body) + deflate_compressor.flush()7980 gzip_compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)81 compressed_body = (82 gzip_compressor.compress(compressed_body) + gzip_compressor.flush()83 )8485 headers = [(b"Content-Encoding", b"deflate, gzip")]86 response = httpx.Response(87 200,88 headers=headers,89 content=compressed_body,90 )91 assert response.content == body929394def test_multi_with_identity():95 body = b"test 123"Inline Brotli samples in tests (#2935)Michał Górnybefore adoptionf653b2f
line 96why, one step back96 compressed_body = b"\x8b\x03\x80test 123\x03"update requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
lines 97–114why, one step back9798 headers = [(b"Content-Encoding", b"br, identity")]99 response = httpx.Response(100 200,101 headers=headers,102 content=compressed_body,103 )104 assert response.content == body105106 headers = [(b"Content-Encoding", b"identity, br")]107 response = httpx.Response(108 200,109 headers=headers,110 content=compressed_body,111 )112 assert response.content == body113114replace pytest-asyncio and pytest-trio with anyio (#2512)Thomas Graingerbefore adoptione27d1b8
line 115why, one step back115@pytest.mark.anyioupdate requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
lines 116–119why, one step back116async def test_streaming():117 body = b"test 123"118 compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)119Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c
line 120why, one step back120 async def compress(body: bytes) -> typing.AsyncIterator[bytes]:update requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
lines 121–144why, one step back121 yield compressor.compress(body)122 yield compressor.flush()123124 headers = [(b"Content-Encoding", b"gzip")]125 response = httpx.Response(126 200,127 headers=headers,128 content=compress(body),129 )130 assert not hasattr(response, "body")131 assert await response.aread() == body132133134@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br", b"identity"))135def test_empty_content(header_value):136 headers = [(b"Content-Encoding", header_value)]137 response = httpx.Response(138 200,139 headers=headers,140 content=b"",141 )142 assert response.content == b""143144Remove some private imports from test_decoders (#2496)Tom Christiebefore adoption563a103
lines 145–149why, one step back145@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br", b"identity"))146def test_decoders_empty_cases(header_value):147 headers = [(b"Content-Encoding", header_value)]148 response = httpx.Response(content=b"", status_code=200, headers=headers)149 assert response.read() == b""update requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
lines 150–154why, one step back150151152@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))153def test_decoding_errors(header_value):154 headers = [(b"Content-Encoding", header_value)]Inline Brotli samples in tests (#2935)Michał Górnybefore adoptionf653b2f
line 155why, one step back155 compressed_body = b"invalid"update requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
lines 156–172why, one step back156 with pytest.raises(httpx.DecodingError):157 request = httpx.Request("GET", "https://example.org")158 httpx.Response(200, headers=headers, content=compressed_body, request=request)159160 with pytest.raises(httpx.DecodingError):161 httpx.Response(200, headers=headers, content=compressed_body)162163164@pytest.mark.parametrize(165 ["data", "encoding"],166 [167 ((b"Hello,", b" world!"), "ascii"),168 ((b"\xe3\x83", b"\x88\xe3\x83\xa9", b"\xe3", b"\x83\x99\xe3\x83\xab"), "utf-8"),169 ((b"Euro character: \x88! abcdefghijklmnopqrstuvwxyz", b""), "cp1252"),170 ((b"Accented: \xd6sterreich abcdefghijklmnopqrstuvwxyz", b""), "iso-8859-1"),171 ],172)replace pytest-asyncio and pytest-trio with anyio (#2512)Thomas Graingerbefore adoptione27d1b8
line 173why, one step back173@pytest.mark.anyioupdate requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
line 174why, one step back174async def test_text_decoder_with_autodetect(data, encoding):Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c
line 175why, one step back175 async def iterator() -> typing.AsyncIterator[bytes]:update requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
lines 176–195why, one step back176 nonlocal data177 for chunk in data:178 yield chunk179180 def autodetect(content):181 return chardet.detect(content).get("encoding")182183 # Accessing `.text` on a read response.184 response = httpx.Response(200, content=iterator(), default_encoding=autodetect)185 await response.aread()186 assert response.text == (b"".join(data)).decode(encoding)187188 # Streaming `.aiter_text` iteratively.189 # Note that if we streamed the text *without* having read it first, then190 # we won't get a `charset_normalizer` guess, and will instead always rely191 # on utf-8 if no charset is specified.192 text = "".join([part async for part in response.aiter_text()])193 assert text == (b"".join(data)).decode(encoding)194195replace pytest-asyncio and pytest-trio with anyio (#2512)Thomas Graingerbefore adoptione27d1b8
line 196why, one step back196@pytest.mark.anyioupdate requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
line 197why, one step back197async def test_text_decoder_known_encoding():Typing: enable disallow_untyped_calls (#2479)Martijn Pietersbefore adoption933551c
line 198why, one step back198 async def iterator() -> typing.AsyncIterator[bytes]:update requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
lines 199–213why, one step back199 yield b"\x83g"200 yield b"\x83"201 yield b"\x89\x83x\x83\x8b"202203 response = httpx.Response(204 200,205 headers=[(b"Content-Type", b"text/html; charset=shift-jis")],206 content=iterator(),207 )208209 await response.aread()210 assert "".join(response.text) == "トラベル"211212213def test_text_decoder_empty_cases():Drop private imports from test_decoders.py (#2570)Florimond Mancabefore adoptioncca6206
lines 214–215why, one step back214 response = httpx.Response(200, content=b"")215 assert response.text == ""update requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
line 216why, one step back216Drop private imports from test_decoders.py (#2570)Florimond Mancabefore adoptioncca6206
lines 217–219why, one step back217 response = httpx.Response(200, content=[b""])218 response.read()219 assert response.text == ""update requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
lines 220–221why, one step back220221Fixed `iter_text` adding an empty string (#2998)James Brazabefore adoption1e11096
lines 222–232why, one step back222@pytest.mark.parametrize(223 ["data", "expected"],224 [((b"Hello,", b" world!"), ["Hello,", " world!"])],225)226def test_streaming_text_decoder(227 data: typing.Iterable[bytes], expected: typing.List[str]228) -> None:229 response = httpx.Response(200, content=iter(data))230 assert list(response.iter_text()) == expected231232update requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
line 233why, one step back233def test_line_decoder_nl():Drop private imports from test_decoders.py (#2570)Florimond Mancabefore adoptioncca6206
lines 234–235why, one step back234 response = httpx.Response(200, content=[b""])235 assert list(response.iter_lines()) == []update requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
line 236why, one step back236Drop private imports from test_decoders.py (#2570)Florimond Mancabefore adoptioncca6206
lines 237–238why, one step back237 response = httpx.Response(200, content=[b"", b"a\n\nb\nc"])238 assert list(response.iter_lines()) == ["a", "", "b", "c"]update requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
lines 239–240why, one step back239240 # Issue #1033Drop private imports from test_decoders.py (#2570)Florimond Mancabefore adoptioncca6206
lines 241–244why, one step back241 response = httpx.Response(242 200, content=[b"", b"12345\n", b"foo ", b"bar ", b"baz\n"]243 )244 assert list(response.iter_lines()) == ["12345", "foo bar baz"]update requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
lines 245–247why, one step back245246247def test_line_decoder_cr():Drop private imports from test_decoders.py (#2570)Florimond Mancabefore adoptioncca6206
lines 248–249why, one step back248 response = httpx.Response(200, content=[b"", b"a\r\rb\rc"])249 assert list(response.iter_lines()) == ["a", "", "b", "c"]update requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
line 250why, one step back250Drop private imports from test_decoders.py (#2570)Florimond Mancabefore adoptioncca6206
lines 251–252why, one step back251 response = httpx.Response(200, content=[b"", b"a\r\rb\rc\r"])252 assert list(response.iter_lines()) == ["a", "", "b", "c"]update requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
lines 253–254why, one step back253254 # Issue #1033Drop private imports from test_decoders.py (#2570)Florimond Mancabefore adoptioncca6206
lines 255–258why, one step back255 response = httpx.Response(256 200, content=[b"", b"12345\r", b"foo ", b"bar ", b"baz\r"]257 )258 assert list(response.iter_lines()) == ["12345", "foo bar baz"]update requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
lines 259–261why, one step back259260261def test_line_decoder_crnl():Drop private imports from test_decoders.py (#2570)Florimond Mancabefore adoptioncca6206
lines 262–263why, one step back262 response = httpx.Response(200, content=[b"", b"a\r\n\r\nb\r\nc"])263 assert list(response.iter_lines()) == ["a", "", "b", "c"]update requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
line 264why, one step back264Drop private imports from test_decoders.py (#2570)Florimond Mancabefore adoptioncca6206
lines 265–266why, one step back265 response = httpx.Response(200, content=[b"", b"a\r\n\r\nb\r\nc\r\n"])266 assert list(response.iter_lines()) == ["a", "", "b", "c"]update requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
line 267why, one step back267Drop private imports from test_decoders.py (#2570)Florimond Mancabefore adoptioncca6206
lines 268–269why, one step back268 response = httpx.Response(200, content=[b"", b"a\r", b"\n\r\nb\r\nc"])269 assert list(response.iter_lines()) == ["a", "", "b", "c"]update requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
lines 270–271why, one step back270271 # Issue #1033Drop private imports from test_decoders.py (#2570)Florimond Mancabefore adoptioncca6206
lines 272–273why, one step back272 response = httpx.Response(200, content=[b"", b"12345\r\n", b"foo bar baz\r\n"])273 assert list(response.iter_lines()) == ["12345", "foo bar baz"]update requests compatibility docs on query and form params (#2461)Ben Falkbefore adoption8e5e3b8
lines 274–285why, one step back274275276def test_invalid_content_encoding_header():277 headers = [(b"Content-Encoding", b"invalid-header")]278 body = b"test 123"279280 response = httpx.Response(281 200,282 headers=headers,283 content=body,284 )285 assert response.content == body