(t *testing.T)
| 25 | } |
| 26 | |
| 27 | func TestApiCommand(t *testing.T) { |
| 28 | tests := []struct { |
| 29 | name string |
| 30 | run func(t *testing.T, api *testutil.MockHttpServer, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) |
| 31 | }{ |
| 32 | {"prints pretty-printed JSON response", func(t *testing.T, api *testutil.MockHttpServer, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) { |
| 33 | cmdReceiver := testutil.GoBegin2(func() (*cobra.Command, error) { |
| 34 | defer api.Close() |
| 35 | rootCmd.SetArgs([]string{"api", "/api"}) |
| 36 | return rootCmd.ExecuteC() |
| 37 | }) |
| 38 | |
| 39 | respondToSdkInit(t, api) |
| 40 | |
| 41 | api.ExpectRequest(t, "GET", "/api").RespondWithStatus(http.StatusOK, "200 OK", map[string]string{ |
| 42 | "Application": "Octopus Deploy", |
| 43 | "Version": "2024.1.0", |
| 44 | }) |
| 45 | |
| 46 | _, err := testutil.ReceivePair(cmdReceiver) |
| 47 | assert.Nil(t, err) |
| 48 | assert.Contains(t, stdOut.String(), `"Application": "Octopus Deploy"`) |
| 49 | assert.Contains(t, stdOut.String(), `"Version": "2024.1.0"`) |
| 50 | }}, |
| 51 | |
| 52 | {"prints error response body on non-2xx status", func(t *testing.T, api *testutil.MockHttpServer, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) { |
| 53 | cmdReceiver := testutil.GoBegin2(func() (*cobra.Command, error) { |
| 54 | defer api.Close() |
| 55 | rootCmd.SetArgs([]string{"api", "/api/nonexistent"}) |
| 56 | return rootCmd.ExecuteC() |
| 57 | }) |
| 58 | |
| 59 | respondToSdkInit(t, api) |
| 60 | |
| 61 | api.ExpectRequest(t, "GET", "/api/nonexistent").RespondWithStatus(http.StatusNotFound, "404 Not Found", map[string]string{ |
| 62 | "ErrorMessage": "Not found", |
| 63 | }) |
| 64 | |
| 65 | _, err := testutil.ReceivePair(cmdReceiver) |
| 66 | assert.NotNil(t, err) |
| 67 | assert.Contains(t, err.Error(), "Not found") |
| 68 | }}, |
| 69 | |
| 70 | {"outputs raw body when response is not valid JSON", func(t *testing.T, api *testutil.MockHttpServer, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) { |
| 71 | cmdReceiver := testutil.GoBegin2(func() (*cobra.Command, error) { |
| 72 | defer api.Close() |
| 73 | rootCmd.SetArgs([]string{"api", "/api/health"}) |
| 74 | return rootCmd.ExecuteC() |
| 75 | }) |
| 76 | |
| 77 | respondToSdkInit(t, api) |
| 78 | |
| 79 | r, _ := api.ReceiveRequest() |
| 80 | assert.Equal(t, "GET", r.Method) |
| 81 | assert.Equal(t, "/api/health", r.URL.Path) |
| 82 | api.Respond(&http.Response{ |
| 83 | StatusCode: http.StatusOK, |
| 84 | Status: "200 OK", |
nothing calls this directly
no test coverage detected