From 49a087e482b3ed653ffa67605dd6bfd88620640b Mon Sep 17 00:00:00 2001 From: Vipin Singh Date: Tue, 14 May 2024 15:37:48 +1000 Subject: [PATCH] Fixed test function --- handler/webhook.go | 13 +++++++------ main_test.go | 6 ++++-- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/handler/webhook.go b/handler/webhook.go index b2d17af..39e0a5d 100644 --- a/handler/webhook.go +++ b/handler/webhook.go @@ -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 } diff --git a/main_test.go b/main_test.go index c9ecc3d..0f2a650 100644 --- a/main_test.go +++ b/main_test.go @@ -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()