maximize Cloudflare edge caching
This commit is contained in:
@@ -84,6 +84,10 @@ def test_city_detail_batch_response_includes_backend_server_timing(monkeypatch):
|
||||
assert "city_detail_batch_full_data_paris" in server_timing
|
||||
assert "city_detail_batch_detail_payload_paris" in server_timing
|
||||
assert "city_detail_batch_total" in server_timing
|
||||
assert response.headers["cache-control"] == (
|
||||
"public, max-age=30, s-maxage=60, stale-while-revalidate=300"
|
||||
)
|
||||
assert response.headers["cloudflare-cdn-cache-control"] == response.headers["cache-control"]
|
||||
|
||||
|
||||
def test_city_detail_response_includes_backend_server_timing(monkeypatch):
|
||||
@@ -125,7 +129,12 @@ def test_scan_terminal_response_includes_backend_server_timing(monkeypatch):
|
||||
monkeypatch.setattr(
|
||||
scan_api.legacy_routes,
|
||||
"build_scan_terminal_payload",
|
||||
lambda filters, force_refresh=False, timing_recorder=None: {"rows": [], "filters": filters},
|
||||
lambda filters, force_refresh=False, timing_recorder=None: {
|
||||
"rows": [],
|
||||
"filters": filters,
|
||||
"status": "ready",
|
||||
"stale": False,
|
||||
},
|
||||
)
|
||||
|
||||
response = client.get("/api/scan/terminal?limit=1")
|
||||
@@ -135,6 +144,30 @@ def test_scan_terminal_response_includes_backend_server_timing(monkeypatch):
|
||||
assert "scan_terminal_assert_entitlement" in server_timing
|
||||
assert "scan_terminal_build_payload" in server_timing
|
||||
assert "scan_terminal_total" in server_timing
|
||||
assert response.headers["cache-control"] == (
|
||||
"public, max-age=0, s-maxage=300, stale-while-revalidate=900"
|
||||
)
|
||||
assert response.headers["cloudflare-cdn-cache-control"] == response.headers["cache-control"]
|
||||
|
||||
|
||||
def test_scan_terminal_stale_response_is_not_cached(monkeypatch):
|
||||
monkeypatch.setattr(scan_api.legacy_routes, "_assert_entitlement", lambda request: None)
|
||||
monkeypatch.setattr(
|
||||
scan_api.legacy_routes,
|
||||
"build_scan_terminal_payload",
|
||||
lambda filters, force_refresh=False, timing_recorder=None: {
|
||||
"rows": [],
|
||||
"filters": filters,
|
||||
"status": "ready",
|
||||
"stale": True,
|
||||
},
|
||||
)
|
||||
|
||||
response = client.get("/api/scan/terminal?limit=1")
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.headers["cache-control"] == "no-store, max-age=0"
|
||||
assert response.headers["cloudflare-cdn-cache-control"] == "no-store, max-age=0"
|
||||
|
||||
|
||||
def test_online_users_response_includes_backend_server_timing():
|
||||
|
||||
@@ -0,0 +1,31 @@
|
||||
from starlette.datastructures import MutableHeaders
|
||||
|
||||
from web.services.cache_headers import (
|
||||
NO_STORE_CACHE_CONTROL,
|
||||
apply_cache_control,
|
||||
apply_no_store,
|
||||
public_edge_cache_control,
|
||||
)
|
||||
|
||||
|
||||
def test_public_edge_cache_control_separates_browser_and_edge_ttl():
|
||||
assert public_edge_cache_control(
|
||||
60,
|
||||
300,
|
||||
browser_max_age_seconds=30,
|
||||
) == "public, max-age=30, s-maxage=60, stale-while-revalidate=300"
|
||||
|
||||
|
||||
def test_cache_helpers_set_cloudflare_specific_header():
|
||||
headers = MutableHeaders()
|
||||
cache_control = public_edge_cache_control(300, 900)
|
||||
|
||||
apply_cache_control(headers, cache_control)
|
||||
|
||||
assert headers["cache-control"] == cache_control
|
||||
assert headers["cloudflare-cdn-cache-control"] == cache_control
|
||||
|
||||
apply_no_store(headers)
|
||||
|
||||
assert headers["cache-control"] == NO_STORE_CACHE_CONTROL
|
||||
assert headers["cloudflare-cdn-cache-control"] == NO_STORE_CACHE_CONTROL
|
||||
@@ -83,6 +83,8 @@ def test_events_endpoint_replays_only_requested_cities(monkeypatch):
|
||||
)
|
||||
|
||||
assert response.status_code == 200
|
||||
assert response.headers["cache-control"] == "no-cache, no-transform"
|
||||
assert response.headers["cloudflare-cdn-cache-control"] == "no-store"
|
||||
assert captured == {
|
||||
"cities": {"taipei", "hong kong"},
|
||||
"since_revision": 42,
|
||||
|
||||
Reference in New Issue
Block a user