fixed message

This commit is contained in:
2026-01-27 13:14:22 +08:00
parent bd0fdf89f3
commit 6d8a2faf25
2 changed files with 19 additions and 17 deletions
+10 -8
View File
@@ -2,6 +2,7 @@ package handlers
import ( import (
"authorization/db" "authorization/db"
"authorization/helper"
"authorization/models" "authorization/models"
"authorization/redisclient" "authorization/redisclient"
"context" "context"
@@ -18,11 +19,10 @@ import (
// @Success 200 {object} HealthResponse // @Success 200 {object} HealthResponse
// @Router /health [get] // @Router /health [get]
func HealthHandler(w http.ResponseWriter, r *http.Request) { func HealthHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "application/json") response := models.HealthResponse{
w.WriteHeader(http.StatusOK)
json.NewEncoder(w).Encode(models.HealthResponse{
Status: "ok", Status: "ok",
}) }
helper.RespondWithJSON(w, http.StatusOK, response)
} }
// ReadyHandler checks if the service is ready to handle requests // ReadyHandler checks if the service is ready to handle requests
@@ -69,16 +69,18 @@ func ReadyHandler(w http.ResponseWriter, r *http.Request) {
allHealthy = false allHealthy = false
} }
status := "ready" status := "AuthZ Capy!"
statusCode := http.StatusOK statusCode := http.StatusOK
if !allHealthy { if !allHealthy {
status = "not_ready" status = "AuthZ not Capy!"
statusCode = http.StatusServiceUnavailable statusCode = http.StatusServiceUnavailable
} }
w.WriteHeader(statusCode) w.WriteHeader(statusCode)
json.NewEncoder(w).Encode(models.HealthResponse{ if err := json.NewEncoder(w).Encode(models.HealthResponse{
Status: status, Status: status,
Services: services, Services: services,
}) }); err != nil {
helper.LogError(err, "Error encoding health response")
}
} }
+9 -9
View File
@@ -141,8 +141,8 @@ func TestReadyHandlerDBUnhealthy(t *testing.T) {
t.Fatalf(FailedToDecodeResponseMessage, err) t.Fatalf(FailedToDecodeResponseMessage, err)
} }
if healthResp.Status != "not_ready" { if healthResp.Status != "AuthZ not Capy!" {
t.Errorf("status = %v, want not_ready", healthResp.Status) t.Errorf("status = %v, want 'AuthZ not Capy!'", healthResp.Status)
} }
if healthResp.Services["database"] != "unhealthy" { if healthResp.Services["database"] != "unhealthy" {
@@ -181,8 +181,8 @@ func TestReadyHandlerDBNotInitialized(t *testing.T) {
t.Fatalf(FailedToDecodeResponseMessage, err) t.Fatalf(FailedToDecodeResponseMessage, err)
} }
if healthResp.Status != "not_ready" { if healthResp.Status != "AuthZ not Capy!" {
t.Errorf("status = %v, want not_ready", healthResp.Status) t.Errorf("status = %v, want 'AuthZ not Capy!'", healthResp.Status)
} }
if healthResp.Services["database"] != "not_initialized" { if healthResp.Services["database"] != "not_initialized" {
@@ -348,8 +348,8 @@ func TestReadyHandlerBothServicesHealthy(t *testing.T) {
var response models.HealthResponse var response models.HealthResponse
json.NewDecoder(w.Body).Decode(&response) json.NewDecoder(w.Body).Decode(&response)
if response.Status != "ready" { if response.Status != "AuthZ Capy!" {
t.Errorf("Expected status 'ready', got '%s'", response.Status) t.Errorf("Expected status 'AuthZ Capy!', got '%s'", response.Status)
} }
if err := mock.ExpectationsWereMet(); err != nil { if err := mock.ExpectationsWereMet(); err != nil {
@@ -380,9 +380,9 @@ func TestReadyHandlerNilDatabaseAndRedis(t *testing.T) {
t.Fatalf(FailedToDecodeResponseMessage, err) t.Fatalf(FailedToDecodeResponseMessage, err)
} }
// The handler returns "not_ready" when services are down // The handler returns "AuthZ not Capy!" when services are down
if response.Status != "not_ready" && response.Status != "unhealthy" && response.Status != "degraded" { if response.Status != "AuthZ not Capy!" {
t.Errorf("Expected status 'not_ready', 'unhealthy', or 'degraded', got '%s'", response.Status) t.Errorf("Expected status 'AuthZ not Capy!', got '%s'", response.Status)
} }
} }