Skip to content
Myr

test_decoders.py

This file

Path

tests/test_decoders.py

264 lines · 7.9 KiB · Python

Last changed by

Switch to private module names (#785)Florimond Mancabefore adoption82dc6f3

History of this file

Take it

The walk stopped after 200 commits: lines older than c38fd68 point at it.

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 1–6why, one step back
1import zlib
23import brotli4import pytest56import httpx

Switch to private module names (#785)Florimond Mancabefore adoption82dc6f3

lines 7–8why, one step back
7from httpx._content_streams import AsyncIteratorStream
8from httpx._decoders import (

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 9–16why, one step back
9 BrotliDecoder,
10 DeflateDecoder,11 GZipDecoder,12 IdentityDecoder,13 LineDecoder,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")

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

line 18why, one step back
18

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

line 19why, one step back
19

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

line 20why, one step back
20def test_deflate():

Support both zlib and deflate encodings (#758)Tom Christiebefore adoption780d184

lines 21–25why, one step back
21 """
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 """

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 26–28why, one step back
26 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 back
29
30 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)

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 45–46why, one step back
45
46 headers = [(b"Content-Encoding", b"deflate")]

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

lines 47–49why, one step back
47 response = httpx.Response(
48 200, headers=headers, content=compressed_body, request=REQUEST49 )

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 50–58why, one step back
50 assert response.content == body
515253def 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")]

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

lines 59–61why, one step back
59 response = httpx.Response(
60 200, headers=headers, content=compressed_body, request=REQUEST61 )

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 62–69why, one step back
62 assert response.content == body
636465def test_brotli():66 body = b"test 123"67 compressed_body = brotli.compress(body)6869 headers = [(b"Content-Encoding", b"br")]

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

lines 70–72why, one step back
70 response = httpx.Response(
71 200, headers=headers, content=compressed_body, request=REQUEST72 )

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 73–87why, one step back
73 assert response.content == body
747576def 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")]

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

lines 88–90why, one step back
88 response = httpx.Response(
89 200, headers=headers, content=compressed_body, request=REQUEST90 )

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 91–98why, one step back
91 assert response.content == body
929394def test_multi_with_identity():95 body = b"test 123"96 compressed_body = brotli.compress(body)9798 headers = [(b"Content-Encoding", b"br, identity")]

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

lines 99–101why, one step back
99 response = httpx.Response(
100 200, headers=headers, content=compressed_body, request=REQUEST101 )

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 102–104why, one step back
102 assert response.content == body
103104 headers = [(b"Content-Encoding", b"identity, br")]

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

lines 105–107why, one step back
105 response = httpx.Response(
106 200, headers=headers, content=compressed_body, request=REQUEST107 )

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 108–120why, one step back
108 assert response.content == body
109110111@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")]

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

line 121why, one step back
121 stream = AsyncIteratorStream(aiterator=compress(body))

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

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

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

line 123why, one step back
123 assert not hasattr(response, "body")

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

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

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 125–129why, one step back
125
126127@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)]

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

line 130why, one step back
130 response = httpx.Response(200, headers=headers, content=b"", request=REQUEST)

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 131–147why, one step back
131 assert response.content == b""
132133134@pytest.mark.parametrize(135 "decoder", (BrotliDecoder, DeflateDecoder, GZipDecoder, IdentityDecoder)136)137def test_decoders_empty_cases(decoder):138 instance = decoder()139 assert instance.decode(b"") == b""140 assert instance.flush() == b""141142143@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))144def test_decoding_errors(header_value):145 headers = [(b"Content-Encoding", header_value)]146 body = b"test 123"147 compressed_body = brotli.compress(body)[3:]

