pegasus/backend/middleware_test.go

54 lines
1.4 KiB
Go
Raw Permalink Normal View History

2026-04-11 00:02:59 -03:00
package main
import (
"net/http"
"net/http/httptest"
"testing"
"scm.bstein.dev/bstein/Pegasus/backend/internal"
)
func TestRedactHeaders(t *testing.T) {
got := redactHeaders(http.Header{
"Cookie": []string{"session"},
"Authorization": []string{"bearer token"},
"X-Token": []string{"secret"},
"X-Other": []string{"ok"},
})
if got.Get("Cookie") != "<redacted>" {
t.Fatalf("expected cookie to be redacted, got %q", got.Get("Cookie"))
}
if got.Get("Authorization") != "<redacted>" {
t.Fatalf("expected authorization to be redacted, got %q", got.Get("Authorization"))
}
if got.Get("X-Token") != "<redacted>" {
t.Fatalf("expected token header to be redacted, got %q", got.Get("X-Token"))
}
if got.Get("X-Other") != "ok" {
t.Fatalf("expected non-sensitive header to pass through, got %q", got.Get("X-Other"))
}
}
func TestDebugHandlerWrapsHandler(t *testing.T) {
origDebug := internal.Debug
defer func() { internal.Debug = origDebug }()
internal.Debug = true
called := false
h := debugHandler(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
called = true
w.WriteHeader(http.StatusTeapot)
}))
rr := httptest.NewRecorder()
h.ServeHTTP(rr, httptest.NewRequest(http.MethodGet, "/test", nil))
if !called {
t.Fatalf("expected wrapped handler to run")
}
if rr.Code != http.StatusTeapot {
t.Fatalf("unexpected status %d", rr.Code)
}
}