|
| 1 | +package api_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "io" |
| 6 | + "net/http" |
| 7 | + "testing" |
| 8 | + |
| 9 | + apiPkg "github.com/OctopusDeploy/cli/pkg/cmd/api" |
| 10 | + cmdRoot "github.com/OctopusDeploy/cli/pkg/cmd/root" |
| 11 | + "github.com/OctopusDeploy/cli/test/testutil" |
| 12 | + "github.com/spf13/cobra" |
| 13 | + "github.com/stretchr/testify/assert" |
| 14 | +) |
| 15 | + |
| 16 | +func TestApiCommand(t *testing.T) { |
| 17 | + tests := []struct { |
| 18 | + name string |
| 19 | + run func(t *testing.T, api *testutil.MockHttpServer, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) |
| 20 | + }{ |
| 21 | + {"prints pretty-printed JSON response", func(t *testing.T, api *testutil.MockHttpServer, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) { |
| 22 | + cmdReceiver := testutil.GoBegin2(func() (*cobra.Command, error) { |
| 23 | + defer api.Close() |
| 24 | + rootCmd.SetArgs([]string{"api", "/api"}) |
| 25 | + return rootCmd.ExecuteC() |
| 26 | + }) |
| 27 | + |
| 28 | + api.ExpectRequest(t, "GET", "/api").RespondWithStatus(http.StatusOK, "200 OK", map[string]string{ |
| 29 | + "Application": "Octopus Deploy", |
| 30 | + "Version": "2024.1.0", |
| 31 | + }) |
| 32 | + |
| 33 | + _, err := testutil.ReceivePair(cmdReceiver) |
| 34 | + assert.Nil(t, err) |
| 35 | + assert.Contains(t, stdOut.String(), `"Application": "Octopus Deploy"`) |
| 36 | + assert.Contains(t, stdOut.String(), `"Version": "2024.1.0"`) |
| 37 | + }}, |
| 38 | + |
| 39 | + {"prints error response body on non-2xx status", func(t *testing.T, api *testutil.MockHttpServer, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) { |
| 40 | + // Stub os.Exit so the test doesn't terminate the process |
| 41 | + origExit := apiPkg.OsExit |
| 42 | + var exitCode int |
| 43 | + apiPkg.OsExit = func(code int) { exitCode = code } |
| 44 | + defer func() { apiPkg.OsExit = origExit }() |
| 45 | + |
| 46 | + cmdReceiver := testutil.GoBegin2(func() (*cobra.Command, error) { |
| 47 | + defer api.Close() |
| 48 | + rootCmd.SetArgs([]string{"api", "/api/nonexistent"}) |
| 49 | + return rootCmd.ExecuteC() |
| 50 | + }) |
| 51 | + |
| 52 | + api.ExpectRequest(t, "GET", "/api/nonexistent").RespondWithStatus(http.StatusNotFound, "404 Not Found", map[string]string{ |
| 53 | + "ErrorMessage": "Not found", |
| 54 | + }) |
| 55 | + |
| 56 | + _, err := testutil.ReceivePair(cmdReceiver) |
| 57 | + assert.Nil(t, err) |
| 58 | + assert.Equal(t, http.StatusNotFound, exitCode) |
| 59 | + assert.Contains(t, stdOut.String(), `"ErrorMessage": "Not found"`) |
| 60 | + }}, |
| 61 | + |
| 62 | + {"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) { |
| 63 | + cmdReceiver := testutil.GoBegin2(func() (*cobra.Command, error) { |
| 64 | + defer api.Close() |
| 65 | + rootCmd.SetArgs([]string{"api", "/api/health"}) |
| 66 | + return rootCmd.ExecuteC() |
| 67 | + }) |
| 68 | + |
| 69 | + r, _ := api.ReceiveRequest() |
| 70 | + assert.Equal(t, "GET", r.Method) |
| 71 | + assert.Equal(t, "/api/health", r.URL.Path) |
| 72 | + api.Respond(&http.Response{ |
| 73 | + StatusCode: http.StatusOK, |
| 74 | + Status: "200 OK", |
| 75 | + Body: io.NopCloser(bytes.NewReader([]byte("OK"))), |
| 76 | + ContentLength: 2, |
| 77 | + }, nil) |
| 78 | + |
| 79 | + _, err := testutil.ReceivePair(cmdReceiver) |
| 80 | + assert.Nil(t, err) |
| 81 | + assert.Equal(t, "OK", stdOut.String()) |
| 82 | + }}, |
| 83 | + |
| 84 | + {"requires an argument", func(t *testing.T, api *testutil.MockHttpServer, rootCmd *cobra.Command, stdOut *bytes.Buffer, stdErr *bytes.Buffer) { |
| 85 | + defer api.Close() |
| 86 | + rootCmd.SetArgs([]string{"api"}) |
| 87 | + _, err := rootCmd.ExecuteC() |
| 88 | + assert.Error(t, err) |
| 89 | + }}, |
| 90 | + } |
| 91 | + |
| 92 | + for _, test := range tests { |
| 93 | + t.Run(test.name, func(t *testing.T) { |
| 94 | + stdOut, stdErr := &bytes.Buffer{}, &bytes.Buffer{} |
| 95 | + api := testutil.NewMockHttpServer() |
| 96 | + fac := testutil.NewMockFactory(api) |
| 97 | + rootCmd := cmdRoot.NewCmdRoot(fac, nil, nil) |
| 98 | + rootCmd.SetOut(stdOut) |
| 99 | + rootCmd.SetErr(stdErr) |
| 100 | + test.run(t, api, rootCmd, stdOut, stdErr) |
| 101 | + }) |
| 102 | + } |
| 103 | +} |
0 commit comments