RepoPulse: update "backend/app/main.py" and "backend/tests/test_frontend_routes.py"
Public changes compared with the previous published export: 2 files changed. Affected areas: - backend/app: 1 file - tests: 1 file Changed files: - Updated: "backend/app/main.py" - Added: "backend/tests/test_frontend_routes.py" Export provenance/manifests refreshed; excluded from the change summary. Publish curated RepoPulse source from 1201120636c6141d94a4aee81b48b5b001cea045
This commit is contained in:
1 parent
787a2b1dc3
commit
131096340e
4 files changed
+84
-6
No files matched your search
@@ -0,0 +1,51 @@
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from fastapi import FastAPI
|
||||
from fastapi.testclient import TestClient
|
||||
|
||||
from app.main import _FrontendStaticFiles
|
||||
|
||||
|
||||
def _client(tmp_path: Path) -> TestClient:
|
||||
static = tmp_path / "static"
|
||||
(static / "assets").mkdir(parents=True)
|
||||
(static / "index.html").write_text("<!doctype html><div id=root></div>", encoding="utf-8")
|
||||
(static / "assets" / "index-abc123.js").write_text("console.log(1)", encoding="utf-8")
|
||||
(static / "favicon.svg").write_text("<svg/>", encoding="utf-8")
|
||||
app = FastAPI()
|
||||
|
||||
@app.get("/api/v1/ping")
|
||||
def ping() -> dict[str, str]:
|
||||
return {"status": "ok"}
|
||||
|
||||
app.mount("/", _FrontendStaticFiles(directory=static, html=True), name="frontend")
|
||||
return TestClient(app)
|
||||
|
||||
|
||||
def test_client_routes_fall_back_to_the_spa_shell(tmp_path: Path) -> None:
|
||||
client = _client(tmp_path)
|
||||
for path in ("/repositories", "/activity", "/queue", "/settings", "/repositories/gitea%3A64/findings"):
|
||||
response = client.get(path)
|
||||
assert response.status_code == 200, path
|
||||
assert "id=root" in response.text
|
||||
assert response.headers["cache-control"] == "no-cache"
|
||||
|
||||
|
||||
def test_api_assets_and_missing_files_keep_an_honest_404(tmp_path: Path) -> None:
|
||||
client = _client(tmp_path)
|
||||
assert client.get("/api/v1/ping").json() == {"status": "ok"}
|
||||
assert client.get("/api/v1/unknown").status_code == 404
|
||||
assert client.get("/assets/missing-chunk.js").status_code == 404
|
||||
assert client.get("/robots.txt").status_code == 404
|
||||
assert client.post("/repositories").status_code in {404, 405}
|
||||
|
||||
|
||||
def test_hashed_assets_stay_immutable_and_files_are_served_directly(tmp_path: Path) -> None:
|
||||
client = _client(tmp_path)
|
||||
asset = client.get("/assets/index-abc123.js")
|
||||
assert asset.status_code == 200
|
||||
assert asset.headers["cache-control"] == "public, max-age=31536000, immutable"
|
||||
assert client.get("/favicon.svg").headers["cache-control"] == "no-cache"
|
||||
assert client.get("/").headers["cache-control"] == "no-cache"
|
||||
Reference in new issue
Block a user