http.FileServer zeigt für Pfade mit Schluss-"/" den Verzeichnisinhalt an; GET /static/media/ listete so alle Uploads samt Vorschau- und Cover-Bildern auf. Beide Dateihandler (assets, static) laufen jetzt über serveFiles, das Verzeichnisse mit 404 beantwortet, und Uploads bekommen wie die Assets ein dauerhaftes Cache-Control (ihre Namen sind einmalig). Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
39 lines
1.1 KiB
Go
39 lines
1.1 KiB
Go
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"))
|
|
}
|
|
}
|
|
}
|