Tighten up top-level API to only expose public API (#608)Tom Christiebefore adoptiond15dc0b

line 148why, one step back
148 with pytest.raises(httpx.DecodingError):

More coverage improvements (#711)Tom Christiebefore adoptionf5eaec7

line 149why, one step back
149 httpx.Response(200, headers=headers, content=compressed_body, request=REQUEST)

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 150–175why, one step back
150
151152@pytest.mark.parametrize(153 ["data", "encoding"],154 [155 ((b"Hello,", b" world!"), "ascii"),156 ((b"\xe3\x83", b"\x88\xe3\x83\xa9", b"\xe3", b"\x83\x99\xe3\x83\xab"), "utf-8"),157 ((b"\x83g\x83\x89\x83x\x83\x8b",) * 64, "shift-jis"),158 ((b"\x83g\x83\x89\x83x\x83\x8b",) * 600, "shift-jis"),159 (160 (b"\xcb\xee\xf0\xe5\xec \xe8\xef\xf1\xf3\xec \xe4\xee\xeb\xee\xf0",) * 64,161 "MacCyrillic",162 ),163 (164 (b"\xa5\xa6\xa5\xa7\xa5\xd6\xa4\xce\xb9\xf1\xba\xdd\xb2\xbd",) * 512,165 "euc-jp",166 ),167 ],168)169@pytest.mark.asyncio170async def test_text_decoder(data, encoding):171 async def iterator():172 nonlocal data173 for chunk in data:174 yield chunk175

Bump coverage (#705)Tom Christiebefore adoptionb8394f1

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

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

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

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

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

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

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

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

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

Bump coverage (#705)Tom Christiebefore adoptionb8394f1

lines 181–186why, one step back
181
182 # Streaming `.aiter_text` iteratively.183 stream = AsyncIteratorStream(aiterator=iterator())184 response = httpx.Response(200, stream=stream, request=REQUEST)185 text = "".join([part async for part in response.aiter_text()])186 assert text == (b"".join(data)).decode(encoding)

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 187–195why, one step back
187
188189@pytest.mark.asyncio190async def test_text_decoder_known_encoding():191 async def iterator():192 yield b"\x83g"193 yield b"\x83"194 yield b"\x89\x83x\x83\x8b"195

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

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

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

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

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

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

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

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

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 202–203why, one step back
202 )
203

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

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

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

lines 205–262why, one step back
205 assert "".join(response.text) == "トラベル"
206207208def test_text_decoder_empty_cases():209 decoder = TextDecoder()210 assert decoder.flush() == ""211212 decoder = TextDecoder()213 assert decoder.decode(b"") == ""214 assert decoder.flush() == ""215216217def test_line_decoder_nl():218 decoder = LineDecoder()219 assert decoder.decode("") == []220 assert decoder.decode("a\n\nb\nc") == ["a\n", "\n", "b\n"]221 assert decoder.flush() == ["c"]222223 decoder = LineDecoder()224 assert decoder.decode("") == []225 assert decoder.decode("a\n\nb\nc\n") == ["a\n", "\n", "b\n", "c\n"]226 assert decoder.flush() == []227228229def test_line_decoder_cr():230 decoder = LineDecoder()231 assert decoder.decode("") == []232 assert decoder.decode("a\r\rb\rc") == ["a\n", "\n", "b\n"]233 assert decoder.flush() == ["c"]234235 decoder = LineDecoder()236 assert decoder.decode("") == []237 assert decoder.decode("a\r\rb\rc\r") == ["a\n", "\n", "b\n"]238 assert decoder.flush() == ["c\n"]239240241def test_line_decoder_crnl():242 decoder = LineDecoder()243 assert decoder.decode("") == []244 assert decoder.decode("a\r\n\r\nb\r\nc") == ["a\n", "\n", "b\n"]245 assert decoder.flush() == ["c"]246247 decoder = LineDecoder()248 assert decoder.decode("") == []249 assert decoder.decode("a\r\n\r\nb\r\nc\r\n") == ["a\n", "\n", "b\n", "c\n"]250 assert decoder.flush() == []251252 decoder = LineDecoder()253 assert decoder.decode("") == []254 assert decoder.decode("a\r") == []255 assert decoder.decode("\n\r\nb\r\nc") == ["a\n", "\n", "b\n"]256 assert decoder.flush() == ["c"]257258259def test_invalid_content_encoding_header():260 headers = [(b"Content-Encoding", b"invalid-header")]261 body = b"test 123"262

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

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

Drop BackgroundManager in favor of fork(func1, func2) (#603)Florimond Mancabefore adoptionc38fd68

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