Skip to content
Myr

test_decoders.py

This file

Path

tests/test_decoders.py

312 lines · 8.5 KiB · Python

Last changed by

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

History of this file

Take it

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

Rename pool limit options (#968)Tom Christiebefore adoption991915a

lines 1–28why, one step back
1import zlib
23import brotli4import pytest56import httpx7from httpx._decoders import (8 BrotliDecoder,9 DeflateDecoder,10 GZipDecoder,11 IdentityDecoder,12 LineDecoder,13 TextDecoder,14)151617def test_deflate():18 """19 Deflate encoding may use either 'zlib' or 'deflate' in the wild.2021 https://stackoverflow.com/questions/1838699/how-can-i-decompress-a-gzip-stream-with-zlib#answer-2231129722 """23 body = b"test 123"24 compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)25 compressed_body = compressor.compress(body) + compressor.flush()2627 headers = [(b"Content-Encoding", b"deflate")]28 response = httpx.Response(

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 29–31why, one step back
29 200,
30 headers=headers,31 content=compressed_body,

Rename pool limit options (#968)Tom Christiebefore adoption991915a

lines 32–46why, one step back
32 )
33 assert response.content == body343536def test_zlib():37 """38 Deflate encoding may use either 'zlib' or 'deflate' in the wild.3940 https://stackoverflow.com/questions/1838699/how-can-i-decompress-a-gzip-stream-with-zlib#answer-2231129741 """42 body = b"test 123"43 compressed_body = zlib.compress(body)4445 headers = [(b"Content-Encoding", b"deflate")]46 response = httpx.Response(

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 47–49why, one step back
47 200,
48 headers=headers,49 content=compressed_body,

Rename pool limit options (#968)Tom Christiebefore adoption991915a

lines 50–60why, one step back
50 )
51 assert response.content == body525354def test_gzip():55 body = b"test 123"56 compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)57 compressed_body = compressor.compress(body) + compressor.flush()5859 headers = [(b"Content-Encoding", b"gzip")]60 response = httpx.Response(

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 61–63why, one step back
61 200,
62 headers=headers,63 content=compressed_body,

Rename pool limit options (#968)Tom Christiebefore adoption991915a

lines 64–73why, one step back
64 )
65 assert response.content == body666768def test_brotli():69 body = b"test 123"70 compressed_body = brotli.compress(body)7172 headers = [(b"Content-Encoding", b"br")]73 response = httpx.Response(

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 74–76why, one step back
74 200,
75 headers=headers,76 content=compressed_body,

Rename pool limit options (#968)Tom Christiebefore adoption991915a

lines 77–93why, one step back
77 )
78 assert response.content == body798081def test_multi():82 body = b"test 123"8384 deflate_compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)85 compressed_body = deflate_compressor.compress(body) + deflate_compressor.flush()8687 gzip_compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)88 compressed_body = (89 gzip_compressor.compress(compressed_body) + gzip_compressor.flush()90 )9192 headers = [(b"Content-Encoding", b"deflate, gzip")]93 response = httpx.Response(

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 94–96why, one step back
94 200,
95 headers=headers,96 content=compressed_body,

Rename pool limit options (#968)Tom Christiebefore adoption991915a

lines 97–106why, one step back
97 )
98 assert response.content == body99100101def test_multi_with_identity():102 body = b"test 123"103 compressed_body = brotli.compress(body)104105 headers = [(b"Content-Encoding", b"br, identity")]106 response = httpx.Response(

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 107–109why, one step back
107 200,
108 headers=headers,109 content=compressed_body,

Rename pool limit options (#968)Tom Christiebefore adoption991915a

lines 110–114why, one step back
110 )
111 assert response.content == body112113 headers = [(b"Content-Encoding", b"identity, br")]114 response = httpx.Response(

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 115–117why, one step back
115 200,
116 headers=headers,117 content=compressed_body,

Rename pool limit options (#968)Tom Christiebefore adoption991915a

lines 118–131why, one step back
118 )
119 assert response.content == body120121122@pytest.mark.asyncio123async def test_streaming():124 body = b"test 123"125 compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)126127 async def compress(body):128 yield compressor.compress(body)129 yield compressor.flush()130131 headers = [(b"Content-Encoding", b"gzip")]

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 132–134why, one step back
132 response = httpx.Response(
133 200,134 headers=headers,

Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135

line 135why, one step back
135 content=compress(body),

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

line 136why, one step back
136 )

Rename pool limit options (#968)Tom Christiebefore adoption991915a

lines 137–143why, one step back
137 assert not hasattr(response, "body")
138 assert await response.aread() == body139140141@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br", b"identity"))142def test_empty_content(header_value):143 headers = [(b"Content-Encoding", header_value)]

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 144–148why, one step back
144 response = httpx.Response(
145 200,146 headers=headers,147 content=b"",148 )

Rename pool limit options (#968)Tom Christiebefore adoption991915a

lines 149–155why, one step back
149 assert response.content == b""
150151152@pytest.mark.parametrize(153 "decoder", (BrotliDecoder, DeflateDecoder, GZipDecoder, IdentityDecoder)154)155def test_decoders_empty_cases(decoder):

Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528

lines 156–158why, one step back
156 response = httpx.Response(content=b"", status_code=200)
157 instance = decoder()158 assert instance.decode(response.content) == b""

Rename pool limit options (#968)Tom Christiebefore adoption991915a

lines 159–167why, one step back
159 assert instance.flush() == b""
160161162@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))163def test_decoding_errors(header_value):164 headers = [(b"Content-Encoding", header_value)]165 body = b"test 123"166 compressed_body = brotli.compress(body)[3:]167 with pytest.raises(httpx.DecodingError):

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 168–169why, one step back
168 request = httpx.Request("GET", "https://example.org")
169 httpx.Response(200, headers=headers, content=compressed_body, request=request)

Rename pool limit options (#968)Tom Christiebefore adoption991915a

line 170why, one step back
170

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 171–172why, one step back
171 with pytest.raises(ValueError):
172 httpx.Response(200, headers=headers, content=compressed_body)

Rename pool limit options (#968)Tom Christiebefore adoption991915a

line 173why, one step back
173

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

line 174why, one step back
174

Rename pool limit options (#968)Tom Christiebefore adoption991915a

lines 175–179why, one step back
175@pytest.mark.parametrize(
176 ["data", "encoding"],177 [178 ((b"Hello,", b" world!"), "ascii"),179 ((b"\xe3\x83", b"\x88\xe3\x83\xa9", b"\xe3", b"\x83\x99\xe3\x83\xab"), "utf-8"),

Drop chardet (#1269)Tom Christiebefore adoptiond0fe113

lines 180–181why, one step back
180 ((b"Euro character: \x88!", b""), "cp1252"),
181 ((b"Accented: \xd6sterreich", b""), "iso-8859-1"),

Rename pool limit options (#968)Tom Christiebefore adoption991915a

lines 182–191why, one step back
182 ],
183)184@pytest.mark.asyncio185async def test_text_decoder(data, encoding):186 async def iterator():187 nonlocal data188 for chunk in data:189 yield chunk190191 # Accessing `.text` on a read response.

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 192–193why, one step back
192 response = httpx.Response(
193 200,

Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135

line 194why, one step back
194 content=iterator(),

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

line 195why, one step back
195 )

Rename pool limit options (#968)Tom Christiebefore adoption991915a

lines 196–199why, one step back
196 await response.aread()
197 assert response.text == (b"".join(data)).decode(encoding)198199 # Streaming `.aiter_text` iteratively.

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 200–201why, one step back
200 response = httpx.Response(
201 200,

Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135

line 202why, one step back
202 content=iterator(),

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

line 203why, one step back
203 )

Rename pool limit options (#968)Tom Christiebefore adoption991915a

lines 204–217why, one step back
204 text = "".join([part async for part in response.aiter_text()])
205 assert text == (b"".join(data)).decode(encoding)206207208@pytest.mark.asyncio209async def test_text_decoder_known_encoding():210 async def iterator():211 yield b"\x83g"212 yield b"\x83"213 yield b"\x89\x83x\x83\x8b"214215 response = httpx.Response(216 200,217 headers=[(b"Content-Type", b"text/html; charset=shift-jis")],

Support `Response(content=<bytes iterator>)` (#1265)Tom Christiebefore adoption5ee6135

line 218why, one step back
218 content=iterator(),

Rename pool limit options (#968)Tom Christiebefore adoption991915a

lines 219–225why, one step back
219 )
220221 await response.aread()222 assert "".join(response.text) == "トラベル"223224225def test_text_decoder_empty_cases():

Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528

line 226why, one step back
226 decoder = TextDecoder()

Rename pool limit options (#968)Tom Christiebefore adoption991915a

lines 227–228why, one step back
227 assert decoder.flush() == ""
228

Make the response's request parameter optional (#1238)tbascoulbefore adoptione0b4528

line 229why, one step back
229 decoder = TextDecoder()

Rename pool limit options (#968)Tom Christiebefore adoption991915a

lines 230–244why, one step back
230 assert decoder.decode(b"") == ""
231 assert decoder.flush() == ""232233234def test_line_decoder_nl():235 decoder = LineDecoder()236 assert decoder.decode("") == []237 assert decoder.decode("a\n\nb\nc") == ["a\n", "\n", "b\n"]238 assert decoder.flush() == ["c"]239240 decoder = LineDecoder()241 assert decoder.decode("") == []242 assert decoder.decode("a\n\nb\nc\n") == ["a\n", "\n", "b\n", "c\n"]243 assert decoder.flush() == []244

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 245–252why, one step back
245 # Issue #1033
246 decoder = LineDecoder()247 assert decoder.decode("") == []248 assert decoder.decode("12345\n") == ["12345\n"]249 assert decoder.decode("foo ") == []250 assert decoder.decode("bar ") == []251 assert decoder.decode("baz\n") == ["foo bar baz\n"]252 assert decoder.flush() == []

Rename pool limit options (#968)Tom Christiebefore adoption991915a

line 253why, one step back
253

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

line 254why, one step back
254

Rename pool limit options (#968)Tom Christiebefore adoption991915a

lines 255–265why, one step back
255def test_line_decoder_cr():
256 decoder = LineDecoder()257 assert decoder.decode("") == []258 assert decoder.decode("a\r\rb\rc") == ["a\n", "\n", "b\n"]259 assert decoder.flush() == ["c"]260261 decoder = LineDecoder()262 assert decoder.decode("") == []263 assert decoder.decode("a\r\rb\rc\r") == ["a\n", "\n", "b\n"]264 assert decoder.flush() == ["c\n"]265

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 266–269why, one step back
266 # Issue #1033
267 decoder = LineDecoder()268 assert decoder.decode("") == []269 assert decoder.decode("12345\r") == []

Fixes for LineDecoding (#1075)Tom Christiebefore adoption20f4911

line 270why, one step back
270 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 271–272why, one step back
271 assert decoder.decode("bar ") == []
272 assert decoder.decode("baz\r") == []

Fixes for LineDecoding (#1075)Tom Christiebefore adoption20f4911

line 273why, one step back
273 assert decoder.flush() == ["foo bar baz\n"]

Rename pool limit options (#968)Tom Christiebefore adoption991915a

line 274why, one step back
274

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

line 275why, one step back
275

Rename pool limit options (#968)Tom Christiebefore adoption991915a

lines 276–291why, one step back
276def test_line_decoder_crnl():
277 decoder = LineDecoder()278 assert decoder.decode("") == []279 assert decoder.decode("a\r\n\r\nb\r\nc") == ["a\n", "\n", "b\n"]280 assert decoder.flush() == ["c"]281282 decoder = LineDecoder()283 assert decoder.decode("") == []284 assert decoder.decode("a\r\n\r\nb\r\nc\r\n") == ["a\n", "\n", "b\n", "c\n"]285 assert decoder.flush() == []286287 decoder = LineDecoder()288 assert decoder.decode("") == []289 assert decoder.decode("a\r") == []290 assert decoder.decode("\n\r\nb\r\nc") == ["a\n", "\n", "b\n"]291 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 292–300why, one step back
292
293 # Issue #1033294 decoder = LineDecoder()295 assert decoder.decode("") == []296 assert decoder.decode("12345\r\n") == ["12345\n"]297 assert decoder.decode("foo ") == []298 assert decoder.decode("bar ") == []299 assert decoder.decode("baz\r\n") == ["foo bar baz\n"]300 assert decoder.flush() == []

Rename pool limit options (#968)Tom Christiebefore adoption991915a

lines 301–306why, one step back
301
302303def test_invalid_content_encoding_header():304 headers = [(b"Content-Encoding", b"invalid-header")]305 body = b"test 123"306

Drop Response(..., request=...) style in test cases. (#1243)Tom Christiebefore adoptionde502a4

lines 307–311why, one step back
307 response = httpx.Response(
308 200,309 headers=headers,310 content=body,311 )

Rename pool limit options (#968)Tom Christiebefore adoption991915a

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