From 6d8a2faf2577873c53b30a4183c3cf4e2211e803 Mon Sep 17 00:00:00 2001 From: F04C Date: Tue, 27 Jan 2026 13:14:22 +0800 Subject: [PATCH] fixed message --- handlers/health.go | 18 ++++++++++-------- handlers/health_test.go | 18 +++++++++--------- 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/handlers/health.go b/handlers/health.go index fa4c026..36d38c5 100644 --- a/handlers/health.go +++ b/handlers/health.go @@ -2,6 +2,7 @@ package handlers import ( "authorization/db" + "authorization/helper" "authorization/models" "authorization/redisclient" "context" @@ -18,11 +19,10 @@ import ( // @Success 200 {object} HealthResponse // @Router /health [get] func HealthHandler(w http.ResponseWriter, r *http.Request) { - w.Header().Set("Content-Type", "application/json") - w.WriteHeader(http.StatusOK) - json.NewEncoder(w).Encode(models.HealthResponse{ + response := models.HealthResponse{ Status: "ok", - }) + } + helper.RespondWithJSON(w, http.StatusOK, response) } // ReadyHandler checks if the service is ready to handle requests @@ -69,16 +69,18 @@ func ReadyHandler(w http.ResponseWriter, r *http.Request) { allHealthy = false } - status := "ready" + status := "AuthZ Capy!" statusCode := http.StatusOK if !allHealthy { - status = "not_ready" + status = "AuthZ not Capy!" statusCode = http.StatusServiceUnavailable } w.WriteHeader(statusCode) - json.NewEncoder(w).Encode(models.HealthResponse{ + if err := json.NewEncoder(w).Encode(models.HealthResponse{ Status: status, Services: services, - }) + }); err != nil { + helper.LogError(err, "Error encoding health response") + } } diff --git a/handlers/health_test.go b/handlers/health_test.go index 8450645..66061be 100644 --- a/handlers/health_test.go +++ b/handlers/health_test.go @@ -141,8 +141,8 @@ func TestReadyHandlerDBUnhealthy(t *testing.T) { t.Fatalf(FailedToDecodeResponseMessage, err) } - if healthResp.Status != "not_ready" { - t.Errorf("status = %v, want not_ready", healthResp.Status) + if healthResp.Status != "AuthZ not Capy!" { + t.Errorf("status = %v, want 'AuthZ not Capy!'", healthResp.Status) } if healthResp.Services["database"] != "unhealthy" { @@ -181,8 +181,8 @@ func TestReadyHandlerDBNotInitialized(t *testing.T) { t.Fatalf(FailedToDecodeResponseMessage, err) } - if healthResp.Status != "not_ready" { - t.Errorf("status = %v, want not_ready", healthResp.Status) + if healthResp.Status != "AuthZ not Capy!" { + t.Errorf("status = %v, want 'AuthZ not Capy!'", healthResp.Status) } if healthResp.Services["database"] != "not_initialized" { @@ -348,8 +348,8 @@ func TestReadyHandlerBothServicesHealthy(t *testing.T) { var response models.HealthResponse json.NewDecoder(w.Body).Decode(&response) - if response.Status != "ready" { - t.Errorf("Expected status 'ready', got '%s'", response.Status) + if response.Status != "AuthZ Capy!" { + t.Errorf("Expected status 'AuthZ Capy!', got '%s'", response.Status) } if err := mock.ExpectationsWereMet(); err != nil { @@ -380,9 +380,9 @@ func TestReadyHandlerNilDatabaseAndRedis(t *testing.T) { t.Fatalf(FailedToDecodeResponseMessage, err) } - // The handler returns "not_ready" when services are down - if response.Status != "not_ready" && response.Status != "unhealthy" && response.Status != "degraded" { - t.Errorf("Expected status 'not_ready', 'unhealthy', or 'degraded', got '%s'", response.Status) + // The handler returns "AuthZ not Capy!" when services are down + if response.Status != "AuthZ not Capy!" { + t.Errorf("Expected status 'AuthZ not Capy!', got '%s'", response.Status) } }