Skip to content
Myr

test_decoders.py

This file

Path

tests/test_decoders.py

218 lines · 6.4 KiB · Python

Last changed by

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

History of this file

Take it

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

Merge pull request #241 from encode/protocol-supportTom Christiebefore adoptione0b523d

lines 1–11why, one step back
1import zlib
23import brotli4import pytest56import httpx7from httpx.decoders import (8 BrotliDecoder,9 DeflateDecoder,10 GZipDecoder,11 IdentityDecoder,

Add Response.stream_lines (#575)Tom Christiebefore adoption248aa58

line 12why, one step back
12 LineDecoder,

Merge pull request #241 from encode/protocol-supportTom Christiebefore adoptione0b523d

lines 13–74why, one step back
13 TextDecoder,
14)151617def test_deflate():18 body = b"test 123"19 compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)20 compressed_body = compressor.compress(body) + compressor.flush()2122 headers = [(b"Content-Encoding", b"deflate")]23 response = httpx.Response(200, headers=headers, content=compressed_body)24 assert response.content == body252627def test_gzip():28 body = b"test 123"29 compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)30 compressed_body = compressor.compress(body) + compressor.flush()3132 headers = [(b"Content-Encoding", b"gzip")]33 response = httpx.Response(200, headers=headers, content=compressed_body)34 assert response.content == body353637def test_brotli():38 body = b"test 123"39 compressed_body = brotli.compress(body)4041 headers = [(b"Content-Encoding", b"br")]42 response = httpx.Response(200, headers=headers, content=compressed_body)43 assert response.content == body444546def test_multi():47 body = b"test 123"4849 deflate_compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)50 compressed_body = deflate_compressor.compress(body) + deflate_compressor.flush()5152 gzip_compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)53 compressed_body = (54 gzip_compressor.compress(compressed_body) + gzip_compressor.flush()55 )5657 headers = [(b"Content-Encoding", b"deflate, gzip")]58 response = httpx.Response(200, headers=headers, content=compressed_body)59 assert response.content == body606162def test_multi_with_identity():63 body = b"test 123"64 compressed_body = brotli.compress(body)6566 headers = [(b"Content-Encoding", b"br, identity")]67 response = httpx.Response(200, headers=headers, content=compressed_body)68 assert response.content == body6970 headers = [(b"Content-Encoding", b"identity, br")]71 response = httpx.Response(200, headers=headers, content=compressed_body)72 assert response.content == body7374

