package server import ( "io" "log/slog" "net/http" "net/http/httptest" "testing" "testing/fstest" ) // Static files carry an ETag, so a browser's revalidation of an unchanged // file is a 304 without a body. func TestStaticETags(t *testing.T) { web := fstest.MapFS{ "index.html": {Data: []byte("Jarl")}, "assets/logo.js": {Data: []byte("export const x = 1;\n")}, } s := &Server{cfg: Config{}, log: slog.New(slog.NewTextHandler(io.Discard, nil)), mux: http.NewServeMux(), limiter: newRateLimiter(), web: web} s.routes() for _, path := range []string{"/", "/assets/logo.js"} { w := httptest.NewRecorder() s.mux.ServeHTTP(w, httptest.NewRequest("GET", path, nil)) tag := w.Header().Get("ETag") if w.Code != 200 || tag == "" || w.Body.Len() == 0 { t.Fatalf("%s: %d etag %q", path, w.Code, tag) } r := httptest.NewRequest("GET", path, nil) r.Header.Set("If-None-Match", tag) w = httptest.NewRecorder() s.mux.ServeHTTP(w, r) if w.Code != http.StatusNotModified || w.Body.Len() != 0 { t.Errorf("%s revalidated: %d, %d bytes", path, w.Code, w.Body.Len()) } } }