- Add internal/adminui with a SPA handler: index fallback for client routes, immutable caching for hashed assets, 404 for missing assets, security headers - Answer 503 with a hint when the UI has not been built, so go build always works - Mount the UI at / and the API at /api/ on the same listener, no CORS needed - Scaffold admin/ with Vite, React, TypeScript, Tailwind v4, shadcn/ui and Vitest
154 lines
5.1 KiB
Go
154 lines
5.1 KiB
Go
package adminui
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
"testing/fstest"
|
|
)
|
|
|
|
func site() fstest.MapFS {
|
|
return fstest.MapFS{
|
|
"index.html": {Data: []byte("<!doctype html><div id=root></div>")},
|
|
"assets/app-abc123.js": {Data: []byte("console.log(1)")},
|
|
"assets/app-abc123.css": {Data: []byte("body{}")},
|
|
"favicon.svg": {Data: []byte("<svg/>")},
|
|
}
|
|
}
|
|
|
|
func fetch(h http.Handler, method, path string) *httptest.ResponseRecorder {
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest(method, path, nil))
|
|
return rec
|
|
}
|
|
|
|
func TestServesIndex(t *testing.T) {
|
|
rec := fetch(New(site()), "GET", "/")
|
|
if rec.Code != 200 || !strings.Contains(rec.Body.String(), `id=root`) {
|
|
t.Fatalf("%d %q", rec.Code, rec.Body.String())
|
|
}
|
|
if got := rec.Header().Get("Cache-Control"); got != "no-cache" {
|
|
t.Errorf("index must be revalidated, Cache-Control = %q", got)
|
|
}
|
|
if !strings.HasPrefix(rec.Header().Get("Content-Type"), "text/html") {
|
|
t.Errorf("content type = %q", rec.Header().Get("Content-Type"))
|
|
}
|
|
}
|
|
|
|
func TestHashedAssetsAreImmutable(t *testing.T) {
|
|
h := New(site())
|
|
js := fetch(h, "GET", "/assets/app-abc123.js")
|
|
if js.Code != 200 || js.Body.String() != "console.log(1)" {
|
|
t.Fatalf("%d %q", js.Code, js.Body.String())
|
|
}
|
|
if got := js.Header().Get("Cache-Control"); got != "public, max-age=31536000, immutable" {
|
|
t.Errorf("Cache-Control = %q", got)
|
|
}
|
|
if ct := js.Header().Get("Content-Type"); !strings.Contains(ct, "javascript") {
|
|
t.Errorf("js content type = %q", ct)
|
|
}
|
|
if ct := fetch(h, "GET", "/assets/app-abc123.css").Header().Get("Content-Type"); !strings.HasPrefix(ct, "text/css") {
|
|
t.Errorf("css content type = %q", ct)
|
|
}
|
|
}
|
|
|
|
func TestOtherStaticFilesAreServedWithoutLongCaching(t *testing.T) {
|
|
rec := fetch(New(site()), "GET", "/favicon.svg")
|
|
if rec.Code != 200 || rec.Body.String() != "<svg/>" {
|
|
t.Fatalf("%d %q", rec.Code, rec.Body.String())
|
|
}
|
|
if strings.Contains(rec.Header().Get("Cache-Control"), "immutable") {
|
|
t.Error("only hashed assets are immutable")
|
|
}
|
|
}
|
|
|
|
func TestClientRoutesFallBackToIndex(t *testing.T) {
|
|
h := New(site())
|
|
for _, p := range []string{"/lists", "/lists/12", "/lists/12/members", "/login"} {
|
|
rec := fetch(h, "GET", p)
|
|
if rec.Code != 200 || !strings.Contains(rec.Body.String(), "id=root") {
|
|
t.Errorf("%s: %d %q", p, rec.Code, rec.Body.String())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestMissingAssetsAre404NotHTML(t *testing.T) {
|
|
h := New(site())
|
|
for _, p := range []string{"/assets/missing.js", "/assets/nope/deeper.css", "/assets/", "/robots.js"} {
|
|
rec := fetch(h, "GET", p)
|
|
if rec.Code != 404 {
|
|
t.Errorf("%s: want 404, got %d", p, rec.Code)
|
|
}
|
|
if strings.Contains(rec.Body.String(), "id=root") {
|
|
t.Errorf("%s: a missing script must not be answered with the HTML shell", p)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNeverAnswersForTheAPI(t *testing.T) {
|
|
h := New(site())
|
|
for _, p := range []string{"/api", "/api/", "/api/v1/lists", "/api/v2/x"} {
|
|
rec := fetch(h, "GET", p)
|
|
if rec.Code != 404 || strings.Contains(rec.Body.String(), "id=root") {
|
|
t.Errorf("%s: %d %q", p, rec.Code, rec.Body.String())
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestOnlyReadMethods(t *testing.T) {
|
|
h := New(site())
|
|
for _, m := range []string{"POST", "PUT", "DELETE", "PATCH"} {
|
|
if rec := fetch(h, m, "/"); rec.Code != 405 {
|
|
t.Errorf("%s: %d", m, rec.Code)
|
|
}
|
|
}
|
|
if rec := fetch(h, "HEAD", "/"); rec.Code != 200 || rec.Body.Len() != 0 {
|
|
t.Errorf("HEAD: %d %q", rec.Code, rec.Body.String())
|
|
}
|
|
}
|
|
|
|
func TestSecurityHeaders(t *testing.T) {
|
|
for _, p := range []string{"/", "/lists/1", "/assets/app-abc123.js"} {
|
|
h := fetch(New(site()), "GET", p).Header()
|
|
csp := h.Get("Content-Security-Policy")
|
|
for _, want := range []string{"default-src 'self'", "frame-ancestors 'none'", "base-uri 'none'", "object-src 'none'"} {
|
|
if !strings.Contains(csp, want) {
|
|
t.Errorf("%s: CSP %q lacks %q", p, csp, want)
|
|
}
|
|
}
|
|
if h.Get("X-Content-Type-Options") != "nosniff" || h.Get("Referrer-Policy") != "no-referrer" || h.Get("X-Frame-Options") != "DENY" {
|
|
t.Errorf("%s: headers %v", p, h)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestTraversalAttemptsAreContained(t *testing.T) {
|
|
fs := site()
|
|
fs["secret.txt"] = &fstest.MapFile{Data: []byte("top secret")}
|
|
h := New(fstest.MapFS{"index.html": fs["index.html"]})
|
|
for _, p := range []string{"/../secret.txt", "/%2e%2e/secret.txt", "/assets/../../secret.txt", "//etc/passwd"} {
|
|
rec := fetch(h, "GET", p)
|
|
if strings.Contains(rec.Body.String(), "top secret") || strings.Contains(rec.Body.String(), "root:") {
|
|
t.Errorf("%s leaked a file", p)
|
|
}
|
|
}
|
|
}
|
|
|
|
func TestNotBuiltAnswers503WithAHint(t *testing.T) {
|
|
rec := fetch(New(fstest.MapFS{".gitkeep": {}}), "GET", "/")
|
|
if rec.Code != 503 || !strings.Contains(rec.Body.String(), "make admin") {
|
|
t.Fatalf("%d %q", rec.Code, rec.Body.String())
|
|
}
|
|
if rec := fetch(New(fstest.MapFS{}), "GET", "/lists"); rec.Code != 503 {
|
|
t.Fatalf("client route without a build: %d", rec.Code)
|
|
}
|
|
}
|
|
|
|
func TestEmbeddedHandlerWorksWithoutABuild(t *testing.T) {
|
|
// The embedded dist holds only .gitkeep in a fresh checkout; it must still compile and answer.
|
|
rec := fetch(Embedded(), "GET", "/")
|
|
if rec.Code != 200 && rec.Code != 503 {
|
|
t.Fatalf("status = %d", rec.Code)
|
|
}
|
|
}
|