mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-11-12 03:55:05 +00:00
Move the supported interpreter range to >=3.11,<3.15, dropping the EOL 3.10 floor and unblocking installs on 3.13/3.14. - pyproject: requires-python ">=3.10,<3.13" -> ">=3.11,<3.15" - update_checker: replace deprecated asyncio.get_event_loop() with asyncio.to_thread() (warns on 3.12+, slated for removal) - test_compression: simplify _run() to plain asyncio.run() - README: bump documented Python range to 3.11 - 3.14 - uv.lock: regenerate for the new interpreter floor
65 lines
1.9 KiB
Python
65 lines
1.9 KiB
Python
"""Unit tests for unshackle.core.api.compression.compression_middleware."""
|
|
|
|
from __future__ import annotations
|
|
|
|
import asyncio
|
|
import gzip
|
|
import json
|
|
|
|
import pytest
|
|
from aiohttp import web
|
|
|
|
from unshackle.core.api.compression import compression_middleware
|
|
|
|
pytestmark = pytest.mark.unit
|
|
|
|
|
|
class _FakeReq:
|
|
def __init__(self, accept_encoding: str = "gzip") -> None:
|
|
self.headers = {"Accept-Encoding": accept_encoding}
|
|
|
|
|
|
def _run(coro):
|
|
return asyncio.run(coro)
|
|
|
|
|
|
def test_skips_when_client_does_not_accept_gzip() -> None:
|
|
body_json = json.dumps({"data": "x" * 4096}).encode()
|
|
|
|
async def handler(req): # noqa: ARG001
|
|
return web.json_response({"data": "x" * 4096})
|
|
|
|
req = _FakeReq(accept_encoding="identity")
|
|
resp = _run(compression_middleware(req, handler))
|
|
assert resp.headers.get("Content-Encoding") != "gzip"
|
|
assert resp.body == body_json or len(resp.body) >= len(body_json) - 8
|
|
|
|
|
|
def test_skips_when_body_below_threshold() -> None:
|
|
async def handler(req): # noqa: ARG001
|
|
return web.json_response({"hi": "x"})
|
|
|
|
resp = _run(compression_middleware(_FakeReq(), handler))
|
|
assert resp.headers.get("Content-Encoding") != "gzip"
|
|
|
|
|
|
def test_skips_non_json_response() -> None:
|
|
async def handler(req): # noqa: ARG001
|
|
return web.Response(body=b"x" * 4096, content_type="text/plain")
|
|
|
|
resp = _run(compression_middleware(_FakeReq(), handler))
|
|
assert resp.headers.get("Content-Encoding") != "gzip"
|
|
|
|
|
|
def test_compresses_large_json_when_accepted() -> None:
|
|
big = {"data": "x" * 4096}
|
|
|
|
async def handler(req): # noqa: ARG001
|
|
return web.json_response(big)
|
|
|
|
resp = _run(compression_middleware(_FakeReq(), handler))
|
|
assert resp.headers.get("Content-Encoding") == "gzip"
|
|
decompressed = gzip.decompress(resp.body)
|
|
assert json.loads(decompressed) == big
|
|
assert resp.headers["Content-Length"] == str(len(resp.body))
|