handlers: a directory under /s/ answers 404, not a listing (closes #286)
check / check (push) Waiting to run
check / check (push) Waiting to run
http.FileServer answered /s/ and /s/css/ with an HTML listing of the embedded files, and /s/css with a redirect to that listing. HandleStatic in internal/handlers now looks the name up in the embedded files first and answers 404 when it names a directory; files are served as before. The routes mount it under /s. Model: opus-5-5
This commit is contained in:
@@ -8,8 +8,6 @@ import (
|
||||
"github.com/go-chi/chi/v5"
|
||||
chimw "github.com/go-chi/chi/v5/middleware"
|
||||
"github.com/prometheus/client_golang/prometheus/promhttp"
|
||||
|
||||
"sneak.berlin/go/dnswatcher/static"
|
||||
)
|
||||
|
||||
// requestTimeout is the maximum duration for handling a request.
|
||||
@@ -47,13 +45,7 @@ func (s *Server) SetupRoutes() {
|
||||
public.Get("/", s.handlers.HandleDashboard())
|
||||
|
||||
// Static assets (embedded CSS/JS)
|
||||
public.Mount(
|
||||
"/s",
|
||||
http.StripPrefix(
|
||||
"/s",
|
||||
http.FileServer(http.FS(static.Static)),
|
||||
),
|
||||
)
|
||||
public.Mount("/s", http.StripPrefix("/s", s.handlers.HandleStatic()))
|
||||
|
||||
// Health check (standard well-known path)
|
||||
public.Get(
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package server_test
|
||||
|
||||
import (
|
||||
"mime"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
@@ -288,3 +289,42 @@ func TestMetricsRateLimitComesBeforeAuth(t *testing.T) {
|
||||
t.Errorf("another address: status = %d, want 200", rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
// TestStaticDirectoriesAnswerNotFound checks that a request naming a
|
||||
// directory of the static assets, with or without the trailing slash,
|
||||
// answers 404 rather than a listing of it or a redirect to one, and
|
||||
// that the stylesheet is still served as CSS.
|
||||
func TestStaticDirectoriesAnswerNotFound(t *testing.T) {
|
||||
viper.Reset()
|
||||
t.Setenv("DNSWATCHER_TARGETS", "example.com")
|
||||
|
||||
srv := routedServer(t)
|
||||
|
||||
for _, path := range []string{"/s", "/s/", "/s/css", "/s/css/"} {
|
||||
rec := serve(srv, httptest.NewRequestWithContext(
|
||||
t.Context(), http.MethodGet, path, nil,
|
||||
))
|
||||
|
||||
if rec.Code != http.StatusNotFound {
|
||||
t.Errorf("GET %s: status = %d, want 404", path, rec.Code)
|
||||
}
|
||||
}
|
||||
|
||||
rec := serve(srv, httptest.NewRequestWithContext(
|
||||
t.Context(), http.MethodGet, "/s/css/tailwind.min.css", nil,
|
||||
))
|
||||
|
||||
if rec.Code != http.StatusOK {
|
||||
t.Errorf("GET stylesheet: status = %d, want 200", rec.Code)
|
||||
}
|
||||
|
||||
contentType := rec.Header().Get("Content-Type")
|
||||
|
||||
mediaType, _, err := mime.ParseMediaType(contentType)
|
||||
if err != nil || mediaType != "text/css" {
|
||||
t.Errorf(
|
||||
"GET stylesheet: Content-Type = %q, want text/css",
|
||||
contentType,
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user