Skip to content

Commit

Permalink
Fixed test function
Browse files Browse the repository at this point in the history
Vipin Singh committed May 14, 2024
1 parent 63b06e8 commit 49a087e
Showing 2 changed files with 11 additions and 8 deletions.
13 changes: 7 additions & 6 deletions handler/webhook.go
Original file line number Diff line number Diff line change
@@ -79,14 +79,15 @@ func (h *WebhookHandler) DBHealthHandler(w http.ResponseWriter, r *http.Request)
return NewAPIError( http.StatusMethodNotAllowed, fmt.Errorf("method not allowed"), "Only GET allowed, using wrong method TYPE")
}

if err := h.db.DescribeTable(h.tableNames[0]); err != nil {
return NewAPIError( http.StatusInternalServerError, err, "Database is not healthy:unable to describe table")
}
tableName := h.tableNames[0]
err := h.db.DescribeTable(tableName)
if err != nil {
logRequestEnd(startTime, method, url, handlerName, http.StatusInternalServerError)
return NewAPIError(http.StatusInternalServerError, err, "Database is unhealthy")
}

// Write the result to the response
logRequestEnd(startTime, method, url, handlerName, http.StatusOK)
logRequestEnd(startTime, method, url, handlerName, http.StatusOK)
writeJSON(w, http.StatusOK, map[string]string{"message": "Database is healthy"})

return nil
}

6 changes: 4 additions & 2 deletions main_test.go
Original file line number Diff line number Diff line change
@@ -287,11 +287,13 @@ func TestDBHealthHandlerFail(t *testing.T) {
tableNames := []string{"EventWebhook"}
db.On("DescribeTable", tableNames[0]).Return(errors.New("database error")) // Simulate an unhealthy database

handler := handler.NewWebhookHandler(db,tableNames)
h := handler.NewWebhookHandler(db,tableNames)
handlerFunc := handler.Make(h.DBHealthHandler)

req := httptest.NewRequest("GET", "/dbhealth", nil)
w := httptest.NewRecorder()

handler.DBHealthHandler(w, req)
handlerFunc(w, req)

res := w.Result()
defer res.Body.Close()

0 comments on commit 49a087e

Please sign in to comment.