Files
romm/backend/tests/test_static_precompression.py
Sam Dornan b48f6003b5 fix(nginx): scope gzip_static to the build output
In the http block gzip_static was inherited by every location, including
the internal /library/ and /cache/ ones that serve downloads. RomM handles
.gz and .tar.gz ROMs, so a library holding both Game.nes and Game.nes.gz
answered a request for the first with the second's bytes under
Content-Encoding: gzip. Reproduced against nginx with a real upstream
issuing X-Accel-Redirect: the download returned 73 bytes of gzip instead
of the 3000-byte ROM. Clients that do not transparently decode, and Range
resumes over /cache/, would have written the wrong file to disk.

It now sits in the three locations that serve the frontend build, where a
.gz sibling can only be one the build wrote. Verified both directions: the
ROM download returns the plain 3000 bytes with no Content-Encoding, and a
fingerprinted asset is still served from its precompressed sibling.

The test now guards the invariant rather than the directive's presence:
gzip_static must be absent from the http block and from every location
serving files the build did not produce.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01HDXxq3vAxJVGhm1pQuK8V7
2026-09-12 02:04:03 +00:00

64 lines
2.2 KiB
Python

"""Assert the frontend precompresses its assets and only they are served as .gz."""
import re
from pathlib import Path
import pytest
REPO_ROOT = Path(__file__).resolve().parents[2]
NGINX_CONF = REPO_ROOT / "docker" / "nginx" / "default.conf"
NGINX_TEMPLATE = REPO_ROOT / "docker" / "nginx" / "templates" / "default.conf.template"
VITE_CONFIG = REPO_ROOT / "frontend" / "vite.config.js"
# Served from the frontend build, where every .gz sibling is one the build wrote.
PRECOMPRESSED_LOCATIONS = ["/", "/assets"]
# Internal download locations. A library may hold both `Game.nes` and
# `Game.nes.gz`, so gzip_static here would answer a request for one with the
# other's bytes under Content-Encoding: gzip.
RAW_LOCATIONS = ["/library/", "/cache/", "/assets/romm/resources/"]
def _location_body(config: str, path: str) -> str:
start = config.index(f"location {path} {{") + len(f"location {path} {{")
depth, i = 1, start
while depth:
if config[i] == "{":
depth += 1
elif config[i] == "}":
depth -= 1
i += 1
return config[start : i - 1]
def _enables_gzip_static(body: str) -> bool:
return bool(re.search(r"^\s*gzip_static\s+on;", body, re.M))
def test_gzip_static_is_not_inherited_by_every_location() -> None:
# In the http block it would reach the internal download locations too.
assert not _enables_gzip_static(NGINX_CONF.read_text())
@pytest.mark.parametrize("path", PRECOMPRESSED_LOCATIONS)
def test_build_output_is_served_precompressed(path: str) -> None:
body = _location_body(NGINX_TEMPLATE.read_text(), path)
assert _enables_gzip_static(body), f"location {path} would ignore the .gz siblings"
@pytest.mark.parametrize("path", RAW_LOCATIONS)
def test_downloads_are_never_served_from_a_gz_sibling(path: str) -> None:
body = _location_body(NGINX_TEMPLATE.read_text(), path)
assert not _enables_gzip_static(body), (
f"gzip_static in {path} would serve a .gz sibling instead of the "
f"requested file"
)
def test_build_precompresses() -> None:
config = VITE_CONFIG.read_text()
assert re.search(r"\bprecompress\(\)", config), (
"vite.config.js no longer runs the precompress plugin, so the "
"gzip_static locations would have nothing to serve"
)