121 lines
4.7 KiB
Python
121 lines
4.7 KiB
Python
import gzip
|
|
import re
|
|
import shutil
|
|
from pathlib import Path
|
|
|
|
import httpx
|
|
import pytest
|
|
|
|
from src import main
|
|
from src.frontend_bundle import build_frontend
|
|
from src.views import dashboard, runtime_bundle, service_worker
|
|
|
|
|
|
FRONTEND = Path(__file__).resolve().parents[1] / "frontend"
|
|
PAGE_SCRIPT = re.compile(r'<script src="(static/[^"]+\.js)"></script>')
|
|
|
|
|
|
def test_page_runtime_is_one_deterministic_content_addressed_bundle(tmp_path):
|
|
first = build_frontend(FRONTEND)
|
|
second = build_frontend(FRONTEND)
|
|
|
|
assert first.runtime_name == second.runtime_name
|
|
assert first.runtime_bytes == second.runtime_bytes
|
|
assert PAGE_SCRIPT.findall(first.dashboard_html) == []
|
|
assert first.dashboard_html.count("<script src=") == 1
|
|
assert f'<script src="{first.runtime_name}"></script>' in first.dashboard_html
|
|
assert first.runtime_name.startswith("runtime-")
|
|
assert first.runtime_name.endswith(".js")
|
|
assert len(gzip.compress(first.runtime_bytes, mtime=0)) <= 100 * 1024
|
|
|
|
changed_frontend = tmp_path / "frontend"
|
|
shutil.copytree(FRONTEND, changed_frontend)
|
|
(changed_frontend / "widgets.js").write_text(
|
|
(changed_frontend / "widgets.js").read_text() + "\n// changed\n"
|
|
)
|
|
|
|
changed = build_frontend(changed_frontend)
|
|
assert changed.runtime_name != first.runtime_name
|
|
assert changed.runtime_bytes != first.runtime_bytes
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_runtime_is_transferred_as_gzip_when_the_client_accepts_it():
|
|
transport = httpx.ASGITransport(app=main.app)
|
|
|
|
async with httpx.AsyncClient(transport=transport, base_url="http://test") as client:
|
|
response = await client.get(
|
|
f"/{main.FRONTEND_BUILD.runtime_name}",
|
|
headers={"Accept-Encoding": "gzip"},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
assert response.headers["content-encoding"] == "gzip"
|
|
assert response.headers["vary"] == "Accept-Encoding"
|
|
assert int(response.headers["content-length"]) <= 100 * 1024
|
|
assert response.content == main.FRONTEND_BUILD.runtime_bytes
|
|
assert response.headers["cache-control"] == "public, max-age=31536000, immutable"
|
|
|
|
|
|
@pytest.mark.anyio
|
|
@pytest.mark.parametrize("accept_encoding", ["identity", "gzip;q=0, identity;q=1"])
|
|
async def test_runtime_stays_unencoded_when_the_client_declines_gzip(accept_encoding):
|
|
transport = httpx.ASGITransport(app=main.app)
|
|
|
|
async with httpx.AsyncClient(transport=transport, base_url="http://test") as client:
|
|
response = await client.get(
|
|
f"/{main.FRONTEND_BUILD.runtime_name}",
|
|
headers={"Accept-Encoding": accept_encoding},
|
|
)
|
|
|
|
assert response.status_code == 200
|
|
assert "content-encoding" not in response.headers
|
|
assert int(response.headers["content-length"]) == len(main.FRONTEND_BUILD.runtime_bytes)
|
|
assert response.content == main.FRONTEND_BUILD.runtime_bytes
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_dashboard_shell_is_compressed_without_losing_response_policies():
|
|
transport = httpx.ASGITransport(app=main.app)
|
|
|
|
async with httpx.AsyncClient(transport=transport, base_url="http://test") as client:
|
|
page = await client.get("/", headers={"Accept-Encoding": "gzip"})
|
|
stylesheet = await client.get(
|
|
"/static/dashboard.css", headers={"Accept-Encoding": "gzip"}
|
|
)
|
|
health = await client.get("/healthz", headers={"Accept-Encoding": "gzip"})
|
|
|
|
assert page.headers["content-encoding"] == "gzip"
|
|
assert page.headers["cache-control"] == "no-cache"
|
|
assert page.headers["content-security-policy"]
|
|
assert main.FRONTEND_BUILD.runtime_name in page.text
|
|
assert stylesheet.headers["content-encoding"] == "gzip"
|
|
assert "mobile-task-dock" in stylesheet.text
|
|
assert health.json() == {"service": "stackchain-dashboard", "status": "ok"}
|
|
assert "content-encoding" not in health.headers
|
|
|
|
|
|
def test_offline_shell_precaches_exact_runtime_without_superseded_page_modules():
|
|
build = build_frontend(FRONTEND)
|
|
|
|
assert f"BASE + '{build.runtime_name}'" in build.service_worker_source
|
|
for source in build.page_sources:
|
|
if source == "static/background-issue-sync.js":
|
|
continue
|
|
assert f"BASE + '{source}'" not in build.service_worker_source
|
|
|
|
|
|
@pytest.mark.anyio
|
|
async def test_runtime_asset_is_immutable_while_html_and_worker_revalidate():
|
|
build = build_frontend(FRONTEND)
|
|
|
|
html = await dashboard()
|
|
bundle = await runtime_bundle(build.runtime_digest)
|
|
worker = await service_worker()
|
|
|
|
assert build.runtime_name in html
|
|
assert bundle.body == build.runtime_bytes
|
|
assert bundle.headers["cache-control"] == "public, max-age=31536000, immutable"
|
|
assert worker.headers["cache-control"] == "no-cache"
|
|
assert build.runtime_name.encode() in worker.body
|