Files
AstrBot/tests/unit/test_dashboard_static_encoding.py
T
卷卷andRuochenPan 67c74b7ed8 feat: gzip dashboard static js, css, and html (#10187)
* feat: gzip dashboard static js, css, and html

Compress JS, CSS, and HTML from the dashboard static routes when the client sends Accept-Encoding: gzip. Images, fonts, event streams, and range requests stay unchanged. Cache-Control is not modified.

Refs #10186

* fix: gzip dashboard static files off the event loop

Read and compress in a worker thread so a large bundle does not stall other dashboard requests.

---------

Co-authored-by: RuochenPan <badbatch0x01@gmail.com>
2026-09-24 11:49:24 +08:00

52 lines
1.8 KiB
Python

"""Tests for dashboard static gzip encoding."""
import gzip
from astrbot.dashboard.static_encoding import (
accepts_gzip,
gzip_static_body,
is_compressible_media_type,
with_gzip_headers,
)
def test_accepts_gzip_only_when_explicitly_allowed():
assert accepts_gzip("gzip")
assert accepts_gzip("br, gzip, deflate")
assert accepts_gzip("gzip;q=0.8")
assert not accepts_gzip(None)
assert not accepts_gzip("")
assert not accepts_gzip("br")
assert not accepts_gzip("gzip;q=0")
assert not accepts_gzip("identity")
def test_only_js_css_and_html_are_compressible():
assert is_compressible_media_type("text/javascript")
assert is_compressible_media_type("text/javascript; charset=utf-8")
assert is_compressible_media_type("application/javascript")
assert is_compressible_media_type("text/css")
assert is_compressible_media_type("text/html")
assert not is_compressible_media_type("font/woff2")
assert not is_compressible_media_type("image/png")
assert not is_compressible_media_type("image/svg+xml")
assert not is_compressible_media_type("text/event-stream")
assert not is_compressible_media_type("application/json")
assert not is_compressible_media_type(None)
def test_gzip_static_body_shrinks_text_and_skips_tiny_payloads():
raw = b"function boot(){return 'dashboard';}" * 80
compressed = gzip_static_body(raw)
assert compressed is not None
assert len(compressed) < len(raw)
assert gzip.decompress(compressed) == raw
assert gzip_static_body(b"tiny") is None
def test_gzip_headers_keep_cache_control():
headers = with_gzip_headers({"Cache-Control": "no-cache", "Vary": "Origin"})
assert headers["Cache-Control"] == "no-cache"
assert headers["Content-Encoding"] == "gzip"
assert headers["Vary"] == "Origin, Accept-Encoding"