package web import ( "io" "log/slog" "net/http" "net/http/httptest" "strings" "testing" "amadan.net/rastrillo/rastrillo" _ "amadan.net/rastrillo/rastrillo/locales/en" ) // Base runs under rastrillo's default Content-Security-Policy. With Oficina ID // on, connect's middleware replaces it, so formPolicy must be that same policy // with only the issuer added to form-action. func TestFormPolicyIsRastrillosWithTheIssuer(t *testing.T) { out, closeAll, err := rastrillo.Handler(rastrillo.Options{Mux: http.NewServeMux(), Logger: slog.New(slog.NewTextHandler(io.Discard, nil))}) if err != nil { t.Fatal(err) } defer closeAll() w := httptest.NewRecorder() out.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/nothing-here", nil)) def := w.Header().Get("Content-Security-Policy") if !strings.HasSuffix(def, "form-action 'self'") { t.Fatalf("rastrillo's default CSP changed shape: %q", def) } if got, want := formPolicy("https://id.oficina.works"), def+" https://id.oficina.works"; got != want { t.Errorf("formPolicy:\n got %q\nwant %q", got, want) } }