package httpapi import ( "net/http" "net/http/httptest" "os" "path/filepath" "testing" ) // TestServeFilesNoListing: Dateien kommen durch, Verzeichnisse liefern 404 // statt einer Liste ihres Inhalts. func TestServeFilesNoListing(t *testing.T) { t.Parallel() dir := t.TempDir() if err := os.MkdirAll(filepath.Join(dir, "media"), 0o755); err != nil { t.Fatal(err) } if err := os.WriteFile(filepath.Join(dir, "media", "a.jpg"), []byte("x"), 0o644); err != nil { t.Fatal(err) } h := http.StripPrefix("/static/", serveFiles(dir, immutable)) for path, want := range map[string]int{ "/static/media/a.jpg": http.StatusOK, "/static/media/": http.StatusNotFound, "/static/": http.StatusNotFound, "/static/fehlt.jpg": http.StatusNotFound, } { rr := httptest.NewRecorder() h.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, path, nil)) if rr.Code != want { t.Errorf("GET %s: %d, erwartet %d", path, rr.Code, want) } if rr.Code == http.StatusOK && rr.Header().Get("Cache-Control") != immutable { t.Errorf("GET %s: Cache-Control %q", path, rr.Header().Get("Cache-Control")) } } }