Skip to content
Myr

test_decoders.py

This file

Path

tests/test_decoders.py

246 lines · 7.3 KiB · Python

Last changed by

Bump coverage (#705)Tom Christiebefore adoptionb8394f1

History of this file

Take 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 back
1import zlib
23import brotli4import pytest56import httpx

Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc

line 7why, one step back
7from httpx.content_streams import AsyncIteratorStream

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 8–12why, one step back
8from httpx.decoders import (
9 BrotliDecoder,10 DeflateDecoder,11 GZipDecoder,12 IdentityDecoder,

Add Response.stream_lines (#575)Tom Christiebefore adoption248aa58

line 13why, one step back
13 LineDecoder,

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 14–16why, one step back
14 TextDecoder,
15)16

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 17why, one step back
17REQUEST = httpx.Request("GET", "https://example.org")

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

line 18why, one step back
18

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 19why, one step back
19

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 20–25why, one step back
20def 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 back
26 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 back
29 assert response.content == body
303132def 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 back
38 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 back
41 assert response.content == body
424344def 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 back
49 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 back
52 assert response.content == body
535455def 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 back
67 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 back
70 assert response.content == body
717273def 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 back
78 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 back
81 assert response.content == body
8283 headers = [(b"Content-Encoding", b"identity, br")]

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

lines 84–86why, one step back
84 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 back
87 assert response.content == body
8889

Drop sync (#544)Tom Christiebefore adoption206c537

lines 90–91why, one step back
90@pytest.mark.asyncio
91async def test_streaming():

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 92–94why, one step back
92 body = b"test 123"
93 compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)94

Drop sync (#544)Tom Christiebefore adoption206c537

line 95why, one step back
95 async def compress(body):

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 96–99why, one step back
96 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 back
100 stream = AsyncIteratorStream(aiterator=compress(body))

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 101why, one step back
101 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 back
102 assert not hasattr(response, "body")

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 103why, one step back
103 assert await response.aread() == body

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 104–108why, one step back
104
105106@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 back
109 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 back
110 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 back
127 with pytest.raises(httpx.DecodingError):

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

lines 128–130why, one step back
128 response = httpx.Response(
129 200, headers=headers, content=compressed_body, request=REQUEST130 )

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 131–150why, one step back
131 response.content
132133134@pytest.mark.parametrize(135 ["data", "encoding"],136 [137 ((b"Hello,", b" world!"), "ascii"),138 ((b"\xe3\x83", b"\x88\xe3\x83\xa9", b"\xe3", b"\x83\x99\xe3\x83\xab"), "utf-8"),139 ((b"\x83g\x83\x89\x83x\x83\x8b",) * 64, "shift-jis"),140 ((b"\x83g\x83\x89\x83x\x83\x8b",) * 600, "shift-jis"),141 (142 (b"\xcb\xee\xf0\xe5\xec \xe8\xef\xf1\xf3\xec \xe4\xee\xeb\xee\xf0",) * 64,143 "MacCyrillic",144 ),145 (146 (b"\xa5\xa6\xa5\xa7\xa5\xd6\xa4\xce\xb9\xf1\xba\xdd\xb2\xbd",) * 512,147 "euc-jp",148 ),149 ],150)

Drop sync (#544)Tom Christiebefore adoption206c537

lines 151–153why, one step back
151@pytest.mark.asyncio
152async def test_text_decoder(data, encoding):153 async def iterator():

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 154–157why, one step back
154 nonlocal data
155 for chunk in data:156 yield chunk157

Bump coverage (#705)Tom Christiebefore adoptionb8394f1

line 158why, one step back
158 # Accessing `.text` on a read response.

Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc

line 159why, one step back
159 stream = AsyncIteratorStream(aiterator=iterator())

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 160why, one step back
160 response = httpx.Response(200, stream=stream, request=REQUEST)

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 161why, one step back
161 await response.aread()

Drop sync (#544)Tom Christiebefore adoption206c537

line 162why, one step back
162 assert response.text == (b"".join(data)).decode(encoding)

Bump coverage (#705)Tom Christiebefore adoptionb8394f1

lines 163–168why, one step back
163
164 # Streaming `.aiter_text` iteratively.165 stream = AsyncIteratorStream(aiterator=iterator())166 response = httpx.Response(200, stream=stream, request=REQUEST)167 text = "".join([part async for part in response.aiter_text()])168 assert text == (b"".join(data)).decode(encoding)

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 169–170why, one step back
169
170

Drop sync (#544)Tom Christiebefore adoption206c537

lines 171–173why, one step back
171@pytest.mark.asyncio
172async def test_text_decoder_known_encoding():173 async def iterator():

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 174–177why, one step back
174 yield b"\x83g"
175 yield b"\x83"176 yield b"\x89\x83x\x83\x8b"177

Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc

line 178why, one step back
178 stream = AsyncIteratorStream(aiterator=iterator())

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 179–181why, one step back
179 response = httpx.Response(
180 200,181 headers=[(b"Content-Type", b"text/html; charset=shift-jis")],

Use Streams API for both requests and responses. (#648)Tom Christiebefore adoptioncee1fcc

line 182why, one step back
182 stream=stream,

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 183why, one step back
183 request=REQUEST,

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 184–185why, one step back
184 )
185

Rename 'read/close' to 'aread/aclose' on Response (#674)Florimond Mancabefore adoptionf9d18a8

line 186why, one step back
186 await response.aread()

Drop sync (#544)Tom Christiebefore adoption206c537

line 187why, one step back
187 assert "".join(response.text) == "トラベル"

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 188–196why, one step back
188
189190def test_text_decoder_empty_cases():191 decoder = TextDecoder()192 assert decoder.flush() == ""193194 decoder = TextDecoder()195 assert decoder.decode(b"") == ""196 assert decoder.flush() == ""

Add Response.stream_lines (#575)Tom Christiebefore adoption248aa58

lines 197–238why, one step back
197
198199def test_line_decoder_nl():200 decoder = LineDecoder()201 assert decoder.decode("") == []202 assert decoder.decode("a\n\nb\nc") == ["a\n", "\n", "b\n"]203 assert decoder.flush() == ["c"]204205 decoder = LineDecoder()206 assert decoder.decode("") == []207 assert decoder.decode("a\n\nb\nc\n") == ["a\n", "\n", "b\n", "c\n"]208 assert decoder.flush() == []209210211def test_line_decoder_cr():212 decoder = LineDecoder()213 assert decoder.decode("") == []214 assert decoder.decode("a\r\rb\rc") == ["a\n", "\n", "b\n"]215 assert decoder.flush() == ["c"]216217 decoder = LineDecoder()218 assert decoder.decode("") == []219 assert decoder.decode("a\r\rb\rc\r") == ["a\n", "\n", "b\n"]220 assert decoder.flush() == ["c\n"]221222223def test_line_decoder_crnl():224 decoder = LineDecoder()225 assert decoder.decode("") == []226 assert decoder.decode("a\r\n\r\nb\r\nc") == ["a\n", "\n", "b\n"]227 assert decoder.flush() == ["c"]228229 decoder = LineDecoder()230 assert decoder.decode("") == []231 assert decoder.decode("a\r\n\r\nb\r\nc\r\n") == ["a\n", "\n", "b\n", "c\n"]232 assert decoder.flush() == []233234 decoder = LineDecoder()235 assert decoder.decode("") == []236 assert decoder.decode("a\r") == []237 assert decoder.decode("\n\r\nb\r\nc") == ["a\n", "\n", "b\n"]238 assert decoder.flush() == ["c"]

Document + recommend async with syntax for AsyncClient (#344)Florimond Mancabefore adoption4616031

lines 239–244why, one step back
239
240241def test_invalid_content_encoding_header():242 headers = [(b"Content-Encoding", b"invalid-header")]243 body = b"test 123"244

Make 'request' non-optional on responses (#666)Florimond Mancabefore adoption56c8eda

line 245why, one step back
245 response = httpx.Response(200, headers=headers, content=body, request=REQUEST)

Drop sync (#544)Tom Christiebefore adoption206c537

line 246why, one step back
246 assert response.content == body