Files
sub2api/backend/internal/web/html_cache_headers_test.go
Henglie f1394a1ab4 fix(web): stop caching settings-injected HTML
Settings are injected into index.html and the page also embeds a
per-request CSP nonce. A cached document can paint stale branding, and
answering If-None-Match with 304 while minting a new nonce makes the
browser reuse HTML whose nonce no longer matches the CSP header.

Mark every settings-injected HTML response as uncacheable for browsers
and CDN edges, and only allow 304 when the response has no nonce.
2026-11-14 13:09:07 +08:00

41 lines
1.2 KiB
Go

//go:build unit
package web
import (
"net/http"
"testing"
"github.com/stretchr/testify/assert"
)
func TestApplyHTMLNoCacheHeaders(t *testing.T) {
header := http.Header{}
applyHTMLNoCacheHeaders(header)
assert.Equal(t, "no-cache, no-store, must-revalidate", header.Get("Cache-Control"))
assert.Equal(t, "no-cache", header.Get("Pragma"))
assert.Equal(t, "0", header.Get("Expires"))
assert.Equal(t, "no-store", header.Get("CDN-Cache-Control"))
assert.Equal(t, "no-store", header.Get("Surrogate-Control"))
}
func TestApplyHTMLNoCacheHeadersReplacesExistingValues(t *testing.T) {
header := http.Header{
"Cache-Control": {"public, max-age=3600"},
"Pragma": {"cache"},
"Expires": {"Thu, 01 Jan 2099 00:00:00 GMT"},
"CDN-Cache-Control": {"max-age=3600"},
"Surrogate-Control": {"max-age=3600"},
}
applyHTMLNoCacheHeaders(header)
assert.Equal(t, "no-cache, no-store, must-revalidate", header.Get("Cache-Control"))
assert.Equal(t, "no-cache", header.Get("Pragma"))
assert.Equal(t, "0", header.Get("Expires"))
assert.Equal(t, "no-store", header.Get("CDN-Cache-Control"))
assert.Equal(t, "no-store", header.Get("Surrogate-Control"))
}