(t *testing.T)
| 66 | } |
| 67 | |
| 68 | func TestAPI_GetSuccess_JSON(t *testing.T) { |
| 69 | out, _ := iostreams.SetForTest(t) |
| 70 | cli, stop := newTestClient(t, func(w http.ResponseWriter, r *http.Request) { |
| 71 | w.Header().Set("Content-Type", "application/json") |
| 72 | w.Header().Set("X-Request-Id", "req-123") |
| 73 | w.WriteHeader(http.StatusOK) |
| 74 | _, _ = w.Write([]byte(`{"value":42}`)) |
| 75 | }) |
| 76 | defer stop() |
| 77 | |
| 78 | if err := runAPI(context.Background(), &Options{}, &cmdutil.FormatOptions{Mode: cmdutil.FormatJSON}, cli, "GET", "/api/v1/foo", false); err != nil { |
| 79 | t.Fatalf("runAPI: %v", err) |
| 80 | } |
| 81 | // v0.7 envelope: {ok:true, data:{status, headers, body}} |
| 82 | var env struct { |
| 83 | OK bool `json:"ok"` |
| 84 | Data struct { |
| 85 | Status int `json:"status"` |
| 86 | Headers map[string]string `json:"headers"` |
| 87 | Body map[string]any `json:"body"` |
| 88 | } `json:"data"` |
| 89 | } |
| 90 | if err := json.Unmarshal(out.Bytes(), &env); err != nil { |
| 91 | t.Fatalf("decode envelope JSON: %v\n%s", err, out.String()) |
| 92 | } |
| 93 | if !env.OK { |
| 94 | t.Errorf("envelope ok: want true, got false") |
| 95 | } |
| 96 | got := env.Data |
| 97 | if got.Status != 200 { |
| 98 | t.Errorf("status: want 200, got %d", got.Status) |
| 99 | } |
| 100 | if got.Headers["Content-Type"] != "application/json" { |
| 101 | t.Errorf("Content-Type header missing: %v", got.Headers) |
| 102 | } |
| 103 | if v, ok := got.Body["value"]; !ok || v.(float64) != 42 { |
| 104 | t.Errorf("body.value: want 42, got %v", got.Body) |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | func TestAPI_PostWithStdinInput(t *testing.T) { |
| 109 | _, _ = iostreams.SetForTest(t) |
nothing calls this directly
no test coverage detected