MCPcopy Create free account
hub / github.com/OctopusDeploy/cli / TestApiCommand

Function TestApiCommand

pkg/cmd/api/api_test.go:27–121  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

25}
26
27func 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",

Callers

nothing calls this directly

Calls 11

CloseMethod · 0.95
ExpectRequestMethod · 0.95
ReceiveRequestMethod · 0.95
RespondMethod · 0.95
GoBegin2Function · 0.92
ReceivePairFunction · 0.92
NewMockHttpServerFunction · 0.92
NewMockFactoryFunction · 0.92
respondToSdkInitFunction · 0.85
RespondWithStatusMethod · 0.80
ErrorMethod · 0.45

Tested by

no test coverage detected