Skip to content
Myr

added control to calling decode for empty contents (#237)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

* added tests for all decoder * fixed BrotliDecoder for b"" value

authored by Can Sarıgöl · committed by Tom Christie · parent ef9fc0f · Browse files · 2 files+29 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+29 added−1 removed

  • tests:+23 added−1 removed
  • other:+6 added−0 removed

2 files+29 added−1 removed

modified:httpx/decoders.py+6 added
96 unmodified lines
97 assert (98 brotli is not None99 ), "The 'brotlipy' or 'brotli' library must be installed to use 'BrotliDecoder'"100 self.decompressor = brotli.Decompressor()101 self.seen_data = False102103 def decode(self, data: bytes) -> bytes:104 if not data:105 return b""106 self.seen_data = True107 try:108 if hasattr(self.decompressor, "decompress"):109 return self.decompressor.decompress(data)110 return self.decompressor.process(data) # pragma: nocover111 except brotli.error as exc:112 raise DecodingError from exc113114 def flush(self) -> bytes:115 if not self.seen_data:116 return b""117 try:118 if hasattr(self.decompressor, "finish"):119 self.decompressor.finish()120 return b""
108 unmodified lines
modified:tests/test_decoders.py+23 added-1 removed
2 unmodified lines
3import brotli4import pytest56import httpx7from httpx.decoders import TextDecoder7from httpx.decoders import (8 BrotliDecoder,9 DeflateDecoder,10 GZipDecoder,11 IdentityDecoder,12 TextDecoder,13)141516def test_deflate():17 body = b"test 123"
66 unmodified lines
84 assert not hasattr(response, "body")85 assert response.read() == body868788@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br", b"identity"))89def test_empty_content(header_value):90 headers = [(b"Content-Encoding", header_value)]91 response = httpx.Response(200, headers=headers, content=b"")92 assert response.content == b""939495@pytest.mark.parametrize(96 "decoder", (BrotliDecoder, DeflateDecoder, GZipDecoder, IdentityDecoder)97)98def test_decoders_empty_cases(decoder):99 instance = decoder()100 assert instance.decode(b"") == b""101 assert instance.flush() == b""102103104@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))105def test_decoding_errors(header_value):106 headers = [(b"Content-Encoding", header_value)]107 body = b"test 123"
64 unmodified lines