package surf import ( "net/http" "net/http/httptest" "strings" "testing" "git.golem15.com/golem15/summercms/modules/pact" "git.golem15.com/golem15/summercms/modules/party" ) const chromeAccept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8" // TestErrorPageTracer: a browser navigation to an unmatched path gets the // branded 404 through surf.Assemble; curl keeps the stdlib text. func TestErrorPageTracer(t *testing.T) { app := transportApp(t, "production", map[string]string{"http.yaml": transportBodyLimits}) plugin := routesPluginFunc{id: "acme.ping", routes: func(r pact.Router) { r.Get("/api/ping", func(w http.ResponseWriter, _ *http.Request) { _, _ = w.Write([]byte("pong")) }) }} h, err := Assemble(app, []party.Plugin{plugin}) if err != nil { t.Fatal(err) } req := httptest.NewRequest(http.MethodGet, "/nowhere", nil) req.Header.Set("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8") req.Header.Set("Sec-Fetch-Mode", "navigate") rec := httptest.NewRecorder() h.ServeHTTP(rec, req) if rec.Code != http.StatusNotFound { t.Fatalf("browser status = %d, want 404", rec.Code) } if got := rec.Header().Get("Content-Type"); got != "text/html; charset=utf-8" { t.Fatalf("browser Content-Type = %q", got) } if got := rec.Header().Get("Cache-Control"); got != "no-store" { t.Fatalf("Cache-Control = %q", got) } if got := rec.Header().Get("Content-Security-Policy"); !strings.HasPrefix(got, "default-src 'none'") { t.Fatalf("Content-Security-Policy = %q", got) } body := rec.Body.String() for _, want := range []string{"404", `href="/"`, "data:image/webp;base64,"} { if !strings.Contains(body, want) { t.Fatalf("browser body lacks %q:\n%s", want, body) } } if strings.Contains(body, "