Skip to content
Myr

test_decoders.py

This file

Path

tests/test_decoders.py

358 lines · 9.9 KiB · Python

Last changed by

Transport API (#1522)Tom Christiebefore adoption1a6e254

History of this file

Take it

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

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 1–8why, one step back
1import zlib
23import brotli4import pytest56import httpx7from httpx._decoders import (8 BrotliDecoder,

Support for `chunk_size` (#1277)Tom Christiebefore adoption27df5e4

line 9why, one step back
9 ByteChunker,

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 10–13why, one step back
10 DeflateDecoder,
11 GZipDecoder,12 IdentityDecoder,13 LineDecoder,

Support for `chunk_size` (#1277)Tom Christiebefore adoption27df5e4

line 14why, one step back
14 TextChunker,

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 15–30why, one step back
15 TextDecoder,
16)171819def test_deflate():20 """21 Deflate encoding may use either 'zlib' or 'deflate' in the wild.2223 https://stackoverflow.com/questions/1838699/how-can-i-decompress-a-gzip-stream-with-zlib#answer-2231129724 """25 body = b"test 123"26 compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)27 compressed_body = compressor.compress(body) + compressor.flush()2829 headers = [(b"Content-Encoding", b"deflate")]30 response = httpx.Response(

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

lines 31–33why, one step back
31 200,
32 headers=headers,33 content=compressed_body,

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 34–48why, one step back
34 )
35 assert response.content == body363738def test_zlib():39 """40 Deflate encoding may use either 'zlib' or 'deflate' in the wild.4142 https://stackoverflow.com/questions/1838699/how-can-i-decompress-a-gzip-stream-with-zlib#answer-2231129743 """44 body = b"test 123"45 compressed_body = zlib.compress(body)4647 headers = [(b"Content-Encoding", b"deflate")]48 response = httpx.Response(

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

lines 49–51why, one step back
49 200,
50 headers=headers,51 content=compressed_body,

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 52–62why, one step back
52 )
53 assert response.content == body545556def test_gzip():57 body = b"test 123"58 compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)59 compressed_body = compressor.compress(body) + compressor.flush()6061 headers = [(b"Content-Encoding", b"gzip")]62 response = httpx.Response(

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

lines 63–65why, one step back
63 200,
64 headers=headers,65 content=compressed_body,

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 66–75why, one step back
66 )
67 assert response.content == body686970def test_brotli():71 body = b"test 123"72 compressed_body = brotli.compress(body)7374 headers = [(b"Content-Encoding", b"br")]75 response = httpx.Response(

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

lines 76–78why, one step back
76 200,
77 headers=headers,78 content=compressed_body,

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 79–95why, one step back
79 )
80 assert response.content == body818283def test_multi():84 body = b"test 123"8586 deflate_compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)87 compressed_body = deflate_compressor.compress(body) + deflate_compressor.flush()8889 gzip_compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)90 compressed_body = (91 gzip_compressor.compress(compressed_body) + gzip_compressor.flush()92 )9394 headers = [(b"Content-Encoding", b"deflate, gzip")]95 response = httpx.Response(

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

lines 96–98why, one step back
96 200,
97 headers=headers,98 content=compressed_body,

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 99–108why, one step back
99 )
100 assert response.content == body101102103def test_multi_with_identity():104 body = b"test 123"105 compressed_body = brotli.compress(body)106107 headers = [(b"Content-Encoding", b"br, identity")]108 response = httpx.Response(

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

lines 109–111why, one step back
109 200,
110 headers=headers,111 content=compressed_body,

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 112–116why, one step back
112 )
113 assert response.content == body114115 headers = [(b"Content-Encoding", b"identity, br")]116 response = httpx.Response(

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

lines 117–119why, one step back
117 200,
118 headers=headers,119 content=compressed_body,

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 120–133why, one step back
120 )
121 assert response.content == body122123124@pytest.mark.asyncio125async def test_streaming():126 body = b"test 123"127 compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)128129 async def compress(body):130 yield compressor.compress(body)131 yield compressor.flush()132133 headers = [(b"Content-Encoding", b"gzip")]

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

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

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

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

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

line 138why, one step back
138 )

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 139–145why, one step back
139 assert not hasattr(response, "body")
140 assert await response.aread() == body141142143@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br", b"identity"))144def test_empty_content(header_value):145 headers = [(b"Content-Encoding", header_value)]

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

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

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 151–157why, one step back
151 assert response.content == b""
152153154@pytest.mark.parametrize(155 "decoder", (BrotliDecoder, DeflateDecoder, GZipDecoder, IdentityDecoder)156)157def test_decoders_empty_cases(decoder):

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

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

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

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

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

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

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

line 172why, one step back
172

