Skip to content

Commit

Permalink
fix: guard or assert nil checks on http responses (#130)
Browse files Browse the repository at this point in the history
Signed-off-by: Chris Gianelloni <[email protected]>
  • Loading branch information
wolf31o2 authored Jan 20, 2025
1 parent 7018bea commit aae59a9
Showing 1 changed file with 12 additions and 0 deletions.
12 changes: 12 additions & 0 deletions internal/api/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,9 @@ func waitForServer(apiBaseURL string) {

for i := 0; i < maxRetries; i++ {
resp, err := http.Get(fmt.Sprintf("%s/healthcheck", apiBaseURL))
if resp == nil {
continue
}
if err == nil && resp.StatusCode == http.StatusOK {
return // Server is ready
}
Expand All @@ -141,6 +144,9 @@ func TestMetricsEndpoint(t *testing.T) {

// Test the /metrics endpoint
resp, err := http.Get(fmt.Sprintf("%s/metrics", metricsBaseURL))
if resp == nil {
t.Fatal("failed to get response from metrics endpoint")
}
if err != nil {
t.Fatalf("failed to call metrics endpoint: %v", err)
}
Expand Down Expand Up @@ -281,6 +287,7 @@ func TestWalletCreateIncrementsCounter(t *testing.T) {

// Fetch the metrics once to get the current count
resp, err := http.Get(fmt.Sprintf("%s/metrics", metricsBaseURL))
assert.NotEqual(t, resp, nil)
assert.NoError(t, err, "failed to call initial metrics endpoint")
assert.Equal(t, http.StatusOK, resp.StatusCode)

Expand All @@ -297,13 +304,15 @@ func TestWalletCreateIncrementsCounter(t *testing.T) {
createWalletResp, err := http.Get(
fmt.Sprintf("%s/api/wallet/create", apiBaseURL),
)
assert.NotEqual(t, createWalletResp, nil)
assert.NoError(t, err, "failed to call /api/wallet/create endpoint")
assert.Equal(t, http.StatusOK, createWalletResp.StatusCode,
"expected /api/wallet/create to return 200 on success")
createWalletResp.Body.Close()

// Fetch the metrics again
resp2, err := http.Get(fmt.Sprintf("%s/metrics", metricsBaseURL))
assert.NotEqual(t, resp2, nil)
assert.NoError(t, err, "failed to call second metrics endpoint")
assert.Equal(t, http.StatusOK, resp2.StatusCode)

Expand All @@ -328,6 +337,9 @@ func TestCreateWalletReturnsMnemonic(t *testing.T) {
defer cleanup()

resp, err := http.Get(fmt.Sprintf("%s/api/wallet/create", apiBaseURL))
if resp == nil {
t.Fatal("Failed calling /api/wallet/create")
}
if err != nil {
t.Fatalf("Failed to call /api/wallet/create: %v", err)
}
Expand Down

0 comments on commit aae59a9

Please sign in to comment.