Skip to content

High-performance browser automation bridge and multi-instance orchestrator with advanced stealth injection and real-time dashboard.

fix(cli): cookies set/clear print the response body once via quiet request variants (PIN-406)

Luigi Agostifbd2aa82a851
diff --git a/internal/cli/actions/actions_cookies.go b/internal/cli/actions/actions_cookies.go
index a1959d36..bd59a1e8 100644
--- a/internal/cli/actions/actions_cookies.go
+++ b/internal/cli/actions/actions_cookies.go
@@ -57,7 +57,7 @@ func CookiesSet(client *http.Client, base, token string, cmd *cobra.Command, nam
body["url"] = target
}
- result := requireMap(apiclient.DoPost(client, base, token, "/cookies", body), 1, "Failed to set cookie")
+ result := requireMap(apiclient.DoPostQuiet(client, base, token, "/cookies", body), 1, "Failed to set cookie")
if !cookieWriteConfirmed(result) {
fmt.Fprintf(os.Stderr, "ERROR: cookies: %q was not set (%v)\n", name, jsonLine(result))
os.Exit(2)
@@ -67,7 +67,7 @@ func CookiesSet(client *http.Client, base, token string, cmd *cobra.Command, nam
// CookiesClear clears all browser cookies.
func CookiesClear(client *http.Client, base, token string, cmd *cobra.Command) {
- result := apiclient.DoDelete(client, base, token, "/cookies", nil)
+ result := apiclient.DoDeleteQuiet(client, base, token, "/cookies", nil)
if result == nil {
fmt.Fprintln(os.Stderr, "ERROR: cookies: clear failed")
os.Exit(2)
diff --git a/internal/cli/actions/actions_cookies_test.go b/internal/cli/actions/actions_cookies_test.go
index bf1e832a..4e5cc6aa 100644
--- a/internal/cli/actions/actions_cookies_test.go
+++ b/internal/cli/actions/actions_cookies_test.go
@@ -2,6 +2,7 @@ package actions
import (
"encoding/json"
+ "io"
"net/http"
"net/http/httptest"
"strings"
@@ -151,6 +152,92 @@ func TestCookiesSetForwardsOnlyTheFlagsGiven(t *testing.T) {
}
}
+// countJSONObjects reports how many top-level JSON objects are concatenated in s.
+// A double-print emits two, which is what breaks `jq` / `json.tool` on the pipe.
+func countJSONObjects(t *testing.T, s string) int {
+ t.Helper()
+ dec := json.NewDecoder(strings.NewReader(s))
+ n := 0
+ for {
+ var v map[string]any
+ err := dec.Decode(&v)
+ if err == io.EOF {
+ return n
+ }
+ if err != nil {
+ t.Fatalf("stdout is not clean JSON (%v): %q", err, s)
+ }
+ n++
+ }
+}
+
+// cookieWriteServer answers POST and DELETE /cookies with the confirmation bodies
+// the two verbs render, so the test observes exactly what reaches stdout.
+func cookieWriteServer(t *testing.T) *httptest.Server {
+ t.Helper()
+ return httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ switch r.Method {
+ case http.MethodPost:
+ _, _ = io.Copy(io.Discard, r.Body)
+ _ = json.NewEncoder(w).Encode(map[string]any{"set": 1, "failed": 0, "total": 1})
+ case http.MethodDelete:
+ _ = json.NewEncoder(w).Encode(map[string]any{"status": "cleared"})
+ default:
+ http.Error(w, "unexpected method", http.StatusMethodNotAllowed)
+ }
+ }))
+}
+
+// The regression: set/clear printed the response body twice (the auto-rendering
+// helper AND printCookiesResult), so --json emitted two objects and terse printed
+// the JSON before its status line. Each path must now emit exactly one thing.
+func TestCookiesSetAndClearPrintExactlyOnce(t *testing.T) {
+ srv := cookieWriteServer(t)
+ defer srv.Close()
+
+ t.Run("set --json is one object", func(t *testing.T) {
+ out := captureStdout(t, func() {
+ CookiesSet(http.DefaultClient, srv.URL, "", newCookiesTestCmd("--json"), "z1", "v1")
+ })
+ if n := countJSONObjects(t, out); n != 1 {
+ t.Errorf("set --json emitted %d JSON objects, want exactly 1: %q", n, out)
+ }
+ })
+
+ t.Run("set terse is only the status line", func(t *testing.T) {
+ out := captureStdout(t, func() {
+ CookiesSet(http.DefaultClient, srv.URL, "", newCookiesTestCmd(), "z2", "v2")
+ })
+ if strings.Contains(out, "{") {
+ t.Errorf("terse set printed a JSON body before its status line: %q", out)
+ }
+ if strings.TrimSpace(out) != "OK" {
+ t.Errorf("terse set printed %q, want only OK", out)
+ }
+ })
+
+ t.Run("clear --json is one object", func(t *testing.T) {
+ out := captureStdout(t, func() {
+ CookiesClear(http.DefaultClient, srv.URL, "", newCookiesTestCmd("--json"))
+ })
+ if n := countJSONObjects(t, out); n != 1 {
+ t.Errorf("clear --json emitted %d JSON objects, want exactly 1: %q", n, out)
+ }
+ })
+
+ t.Run("clear terse is only the status line", func(t *testing.T) {
+ out := captureStdout(t, func() {
+ CookiesClear(http.DefaultClient, srv.URL, "", newCookiesTestCmd())
+ })
+ if strings.Contains(out, "{") {
+ t.Errorf("terse clear printed a JSON body before its status line: %q", out)
+ }
+ if strings.TrimSpace(out) != "OK" {
+ t.Errorf("terse clear printed %q, want only OK", out)
+ }
+ })
+}
+
// The confirmation must fail CLOSED. A response that cannot say how many cookies were
// stored has not confirmed the write, and a check written as "if the field is readable AND
// looks bad" silently retires itself the moment the field is renamed.
diff --git a/internal/cli/apiclient/client.go b/internal/cli/apiclient/client.go
index 4dc7288f..3a392a58 100644
--- a/internal/cli/apiclient/client.go
+++ b/internal/cli/apiclient/client.go
@@ -271,6 +271,20 @@ func DoDeleteJSON(client *http.Client, base, token, path string, body map[string
return doAndRender(client, token, request{method: "DELETE", url: base + path, body: body})
}
+// DoDeleteQuiet is like DoDelete but does not print the response body, so a
+// self-rendering caller emits its own output exactly once. It keeps DoDelete's
+// fatal-on-transport-error + exit-on-HTTP-error policy; a non-object body
+// returns a nil map for the caller to branch on.
+func DoDeleteQuiet(client *http.Client, base, token, path string, params url.Values) map[string]any {
+ r := request{method: "DELETE", url: buildURL(base, path, params)}
+ status, body := mustRequest(client, token, r)
+ exitOnAPIError(r, status, body)
+
+ var result map[string]any
+ _ = json.Unmarshal(body, &result)
+ return result
+}
+
// ResolveInstanceBase fetches the named instance from the orchestrator and returns
// a base URL pointing directly at that instance's API port.
func ResolveInstanceBase(orchBase, token, instanceID, bind string) string {