test_decoders.py
This file
Path
tests/test_decoders.py
88 lines · 2.7 KiB · Python
Last changed by
Renaming -> httpx (#129)Tom Christiebefore adoption2d09d5b
History of this fileTake it
1import zlib23import brotli4import pytest56import httpx789def test_deflate():10 body = b"test 123"11 compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)12 compressed_body = compressor.compress(body) + compressor.flush()1314 headers = [(b"Content-Encoding", b"deflate")]15 response = httpx.Response(200, headers=headers, content=compressed_body)16 assert response.content == body171819def test_gzip():20 body = b"test 123"21 compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)22 compressed_body = compressor.compress(body) + compressor.flush()2324 headers = [(b"Content-Encoding", b"gzip")]25 response = httpx.Response(200, headers=headers, content=compressed_body)26 assert response.content == body272829def test_brotli():30 body = b"test 123"31 compressed_body = brotli.compress(body)3233 headers = [(b"Content-Encoding", b"br")]34 response = httpx.Response(200, headers=headers, content=compressed_body)35 assert response.content == body363738def test_multi():39 body = b"test 123"4041 deflate_compressor = zlib.compressobj(9, zlib.DEFLATED, -zlib.MAX_WBITS)42 compressed_body = deflate_compressor.compress(body) + deflate_compressor.flush()4344 gzip_compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)45 compressed_body = (46 gzip_compressor.compress(compressed_body) + gzip_compressor.flush()47 )4849 headers = [(b"Content-Encoding", b"deflate, gzip")]50 response = httpx.Response(200, headers=headers, content=compressed_body)51 assert response.content == body525354def test_multi_with_identity():55 body = b"test 123"56 compressed_body = brotli.compress(body)5758 headers = [(b"Content-Encoding", b"br, identity")]59 response = httpx.Response(200, headers=headers, content=compressed_body)60 assert response.content == body6162 headers = [(b"Content-Encoding", b"identity, br")]63 response = httpx.Response(200, headers=headers, content=compressed_body)64 assert response.content == body656667def test_streaming():68 body = b"test 123"69 compressor = zlib.compressobj(9, zlib.DEFLATED, zlib.MAX_WBITS | 16)7071 def compress(body):72 yield compressor.compress(body)73 yield compressor.flush()7475 headers = [(b"Content-Encoding", b"gzip")]76 response = httpx.Response(200, headers=headers, content=compress(body))77 assert not hasattr(response, "body")78 assert response.read() == body798081@pytest.mark.parametrize("header_value", (b"deflate", b"gzip", b"br"))82def test_decoding_errors(header_value):83 headers = [(b"Content-Encoding", header_value)]84 body = b"test 123"85 compressed_body = brotli.compress(body)[3:]86 with pytest.raises(httpx.exceptions.DecodingError):87 response = httpx.Response(200, headers=headers, content=compressed_body)88 response.content