test_decoders.py
This file
Path
tests/test_decoders.py
294 lines · 8.9 KiB · Python
Last changed by
Exception hierarchy (#1095)Tom Christiebefore adoption9409900
History of this fileTake it
The walk stopped after 200 commits: lines older than 11e7604 point at it.
Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 1–6why, one step back1import zlib23import brotli4import pytest56import httpxSwitch to private module names (#785)Florimond Mancabefore adoption82dc6f3
lines 7–8why, one step back7from httpx._content_streams import AsyncIteratorStream8from httpx._decoders import (Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 9–20why, one step back9 BrotliDecoder,10 DeflateDecoder,11 GZipDecoder,12 IdentityDecoder,13 LineDecoder,14 TextDecoder,15)1617REQUEST = httpx.Request("GET", "https://example.org")181920def test_deflate():Support both zlib and deflate encodings (#758)Tom Christiebefore adoption780d184
lines 21–25why, one step back21 """22 Deflate encoding may use either 'zlib' or 'deflate' in the wild.2324 https://stackoverflow.com/questions/1838699/how-can-i-decompress-a-gzip-stream-with-zlib#answer-2231129725 """Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 26–28why, one step back26 body = b"test 123"27 compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)28 compressed_body = compressor.compress(body) + compressor.flush()Support both zlib and deflate encodings (#758)Tom Christiebefore adoption780d184
lines 29–44why, one step back2930 headers = [(b"Content-Encoding", b"deflate")]31 response = httpx.Response(32 200, headers=headers, content=compressed_body, request=REQUEST33 )34 assert response.content == body353637def test_zlib():38 """39 Deflate encoding may use either 'zlib' or 'deflate' in the wild.4041 https://stackoverflow.com/questions/1838699/how-can-i-decompress-a-gzip-stream-with-zlib#answer-2231129742 """43 body = b"test 123"44 compressed_body = zlib.compress(body)Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 45–137why, one step back4546 headers = [(b"Content-Encoding", b"deflate")]47 response = httpx.Response(48 200, headers=headers, content=compressed_body, request=REQUEST49 )50 assert response.content == body515253def test_gzip():54 body = b"test 123"55 compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)56 compressed_body = compressor.compress(body) + compressor.flush()5758 headers = [(b"Content-Encoding", b"gzip")]59 response = httpx.Response(60 200, headers=headers, content=compressed_body, request=REQUEST61 )62 assert response.content == body636465def test_brotli():66 body = b"test 123"67 compressed_body = brotli.compress(body)6869 headers = [(b"Content-Encoding", b"br")]70 response = httpx.Response(71 200, headers=headers, content=compressed_body, request=REQUEST72 )73 assert response.content == body747576def test_multi():77 body = b"test 123"7879 deflate_compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)80 compressed_body = deflate_compressor.compress(body) + deflate_compressor.flush()8182 gzip_compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)83 compressed_body = (84 gzip_compressor.compress(compressed_body) + gzip_compressor.flush()85 )8687 headers = [(b"Content-Encoding", b"deflate, gzip")]88 response = httpx.Response(89 200, headers=headers, content=compressed_body, request=REQUEST90 )91 assert response.content == body929394def test_multi_with_identity():95 body = b"test 123"96 compressed_body = brotli.compress(body)9798 headers = [(b"Content-Encoding", b"br, identity")]99 response = httpx.Response(100 200, headers=headers, content=compressed_body, request=REQUEST101 )102 assert response.content == body103104 headers = [(b"Content-Encoding", b"identity, br")]105 response = httpx.Response(106 200, headers=headers, content=compressed_body, request=REQUEST107 )108 assert response.content == body109110111@pytest.mark.asyncio112async def test_streaming():113 body = b"test 123"114 compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)115116 async def compress(body):117 yield compressor.compress(body)118 yield compressor.flush()119120 headers = [(b"Content-Encoding", b"gzip")]121 stream = AsyncIteratorStream(aiterator=compress(body))122 response = httpx.Response(200, headers=headers, stream=stream, request=REQUEST)123 assert not hasattr(response, "body")124 assert await response.aread() == body125126127@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br", b"identity"))128def test_empty_content(header_value):129 headers = [(b"Content-Encoding", header_value)]130 response = httpx.Response(200, headers=headers, content=b"", request=REQUEST)131 assert response.content == b""132133134@pytest.mark.parametrize(135 "decoder", (BrotliDecoder, DeflateDecoder, GZipDecoder, IdentityDecoder)136)137def test_decoders_empty_cases(decoder):Exception hierarchy (#1095)Tom Christiebefore adoption9409900
lines 138–139why, one step back138 request = httpx.Request(method="GET", url="https://www.example.com")139 instance = decoder(request)Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 140–149why, one step back140 assert instance.decode(b"") == b""141 assert instance.flush() == b""142143144@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))145def test_decoding_errors(header_value):146 headers = [(b"Content-Encoding", header_value)]147 body = b"test 123"148 compressed_body = brotli.compress(body)[3:]149 with pytest.raises(httpx.DecodingError):More coverage improvements (#711)Tom Christiebefore adoptionf5eaec7
line 150why, one step back150 httpx.Response(200, headers=headers, content=compressed_body, request=REQUEST)Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 151–209why, one step back151152153@pytest.mark.parametrize(154 ["data", "encoding"],155 [156 ((b"Hello,", b" world!"), "ascii"),157 ((b"\xe3\x83", b"\x88\xe3\x83\xa9", b"\xe3", b"\x83\x99\xe3\x83\xab"), "utf-8"),158 ((b"\x83g\x83\x89\x83x\x83\x8b",) * 64, "shift-jis"),159 ((b"\x83g\x83\x89\x83x\x83\x8b",) * 600, "shift-jis"),160 (161 (b"\xcb\xee\xf0\xe5\xec \xe8\xef\xf1\xf3\xec \xe4\xee\xeb\xee\xf0",) * 64,162 "MacCyrillic",163 ),164 (165 (b"\xa5\xa6\xa5\xa7\xa5\xd6\xa4\xce\xb9\xf1\xba\xdd\xb2\xbd",) * 512,166 "euc-jp",167 ),168 ],169)170@pytest.mark.asyncio171async def test_text_decoder(data, encoding):172 async def iterator():173 nonlocal data174 for chunk in data:175 yield chunk176177 # Accessing `.text` on a read response.178 stream = AsyncIteratorStream(aiterator=iterator())179 response = httpx.Response(200, stream=stream, request=REQUEST)180 await response.aread()181 assert response.text == (b"".join(data)).decode(encoding)182183 # Streaming `.aiter_text` iteratively.184 stream = AsyncIteratorStream(aiterator=iterator())185 response = httpx.Response(200, stream=stream, request=REQUEST)186 text = "".join([part async for part in response.aiter_text()])187 assert text == (b"".join(data)).decode(encoding)188189190@pytest.mark.asyncio191async def test_text_decoder_known_encoding():192 async def iterator():193 yield b"\x83g"194 yield b"\x83"195 yield b"\x89\x83x\x83\x8b"196197 stream = AsyncIteratorStream(aiterator=iterator())198 response = httpx.Response(199 200,200 headers=[(b"Content-Type", b"text/html; charset=shift-jis")],201 stream=stream,202 request=REQUEST,203 )204205 await response.aread()206 assert "".join(response.text) == "トラベル"207208209def test_text_decoder_empty_cases():Exception hierarchy (#1095)Tom Christiebefore adoption9409900
lines 210–212why, one step back210 request = httpx.Request(method="GET", url="https://www.example.com")211212 decoder = TextDecoder(request=request)Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 213–214why, one step back213 assert decoder.flush() == ""214Exception hierarchy (#1095)Tom Christiebefore adoption9409900
line 215why, one step back215 decoder = TextDecoder(request=request)Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 216–230why, one step back216 assert decoder.decode(b"") == ""217 assert decoder.flush() == ""218219220def test_line_decoder_nl():221 decoder = LineDecoder()222 assert decoder.decode("") == []223 assert decoder.decode("a\n\nb\nc") == ["a\n", "\n", "b\n"]224 assert decoder.flush() == ["c"]225226 decoder = LineDecoder()227 assert decoder.decode("") == []228 assert decoder.decode("a\n\nb\nc\n") == ["a\n", "\n", "b\n", "c\n"]229 assert decoder.flush() == []230Fixes #1033 - Ensure that text that spans 3 invocations before newline is handled - don't clobber the buffer between invocations that haven't seen any lines. (#1034)Sheridan C Rawlinsbefore adoptionad5e6eb
lines 231–238why, one step back231 # Issue #1033232 decoder = LineDecoder()233 assert decoder.decode("") == []234 assert decoder.decode("12345\n") == ["12345\n"]235 assert decoder.decode("foo ") == []236 assert decoder.decode("bar ") == []237 assert decoder.decode("baz\n") == ["foo bar baz\n"]238 assert decoder.flush() == []Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
line 239why, one step back239Fixes #1033 - Ensure that text that spans 3 invocations before newline is handled - don't clobber the buffer between invocations that haven't seen any lines. (#1034)Sheridan C Rawlinsbefore adoptionad5e6eb
line 240why, one step back240Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 241–251why, one step back241def test_line_decoder_cr():242 decoder = LineDecoder()243 assert decoder.decode("") == []244 assert decoder.decode("a\r\rb\rc") == ["a\n", "\n", "b\n"]245 assert decoder.flush() == ["c"]246247 decoder = LineDecoder()248 assert decoder.decode("") == []249 assert decoder.decode("a\r\rb\rc\r") == ["a\n", "\n", "b\n"]250 assert decoder.flush() == ["c\n"]251Fixes #1033 - Ensure that text that spans 3 invocations before newline is handled - don't clobber the buffer between invocations that haven't seen any lines. (#1034)Sheridan C Rawlinsbefore adoptionad5e6eb
lines 252–255why, one step back252 # Issue #1033253 decoder = LineDecoder()254 assert decoder.decode("") == []255 assert decoder.decode("12345\r") == []Fixes for LineDecoding (#1075)Tom Christiebefore adoption20f4911
line 256why, one step back256 assert decoder.decode("foo ") == ["12345\n"]Fixes #1033 - Ensure that text that spans 3 invocations before newline is handled - don't clobber the buffer between invocations that haven't seen any lines. (#1034)Sheridan C Rawlinsbefore adoptionad5e6eb
lines 257–258why, one step back257 assert decoder.decode("bar ") == []258 assert decoder.decode("baz\r") == []Fixes for LineDecoding (#1075)Tom Christiebefore adoption20f4911
line 259why, one step back259 assert decoder.flush() == ["foo bar baz\n"]Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
line 260why, one step back260Fixes #1033 - Ensure that text that spans 3 invocations before newline is handled - don't clobber the buffer between invocations that haven't seen any lines. (#1034)Sheridan C Rawlinsbefore adoptionad5e6eb
line 261why, one step back261Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 262–277why, one step back262def test_line_decoder_crnl():263 decoder = LineDecoder()264 assert decoder.decode("") == []265 assert decoder.decode("a\r\n\r\nb\r\nc") == ["a\n", "\n", "b\n"]266 assert decoder.flush() == ["c"]267268 decoder = LineDecoder()269 assert decoder.decode("") == []270 assert decoder.decode("a\r\n\r\nb\r\nc\r\n") == ["a\n", "\n", "b\n", "c\n"]271 assert decoder.flush() == []272273 decoder = LineDecoder()274 assert decoder.decode("") == []275 assert decoder.decode("a\r") == []276 assert decoder.decode("\n\r\nb\r\nc") == ["a\n", "\n", "b\n"]277 assert decoder.flush() == ["c"]Fixes #1033 - Ensure that text that spans 3 invocations before newline is handled - don't clobber the buffer between invocations that haven't seen any lines. (#1034)Sheridan C Rawlinsbefore adoptionad5e6eb
lines 278–286why, one step back278279 # Issue #1033280 decoder = LineDecoder()281 assert decoder.decode("") == []282 assert decoder.decode("12345\r\n") == ["12345\n"]283 assert decoder.decode("foo ") == []284 assert decoder.decode("bar ") == []285 assert decoder.decode("baz\r\n") == ["foo bar baz\n"]286 assert decoder.flush() == []Sync streaming interface on responses (#695)Tom Christiebefore adoption11e7604
lines 287–294why, one step back287288289def test_invalid_content_encoding_header():290 headers = [(b"Content-Encoding", b"invalid-header")]291 body = b"test 123"292293 response = httpx.Response(200, headers=headers, content=body, request=REQUEST)294 assert response.content == body