brotli is not None
), "The 'brotlipy' or 'brotli' library must be installed to use 'BrotliDecoder'"
self.decompressor = brotli.Decompressor()
+ self.seen_data = False
def decode(self, data: bytes) -> bytes:
+ if not data:
+ return b""
+ self.seen_data = True
try:
if hasattr(self.decompressor, "decompress"):
return self.decompressor.decompress(data)
raise DecodingError from exc
def flush(self) -> bytes:
+ if not self.seen_data:
+ return b""
try:
if hasattr(self.decompressor, "finish"):
self.decompressor.finish()
import pytest
import httpx
-from httpx.decoders import TextDecoder
+from httpx.decoders import (
+ BrotliDecoder,
+ DeflateDecoder,
+ GZipDecoder,
+ IdentityDecoder,
+ TextDecoder,
+)
def test_deflate():
assert response.read() == body
+@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br", b"identity"))
+def test_empty_content(header_value):
+ headers = [(b"Content-Encoding", header_value)]
+ response = httpx.Response(200, headers=headers, content=b"")
+ assert response.content == b""
+
+
+@pytest.mark.parametrize(
+ "decoder", (BrotliDecoder, DeflateDecoder, GZipDecoder, IdentityDecoder)
+)
+def test_decoders_empty_cases(decoder):
+ instance = decoder()
+ assert instance.decode(b"") == b""
+ assert instance.flush() == b""
+
+
@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))
def test_decoding_errors(header_value):
headers = [(b"Content-Encoding", header_value)]