package main import ( "net/http" "net/http/httptest" "testing" "testing/fstest" ) func TestStaticFilesCache(t *testing.T) { h := staticFiles(fstest.MapFS{ "index.html": {Data: []byte("
v1
")}, "app.js": {Data: []byte("v1")}, "fonts/mono.woff2": {Data: []byte("font")}, }) get := func(path, etag string) *httptest.ResponseRecorder { req := httptest.NewRequest("GET", path, nil) if etag != "" { req.Header.Set("If-None-Match", etag) } rec := httptest.NewRecorder() h.ServeHTTP(rec, req) return rec } rec := get("/app.js", "") tag := rec.Header().Get("ETag") if rec.Code != http.StatusOK || tag == "" || rec.Header().Get("Cache-Control") != "no-cache" { t.Fatalf("app.js: %d, ETag %q, Cache-Control %q", rec.Code, tag, rec.Header().Get("Cache-Control")) } if rec := get("/app.js", tag); rec.Code != http.StatusNotModified || rec.Body.Len() != 0 { t.Fatalf("revalidation: %d with %d bytes, want 304 without body", rec.Code, rec.Body.Len()) } if rec := get("/app.js", `"old"`); rec.Code != http.StatusOK || rec.Body.String() != "v1" { t.Fatalf("stale ETag: %d %q", rec.Code, rec.Body) } if rec := get("/", ""); rec.Code != http.StatusOK || rec.Header().Get("ETag") == "" || rec.Header().Get("Cache-Control") != "no-cache" { t.Fatalf("index: %d %v", rec.Code, rec.Header()) } if rec := get("/fonts/mono.woff2", ""); rec.Header().Get("Cache-Control") != "public, max-age=2592000" { t.Fatalf("font Cache-Control %q", rec.Header().Get("Cache-Control")) } }