Skip to content

Commit 7fac003

Browse files
committed
test against multiple status codes
1 parent 99e396a commit 7fac003

File tree

1 file changed

+38
-27
lines changed

1 file changed

+38
-27
lines changed

cli/support_test.go

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -162,39 +162,50 @@ func TestSupportBundle(t *testing.T) {
162162
})
163163

164164
// This ensures that the CLI does not panic when trying to generate a support bundle
165-
// against a fake server that returns a 200 OK for all requests. This essentially
165+
// against a fake server that returns an empty response for all requests. This essentially
166166
// ensures that (almost) all of the support bundle generating code paths get a zero value.
167167
t.Run("DontPanic", func(t *testing.T) {
168168
t.Parallel()
169169

170-
// Start up a fake server that will return a blank 200 OK response for everything.
171-
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
172-
t.Logf("received request: %s %s", r.Method, r.URL)
173-
switch r.URL.Path {
174-
case "/api/v2/authcheck":
175-
// Fake auth check
176-
resp := codersdk.AuthorizationResponse{
177-
"Read DeploymentValues": true,
178-
}
179-
w.WriteHeader(http.StatusOK)
180-
assert.NoError(t, json.NewEncoder(w).Encode(resp))
181-
default:
182-
// Simply return a 200 OK for everything else.
183-
w.WriteHeader(http.StatusOK)
184-
}
185-
}))
186-
defer srv.Close()
187-
u, err := url.Parse(srv.URL)
188-
require.NoError(t, err)
189-
client := codersdk.New(u)
170+
for _, code := range []int{
171+
http.StatusOK,
172+
http.StatusUnauthorized,
173+
http.StatusForbidden,
174+
http.StatusNotFound,
175+
http.StatusInternalServerError,
176+
} {
177+
t.Run(http.StatusText(code), func(t *testing.T) {
178+
t.Parallel()
179+
// Start up a fake server
180+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
181+
t.Logf("received request: %s %s", r.Method, r.URL)
182+
switch r.URL.Path {
183+
case "/api/v2/authcheck":
184+
// Fake auth check
185+
resp := codersdk.AuthorizationResponse{
186+
"Read DeploymentValues": true,
187+
}
188+
w.WriteHeader(http.StatusOK)
189+
assert.NoError(t, json.NewEncoder(w).Encode(resp))
190+
default:
191+
// Simply return a blank response for everything else.
192+
w.WriteHeader(code)
193+
}
194+
}))
195+
defer srv.Close()
196+
u, err := url.Parse(srv.URL)
197+
require.NoError(t, err)
198+
client := codersdk.New(u)
190199

191-
d := t.TempDir()
192-
path := filepath.Join(d, "bundle.zip")
200+
d := t.TempDir()
201+
path := filepath.Join(d, "bundle.zip")
193202

194-
inv, root := clitest.New(t, "support", "bundle", "--url-override", srv.URL, "--output-file", path, "--yes")
195-
clitest.SetupConfig(t, client, root)
196-
err = inv.Run()
197-
require.NoError(t, err)
203+
inv, root := clitest.New(t, "support", "bundle", "--url-override", srv.URL, "--output-file", path, "--yes")
204+
clitest.SetupConfig(t, client, root)
205+
err = inv.Run()
206+
require.NoError(t, err)
207+
})
208+
}
198209
})
199210
}
200211

0 commit comments

Comments
 (0)