From 13993926da7e27c34b5d31b9607b8883d3db2f87 Mon Sep 17 00:00:00 2001
From: ale <ale@incal.net>
Date: Fri, 1 Sep 2023 18:01:48 +0100
Subject: [PATCH] Always dump browser state on http probe errors

---
 probes/http/http_probe.go |  1 +
 protocol/http/http.go     | 10 ++++++++++
 protocol/http/step.go     |  2 --
 3 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/probes/http/http_probe.go b/probes/http/http_probe.go
index 2288827..e18b078 100644
--- a/probes/http/http_probe.go
+++ b/probes/http/http_probe.go
@@ -61,6 +61,7 @@ func (p *httpProbe) RunProbe(ctx context.Context, debug *log.Logger) error {
 		if err := http.CheckStep(ctx, browser, step, debug); err != nil {
 			err = fmt.Errorf("step %s failed: %w", stepName, err)
 			debug.Printf("%v", err)
+			browser.Dump(debug)
 			return err
 		}
 	}
diff --git a/protocol/http/http.go b/protocol/http/http.go
index 9839297..9814ce2 100644
--- a/protocol/http/http.go
+++ b/protocol/http/http.go
@@ -225,3 +225,13 @@ func (b *Browser) SubmitForm(ctx context.Context, formSelector string, moreValue
 		Args: values,
 	})
 }
+
+func (b *Browser) Dump(l *log.Logger) {
+	if b.lastResponse == nil {
+		return
+	}
+
+	l.Printf("[browser state] URL=%s", b.lastResponse.URL.String())
+	l.Printf("[browser state] status=%d", b.lastResponse.StatusCode)
+	l.Printf("[browser state] body:\n%s", b.lastResponse.Body)
+}
diff --git a/protocol/http/step.go b/protocol/http/step.go
index 1e1d7be..09c2db8 100644
--- a/protocol/http/step.go
+++ b/protocol/http/step.go
@@ -61,11 +61,9 @@ func CheckStep(ctx context.Context, b *Browser, step *Step, debug *log.Logger) e
 	}
 
 	if step.ExpectedURL.IsSet() && !step.ExpectedURL.MatchString(resp.URL.String()) {
-		debug.Printf("response body:\n%s", resp.Body)
 		return fmt.Errorf("expected URL %s, got %s instead", step.ExpectedURL.String(), resp.URL.String())
 	}
 	if step.ExpectedData.IsSet() && !step.ExpectedData.MatchString(resp.Body) {
-		debug.Printf("response body:\n%s", resp.Body)
 		return fmt.Errorf("page body at %s does not match expected regexp %s", resp.URL.String(), step.ExpectedData.String())
 	}
 	return nil
-- 
GitLab