package api_test import ( "context" "net/http" "net/http/httptest" "strings" "sync/atomic" "testing" "time" ) func TestEvidence(t *testing.T) { e := newEnv(t) ctx, cancel := context.WithCancel(context.Background()) t.Cleanup(cancel) go e.api.RunScheduler(ctx) var healthy atomic.Bool site := httptest.NewTLSServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { if r.URL.Path == "/" { http.Redirect(w, r, "/app", http.StatusFound) return } http.SetCookie(w, &http.Cookie{Name: "sid", Value: "cookie-secret"}) if !healthy.Load() { w.WriteHeader(http.StatusBadGateway) _, _ = w.Write([]byte(` upstream down`)) return } _, _ = w.Write([]byte("all good")) })) defer site.Close() tok, region := e.platformChecker(t, uniq("eu-")) e.startChecker(t, tok) owner := e.browser(t) owner.signup("o@"+uniq("ev")+".test", uniq("ev-")) st, body := owner.do("POST", "/api/v1/org/monitors", map[string]any{ "name": "Shop", "type": "http", "target": site.URL + "/", "interval_seconds": 10, "timeout_ms": 2000, "confirmations": 1, "regions": []string{region}, "config": map[string]any{"verify_tls": false, "headers": map[string]string{"X-Token": "header-secret"}}, }) if st != http.StatusCreated { t.Fatalf("create monitor = %d %v", st, body) } id := body["id"].(string) evidence := func() []any { _, b := owner.do("GET", "/api/v1/org/monitors/"+id+"/evidence", nil) list, _ := b["evidence"].([]any) return list } waitFor(t, 15*time.Second, "evidence of the failure", func() bool { return len(evidence()) > 0 }) first := evidence()[0].(map[string]any) _, detail := owner.do("GET", "/api/v1/org/monitors/"+id+"/evidence/"+first["id"].(string), nil) raw := mustJSON(detail) for _, want := range []string{`"status":302`, `"status":502`, `"not_after"`, `upstream down`} { if !strings.Contains(raw, want) { t.Errorf("evidence lacks %s: %.600s", want, raw) } } steps := detail["evidence"].(map[string]any)["steps"].([]any) cookie := steps[len(steps)-1].(map[string]any)["response_headers"].(map[string]any)["Set-Cookie"].([]any) if len(cookie) != 1 || cookie[0] != "sid=" { t.Errorf("Set-Cookie = %v, want the name only", cookie) } for _, secret := range []string{"cookie-secret", "header-secret"} { if strings.Contains(raw, secret) { t.Errorf("evidence contains %s", secret) } } // The page shows the body as text, never as markup. _, _, page := owner.page("/monitors/" + id + "/evidence/" + first["id"].(string)) if strings.Contains(page, `