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
The walk stopped after 200 commits: lines older than 4616031 point at it.
Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031
lines 1–6why, one step back1import zlib23import brotli4import pytest56import httpxUse Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
line 7why, one step back7from httpx.content_streams import AsyncIteratorStreamDocument + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031
lines 8–12why, one step back8from httpx.decoders import (9 BrotliDecoder,10 DeflateDecoder,11 GZipDecoder,12 IdentityDecoder,Add Response.stream_lines (#575)Tom Christiebefore adoption248aa58
line 13why, one step back13 LineDecoder,Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031
lines 14–16why, one step back14 TextDecoder,15)16Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 17why, one step back17REQUEST = httpx.Request("GET", "https://example.org")Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031
line 18why, one step back18Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 19why, one step back19Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031
lines 20–25why, one step back20def 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")]Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
lines 26–28why, one step back26 response = httpx.Response(27 200, headers=headers, content=compressed_body, request=REQUEST28 )Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031
lines 29–37why, one step back29 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")]Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
lines 38–40why, one step back38 response = httpx.Response(39 200, headers=headers, content=compressed_body, request=REQUEST40 )Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031
lines 41–48why, one step back41 assert response.content == body424344def test_brotli():45 body = b"test 123"46 compressed_body = brotli.compress(body)4748 headers = [(b"Content-Encoding", b"br")]Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
lines 49–51why, one step back49 response = httpx.Response(50 200, headers=headers, content=compressed_body, request=REQUEST51 )Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031
lines 52–66why, one step back52 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")]Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
lines 67–69why, one step back67 response = httpx.Response(68 200, headers=headers, content=compressed_body, request=REQUEST69 )Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031
lines 70–77why, one step back70 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")]Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
lines 78–80why, one step back78 response = httpx.Response(79 200, headers=headers, content=compressed_body, request=REQUEST80 )Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031
lines 81–83why, one step back81 assert response.content == body8283 headers = [(b"Content-Encoding", b"identity, br")]Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
lines 84–86why, one step back84 response = httpx.Response(85 200, headers=headers, content=compressed_body, request=REQUEST86 )Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031
lines 87–89why, one step back87 assert response.content == body8889Drop sync (#544)Tom Christiebefore adoption206c537
lines 90–91why, one step back90@pytest.mark.asyncio91async def test_streaming():Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031
lines 92–94why, one step back92 body = b"test 123"93 compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)94Drop sync (#544)Tom Christiebefore adoption206c537
line 95why, one step back95 async def compress(body):Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031
lines 96–99why, one step back96 yield compressor.compress(body)97 yield compressor.flush()9899 headers = [(b"Content-Encoding", b"gzip")]Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
line 100why, one step back100 stream = AsyncIteratorStream(aiterator=compress(body))Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 101why, one step back101 response = httpx.Response(200, headers=headers, stream=stream, request=REQUEST)Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031
line 102why, one step back102 assert not hasattr(response, "body")Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8
line 103why, one step back103 assert await response.aread() == bodyDocument + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031
lines 104–108why, one step back104105106@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)]Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 109why, one step back109 response = httpx.Response(200, headers=headers, content=b"", request=REQUEST)Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031
lines 110–126why, one step back110 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:]Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b
line 127why, one step back127 with pytest.raises(httpx.DecodingError):More coverage improvements (#711)Tom Christiebefore adoptionf5eaec7
line 128why, one step back128 httpx.Response(200, headers=headers, content=compressed_body, request=REQUEST)Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031
lines 129–147why, one step back129130131@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)Drop sync (#544)Tom Christiebefore adoption206c537
lines 148–150why, one step back148@pytest.mark.asyncio149async def test_text_decoder(data, encoding):150 async def iterator():Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031
lines 151–154why, one step back151 nonlocal data152 for chunk in data:153 yield chunk154Bump coverage (#705)Tom Christiebefore adoptionb8394f1
line 155why, one step back155 # Accessing `.text` on a read response.Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
line 156why, one step back156 stream = AsyncIteratorStream(aiterator=iterator())Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 157why, one step back157 response = httpx.Response(200, stream=stream, request=REQUEST)Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8
line 158why, one step back158 await response.aread()Drop sync (#544)Tom Christiebefore adoption206c537
line 159why, one step back159 assert response.text == (b"".join(data)).decode(encoding)Bump coverage (#705)Tom Christiebefore adoptionb8394f1
lines 160–165why, one step back160161 # 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)Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031
lines 166–167why, one step back166167Drop sync (#544)Tom Christiebefore adoption206c537
lines 168–170why, one step back168@pytest.mark.asyncio169async def test_text_decoder_known_encoding():170 async def iterator():Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031
lines 171–174why, one step back171 yield b"\x83g"172 yield b"\x83"173 yield b"\x89\x83x\x83\x8b"174Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
line 175why, one step back175 stream = AsyncIteratorStream(aiterator=iterator())Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031
lines 176–178why, one step back176 response = httpx.Response(177 200,178 headers=[(b"Content-Type", b"text/html; charset=shift-jis")],Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc
line 179why, one step back179 stream=stream,Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 180why, one step back180 request=REQUEST,Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031
lines 181–182why, one step back181 )182Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8
line 183why, one step back183 await response.aread()Drop sync (#544)Tom Christiebefore adoption206c537
line 184why, one step back184 assert "".join(response.text) == "トラベル"Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031
lines 185–193why, one step back185186187def test_text_decoder_empty_cases():188 decoder = TextDecoder()189 assert decoder.flush() == ""190191 decoder = TextDecoder()192 assert decoder.decode(b"") == ""193 assert decoder.flush() == ""Add Response.stream_lines (#575)Tom Christiebefore adoption248aa58
lines 194–235why, one step back194195196def 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"]Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031
lines 236–241why, one step back236237238def test_invalid_content_encoding_header():239 headers = [(b"Content-Encoding", b"invalid-header")]240 body = b"test 123"241Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda
line 242why, one step back242 response = httpx.Response(200, headers=headers, content=body, request=REQUEST)Drop sync (#544)Tom Christiebefore adoption206c537
line 243why, one step back243 assert response.content == body