Drop sync (#544)Tom Christiebefore adoption206c537

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

Merge pull request #241 from encode/protocol-supportTom Christiebefore adoptione0b523d

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

Drop sync (#544)Tom Christiebefore adoption206c537

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

Merge pull request #241 from encode/protocol-supportTom Christiebefore adoptione0b523d

lines 81–86why, one step back
81 yield compressor.compress(body)
82 yield compressor.flush()8384 headers = [(b"Content-Encoding", b"gzip")]85 response = httpx.Response(200, headers=headers, content=compress(body))86 assert not hasattr(response, "body")

Drop sync (#544)Tom Christiebefore adoption206c537

line 87why, one step back
87 assert await response.read() == body

Merge pull request #241 from encode/protocol-supportTom Christiebefore adoptione0b523d

lines 88–110why, one step back
88
8990@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br", b"identity"))91def test_empty_content(header_value):92 headers = [(b"Content-Encoding", header_value)]93 response = httpx.Response(200, headers=headers, content=b"")94 assert response.content == b""959697@pytest.mark.parametrize(98 "decoder", (BrotliDecoder, DeflateDecoder, GZipDecoder, IdentityDecoder)99)100def test_decoders_empty_cases(decoder):101 instance = decoder()102 assert instance.decode(b"") == b""103 assert instance.flush() == b""104105106@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))107def test_decoding_errors(header_value):108 headers = [(b"Content-Encoding", header_value)]109 body = b"test 123"110 compressed_body = brotli.compress(body)[3:]

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

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

Merge pull request #241 from encode/protocol-supportTom Christiebefore adoptione0b523d

lines 112–132why, one step back
112 response = httpx.Response(200, headers=headers, content=compressed_body)
113 response.content114115116@pytest.mark.parametrize(117 ["data", "encoding"],118 [119 ((b"Hello,", b" world!"), "ascii"),120 ((b"\xe3\x83", b"\x88\xe3\x83\xa9", b"\xe3", b"\x83\x99\xe3\x83\xab"), "utf-8"),121 ((b"\x83g\x83\x89\x83x\x83\x8b",) * 64, "shift-jis"),122 ((b"\x83g\x83\x89\x83x\x83\x8b",) * 600, "shift-jis"),123 (124 (b"\xcb\xee\xf0\xe5\xec \xe8\xef\xf1\xf3\xec \xe4\xee\xeb\xee\xf0",) * 64,125 "MacCyrillic",126 ),127 (128 (b"\xa5\xa6\xa5\xa7\xa5\xd6\xa4\xce\xb9\xf1\xba\xdd\xb2\xbd",) * 512,129 "euc-jp",130 ),131 ],132)

Drop sync (#544)Tom Christiebefore adoption206c537

lines 133–135why, one step back
133@pytest.mark.asyncio
134async def test_text_decoder(data, encoding):135 async def iterator():

Merge pull request #241 from encode/protocol-supportTom Christiebefore adoptione0b523d

lines 136–140why, one step back
136 nonlocal data
137 for chunk in data:138 yield chunk139140 response = httpx.Response(200, content=iterator())

Drop sync (#544)Tom Christiebefore adoption206c537

lines 141–142why, one step back
141 await response.read()
142 assert response.text == (b"".join(data)).decode(encoding)

Merge pull request #241 from encode/protocol-supportTom Christiebefore adoptione0b523d

lines 143–144why, one step back
143
144

Drop sync (#544)Tom Christiebefore adoption206c537

lines 145–147why, one step back
145@pytest.mark.asyncio
146async def test_text_decoder_known_encoding():147 async def iterator():

Merge pull request #241 from encode/protocol-supportTom Christiebefore adoptione0b523d

lines 148–157why, one step back
148 yield b"\x83g"
149 yield b"\x83"150 yield b"\x89\x83x\x83\x8b"151152 response = httpx.Response(153 200,154 headers=[(b"Content-Type", b"text/html; charset=shift-jis")],155 content=iterator(),156 )157

Drop sync (#544)Tom Christiebefore adoption206c537

lines 158–159why, one step back
158 await response.read()
159 assert "".join(response.text) == "トラベル"

Merge pull request #241 from encode/protocol-supportTom Christiebefore adoptione0b523d

lines 160–168why, one step back
160
161162def test_text_decoder_empty_cases():163 decoder = TextDecoder()164 assert decoder.flush() == ""165166 decoder = TextDecoder()167 assert decoder.decode(b"") == ""168 assert decoder.flush() == ""

Add Response.stream_lines (#575)Tom Christiebefore adoption248aa58

lines 169–210why, one step back
169
170171def test_line_decoder_nl():172 decoder = LineDecoder()173 assert decoder.decode("") == []174 assert decoder.decode("a\n\nb\nc") == ["a\n", "\n", "b\n"]175 assert decoder.flush() == ["c"]176177 decoder = LineDecoder()178 assert decoder.decode("") == []179 assert decoder.decode("a\n\nb\nc\n") == ["a\n", "\n", "b\n", "c\n"]180 assert decoder.flush() == []181182183def test_line_decoder_cr():184 decoder = LineDecoder()185 assert decoder.decode("") == []186 assert decoder.decode("a\r\rb\rc") == ["a\n", "\n", "b\n"]187 assert decoder.flush() == ["c"]188189 decoder = LineDecoder()190 assert decoder.decode("") == []191 assert decoder.decode("a\r\rb\rc\r") == ["a\n", "\n", "b\n"]192 assert decoder.flush() == ["c\n"]193194195def test_line_decoder_crnl():196 decoder = LineDecoder()197 assert decoder.decode("") == []198 assert decoder.decode("a\r\n\r\nb\r\nc") == ["a\n", "\n", "b\n"]199 assert decoder.flush() == ["c"]200201 decoder = LineDecoder()202 assert decoder.decode("") == []203 assert decoder.decode("a\r\n\r\nb\r\nc\r\n") == ["a\n", "\n", "b\n", "c\n"]204 assert decoder.flush() == []205206 decoder = LineDecoder()207 assert decoder.decode("") == []208 assert decoder.decode("a\r") == []209 assert decoder.decode("\n\r\nb\r\nc") == ["a\n", "\n", "b\n"]210 assert decoder.flush() == ["c"]

Merge pull request #241 from encode/protocol-supportTom Christiebefore adoptione0b523d

lines 211–217why, one step back
211
212213def test_invalid_content_encoding_header():214 headers = [(b"Content-Encoding", b"invalid-header")]215 body = b"test 123"216217 response = httpx.Response(200, headers=headers, content=body)

Drop sync (#544)Tom Christiebefore adoption206c537

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