Skip to content
Myr

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 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):

More coverage improvements (#711)Tom Christiebefore adoptionf5eaec7

line 128why, one step back
128 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 back
129
130131@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 back
148@pytest.mark.asyncio
149async 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 back
151 nonlocal data
152 for chunk in data:153 yield chunk154

Bump coverage (#705)Tom Christiebefore adoptionb8394f1

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

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

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

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

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

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Bump coverage (#705)Tom Christiebefore adoptionb8394f1

lines 160–165why, one step back
160
161 # 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 back
166
167

Drop sync (#544)Tom Christiebefore adoption206c537

lines 168–170why, one step back
168@pytest.mark.asyncio
169async 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 back
171 yield b"\x83g"
172 yield b"\x83"173 yield b"\x89\x83x\x83\x8b"174

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

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

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

lines 176–178why, one step back
176 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 back
179 stream=stream,

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

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

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

lines 181–182why, one step back
181 )
182

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

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

lines 185–193why, one step back
185
186187def 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 back
194
195196def 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 back
236
237238def test_invalid_content_encoding_header():239 headers = [(b"Content-Encoding", b"invalid-header")]240 body = b"test 123"241

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

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

Drop sync (#544)Tom Christiebefore adoption206c537

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