Transport API (#1522)Tom Christiebefore adoption1a6e254

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

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

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

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

line 175why, one step back
175

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

line 176why, one step back
176

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 177–181why, one step back
177@pytest.mark.parametrize(
178 ["data", "encoding"],179 [180 ((b"Hello,", b" world!"), "ascii"),181 ((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 182–183why, one step back
182 ((b"Euro character: \x88!", b""), "cp1252"),
183 ((b"Accented: \xd6sterreich", b""), "iso-8859-1"),

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 184–193why, one step back
184 ],
185)186@pytest.mark.asyncio187async def test_text_decoder(data, encoding):188 async def iterator():189 nonlocal data190 for chunk in data:191 yield chunk192193 # Accessing `.text` on a read response.

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

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

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

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

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

line 197why, one step back
197 )

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

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

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

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

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

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

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

line 205why, one step back
205 )

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 206–219why, one step back
206 text = "".join([part async for part in response.aiter_text()])
207 assert text == (b"".join(data)).decode(encoding)208209210@pytest.mark.asyncio211async def test_text_decoder_known_encoding():212 async def iterator():213 yield b"\x83g"214 yield b"\x83"215 yield b"\x89\x83x\x83\x8b"216217 response = httpx.Response(218 200,219 headers=[(b"Content-Type", b"text/html; charset=shift-jis")],

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

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

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 221–227why, one step back
221 )
222223 await response.aread()224 assert "".join(response.text) == "トラベル"225226227def test_text_decoder_empty_cases():

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

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

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 229–230why, one step back
229 assert decoder.flush() == ""
230

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

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

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 232–246why, one step back
232 assert decoder.decode(b"") == ""
233 assert decoder.flush() == ""234235236def test_line_decoder_nl():237 decoder = LineDecoder()238 assert decoder.decode("") == []239 assert decoder.decode("a\n\nb\nc") == ["a\n", "\n", "b\n"]240 assert decoder.flush() == ["c"]241242 decoder = LineDecoder()243 assert decoder.decode("") == []244 assert decoder.decode("a\n\nb\nc\n") == ["a\n", "\n", "b\n", "c\n"]245 assert decoder.flush() == []246

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

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

line 255why, one step back
255

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 256why, one step back
256

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 257–267why, one step back
257def test_line_decoder_cr():
258 decoder = LineDecoder()259 assert decoder.decode("") == []260 assert decoder.decode("a\r\rb\rc") == ["a\n", "\n", "b\n"]261 assert decoder.flush() == ["c"]262263 decoder = LineDecoder()264 assert decoder.decode("") == []265 assert decoder.decode("a\r\rb\rc\r") == ["a\n", "\n", "b\n"]266 assert decoder.flush() == ["c\n"]267

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

Fixes for LineDecoding (#1075)Tom Christiebefore adoption20f4911

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

Fixes for LineDecoding (#1075)Tom Christiebefore adoption20f4911

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

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

line 276why, one step back
276

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 277why, one step back
277

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 278–293why, one step back
278def test_line_decoder_crnl():
279 decoder = LineDecoder()280 assert decoder.decode("") == []281 assert decoder.decode("a\r\n\r\nb\r\nc") == ["a\n", "\n", "b\n"]282 assert decoder.flush() == ["c"]283284 decoder = LineDecoder()285 assert decoder.decode("") == []286 assert decoder.decode("a\r\n\r\nb\r\nc\r\n") == ["a\n", "\n", "b\n", "c\n"]287 assert decoder.flush() == []288289 decoder = LineDecoder()290 assert decoder.decode("") == []291 assert decoder.decode("a\r") == []292 assert decoder.decode("\n\r\nb\r\nc") == ["a\n", "\n", "b\n"]293 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 294–302why, one step back
294
295 # Issue #1033296 decoder = LineDecoder()297 assert decoder.decode("") == []298 assert decoder.decode("12345\r\n") == ["12345\n"]299 assert decoder.decode("foo ") == []300 assert decoder.decode("bar ") == []301 assert decoder.decode("baz\r\n") == ["foo bar baz\n"]302 assert decoder.flush() == []

Support for `chunk_size` (#1277)Tom Christiebefore adoption27df5e4

lines 303–346why, one step back
303
304305def test_byte_chunker():306 decoder = ByteChunker()307 assert decoder.decode(b"1234567") == [b"1234567"]308 assert decoder.decode(b"89") == [b"89"]309 assert decoder.flush() == []310311 decoder = ByteChunker(chunk_size=3)312 assert decoder.decode(b"1234567") == [b"123", b"456"]313 assert decoder.decode(b"89") == [b"789"]314 assert decoder.flush() == []315316 decoder = ByteChunker(chunk_size=3)317 assert decoder.decode(b"123456") == [b"123", b"456"]318 assert decoder.decode(b"789") == [b"789"]319 assert decoder.flush() == []320321 decoder = ByteChunker(chunk_size=3)322 assert decoder.decode(b"123456") == [b"123", b"456"]323 assert decoder.decode(b"78") == []324 assert decoder.flush() == [b"78"]325326327def test_text_chunker():328 decoder = TextChunker()329 assert decoder.decode("1234567") == ["1234567"]330 assert decoder.decode("89") == ["89"]331 assert decoder.flush() == []332333 decoder = TextChunker(chunk_size=3)334 assert decoder.decode("1234567") == ["123", "456"]335 assert decoder.decode("89") == ["789"]336 assert decoder.flush() == []337338 decoder = TextChunker(chunk_size=3)339 assert decoder.decode("123456") == ["123", "456"]340 assert decoder.decode("789") == ["789"]341 assert decoder.flush() == []342343 decoder = TextChunker(chunk_size=3)344 assert decoder.decode("123456") == ["123", "456"]345 assert decoder.decode("78") == []346 assert decoder.flush() == ["78"]

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

lines 347–352why, one step back
347
348349def test_invalid_content_encoding_header():350 headers = [(b"Content-Encoding", b"invalid-header")]351 body = b"test 123"352

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

lines 353–357why, one step back
353 response = httpx.Response(
354 200,355 headers=headers,356 content=body,357 )

Raise HTTPStatusError in raise_from_status (#1072)François Voronbefore adoption27b0dbc

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