Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cmd/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@ var apiCmd = &cobra.Command{
Short: "Make a raw API request to the given Equinix API path",
Long: `Make a raw authenticated request to the Equinix API using your configured credentials.`,
Args: cobra.ExactArgs(1),
PreRunE: func(_ *cobra.Command, _ []string) error {
switch format {
case "json", "yaml":
return nil
}
return fmt.Errorf("invalid --format %q: must be json or yaml", format)
},
Run: func(_ *cobra.Command, args []string) {
path := args[0]

Expand Down
36 changes: 36 additions & 0 deletions cmd/api_output_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,39 @@
t.Errorf("output = %q, want %q", out, "OK\n")
}
}

// An unknown --format used to make the request and print a blank line.
func TestE2E_APIRejectsUnknownFormat(t *testing.T) {
var requests int
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {

Check failure on line 72 in cmd/api_output_test.go

View workflow job for this annotation

GitHub Actions / lint

unused-parameter: parameter 'r' seems to be unused, consider removing or renaming it as _ (revive)
requests++
w.Header().Set("Content-Type", "application/json")
_ = json.NewEncoder(w).Encode(map[string]string{
"access_token": "fake-token",
"token_timeout": "3600",
"token_type": "Bearer",
"user_name": "fake-user",
"id": "abc",
})
}))
t.Cleanup(srv.Close)
target, err := url.Parse(srv.URL)
if err != nil {
t.Fatal(err)
}
orig := http.DefaultTransport
http.DefaultTransport = redirectTransport{target: target, base: orig}
t.Cleanup(func() { http.DefaultTransport = orig })
t.Cleanup(func() { format = "json" })

out, err := runCLI(t, "api", "fabric/v4/ports", "--format", "xml")
if err == nil {
t.Fatalf("command succeeded with --format xml, output %q", out)
}
if want := `invalid --format "xml": must be json or yaml`; err.Error() != want {
t.Errorf("error = %q, want %q", err, want)
}
if requests != 0 {
t.Errorf("made %d API requests, want none", requests)
}
}
Loading