fixed sonarqube issues
This commit is contained in:
+26
-26
@@ -27,9 +27,9 @@ func TestInitAuthService(t *testing.T) {
|
||||
t.Log("InitAuthService completed successfully")
|
||||
}
|
||||
|
||||
func TestAuthorizeHandler_NoJWTClaims(t *testing.T) {
|
||||
func TestAuthorizeHandlerNoJWTClaims(t *testing.T) {
|
||||
// Setup
|
||||
req := httptest.NewRequest("POST", "/v1/auth/check", nil)
|
||||
req := httptest.NewRequest("POST", AuthCheckEndpoint, nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
// Execute
|
||||
@@ -37,11 +37,11 @@ func TestAuthorizeHandler_NoJWTClaims(t *testing.T) {
|
||||
|
||||
// Assert
|
||||
if w.Code != http.StatusUnauthorized {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusUnauthorized, w.Code)
|
||||
t.Errorf(ExpectedStatusMessage, http.StatusUnauthorized, w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthorizeHandler_InvalidJSON(t *testing.T) {
|
||||
func TestAuthorizeHandlerInvalidJSON(t *testing.T) {
|
||||
// Setup - no need to init service, we're testing JSON parsing before auth
|
||||
claims := &models.Claims{
|
||||
UserID: "user123",
|
||||
@@ -49,7 +49,7 @@ func TestAuthorizeHandler_InvalidJSON(t *testing.T) {
|
||||
Role: "admin",
|
||||
}
|
||||
|
||||
req := httptest.NewRequest("POST", "/v1/auth/check", bytes.NewBufferString("invalid json"))
|
||||
req := httptest.NewRequest("POST", AuthCheckEndpoint, bytes.NewBufferString("invalid json"))
|
||||
ctx := context.WithValue(req.Context(), models.ContextKey("claims"), claims)
|
||||
req = req.WithContext(ctx)
|
||||
w := httptest.NewRecorder()
|
||||
@@ -59,11 +59,11 @@ func TestAuthorizeHandler_InvalidJSON(t *testing.T) {
|
||||
|
||||
// Assert
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusBadRequest, w.Code)
|
||||
t.Errorf(ExpectedStatusMessage, http.StatusBadRequest, w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthorizeHandler_MissingRequiredFields(t *testing.T) {
|
||||
func TestAuthorizeHandlerMissingRequiredFields(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
payload models.AuthorizationContext
|
||||
@@ -91,7 +91,7 @@ func TestAuthorizeHandler_MissingRequiredFields(t *testing.T) {
|
||||
}
|
||||
|
||||
body, _ := json.Marshal(tc.payload)
|
||||
req := httptest.NewRequest("POST", "/v1/auth/check", bytes.NewBuffer(body))
|
||||
req := httptest.NewRequest("POST", AuthCheckEndpoint, bytes.NewBuffer(body))
|
||||
ctx := context.WithValue(req.Context(), models.ContextKey("claims"), claims)
|
||||
req = req.WithContext(ctx)
|
||||
w := httptest.NewRecorder()
|
||||
@@ -99,13 +99,13 @@ func TestAuthorizeHandler_MissingRequiredFields(t *testing.T) {
|
||||
AuthorizeHandler(w, req)
|
||||
|
||||
if w.Code != http.StatusBadRequest {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusBadRequest, w.Code)
|
||||
t.Errorf(ExpectedStatusMessage, http.StatusBadRequest, w.Code)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthorizeHandler_UserIDMismatch(t *testing.T) {
|
||||
func TestAuthorizeHandlerUserIDMismatch(t *testing.T) {
|
||||
// Setup
|
||||
claims := &models.Claims{
|
||||
UserID: "user123",
|
||||
@@ -120,7 +120,7 @@ func TestAuthorizeHandler_UserIDMismatch(t *testing.T) {
|
||||
}
|
||||
|
||||
body, _ := json.Marshal(payload)
|
||||
req := httptest.NewRequest("POST", "/v1/auth/check", bytes.NewBuffer(body))
|
||||
req := httptest.NewRequest("POST", AuthCheckEndpoint, bytes.NewBuffer(body))
|
||||
ctx := context.WithValue(req.Context(), models.ContextKey("claims"), claims)
|
||||
req = req.WithContext(ctx)
|
||||
w := httptest.NewRecorder()
|
||||
@@ -130,11 +130,11 @@ func TestAuthorizeHandler_UserIDMismatch(t *testing.T) {
|
||||
|
||||
// Assert
|
||||
if w.Code != http.StatusForbidden {
|
||||
t.Errorf("Expected status %d, got %d", http.StatusForbidden, w.Code)
|
||||
t.Errorf(ExpectedStatusMessage, http.StatusForbidden, w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthorizeHandler_NilMaps(t *testing.T) {
|
||||
func TestAuthorizeHandlerNilMaps(t *testing.T) {
|
||||
// Test that nil maps don't cause additional panics beyond missing authService
|
||||
claims := &models.Claims{
|
||||
UserID: "user123",
|
||||
@@ -151,7 +151,7 @@ func TestAuthorizeHandler_NilMaps(t *testing.T) {
|
||||
}
|
||||
|
||||
body, _ := json.Marshal(payload)
|
||||
req := httptest.NewRequest("POST", "/v1/auth/check", bytes.NewBuffer(body))
|
||||
req := httptest.NewRequest("POST", AuthCheckEndpoint, bytes.NewBuffer(body))
|
||||
ctx := context.WithValue(req.Context(), models.ContextKey("claims"), claims)
|
||||
req = req.WithContext(ctx)
|
||||
w := httptest.NewRecorder()
|
||||
@@ -173,7 +173,7 @@ func TestAuthorizeHandler_NilMaps(t *testing.T) {
|
||||
|
||||
// Additional comprehensive test cases
|
||||
|
||||
func TestAuthorizeHandler_EmptyUserID(t *testing.T) {
|
||||
func TestAuthorizeHandlerEmptyUserID(t *testing.T) {
|
||||
claims := &models.Claims{
|
||||
UserID: "user123",
|
||||
Username: "testuser",
|
||||
@@ -187,7 +187,7 @@ func TestAuthorizeHandler_EmptyUserID(t *testing.T) {
|
||||
}
|
||||
|
||||
body, _ := json.Marshal(payload)
|
||||
req := httptest.NewRequest("POST", "/v1/auth/check", bytes.NewBuffer(body))
|
||||
req := httptest.NewRequest("POST", AuthCheckEndpoint, bytes.NewBuffer(body))
|
||||
ctx := context.WithValue(req.Context(), models.ContextKey("claims"), claims)
|
||||
req = req.WithContext(ctx)
|
||||
w := httptest.NewRecorder()
|
||||
@@ -199,7 +199,7 @@ func TestAuthorizeHandler_EmptyUserID(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthorizeHandler_EmptyResource(t *testing.T) {
|
||||
func TestAuthorizeHandlerEmptyResource(t *testing.T) {
|
||||
claims := &models.Claims{
|
||||
UserID: "user123",
|
||||
Username: "testuser",
|
||||
@@ -213,7 +213,7 @@ func TestAuthorizeHandler_EmptyResource(t *testing.T) {
|
||||
}
|
||||
|
||||
body, _ := json.Marshal(payload)
|
||||
req := httptest.NewRequest("POST", "/v1/auth/check", bytes.NewBuffer(body))
|
||||
req := httptest.NewRequest("POST", AuthCheckEndpoint, bytes.NewBuffer(body))
|
||||
ctx := context.WithValue(req.Context(), models.ContextKey("claims"), claims)
|
||||
req = req.WithContext(ctx)
|
||||
w := httptest.NewRecorder()
|
||||
@@ -225,7 +225,7 @@ func TestAuthorizeHandler_EmptyResource(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthorizeHandler_EmptyAction(t *testing.T) {
|
||||
func TestAuthorizeHandlerEmptyAction(t *testing.T) {
|
||||
claims := &models.Claims{
|
||||
UserID: "user123",
|
||||
Username: "testuser",
|
||||
@@ -239,7 +239,7 @@ func TestAuthorizeHandler_EmptyAction(t *testing.T) {
|
||||
}
|
||||
|
||||
body, _ := json.Marshal(payload)
|
||||
req := httptest.NewRequest("POST", "/v1/auth/check", bytes.NewBuffer(body))
|
||||
req := httptest.NewRequest("POST", AuthCheckEndpoint, bytes.NewBuffer(body))
|
||||
ctx := context.WithValue(req.Context(), models.ContextKey("claims"), claims)
|
||||
req = req.WithContext(ctx)
|
||||
w := httptest.NewRecorder()
|
||||
@@ -251,8 +251,8 @@ func TestAuthorizeHandler_EmptyAction(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthorizeHandler_InvalidClaimsType(t *testing.T) {
|
||||
req := httptest.NewRequest("POST", "/v1/auth/check", bytes.NewBufferString(`{"userId":"user123","resource":"doc","action":"read"}`))
|
||||
func TestAuthorizeHandlerInvalidClaimsType(t *testing.T) {
|
||||
req := httptest.NewRequest("POST", AuthCheckEndpoint, bytes.NewBufferString(`{"userId":"user123","resource":"doc","action":"read"}`))
|
||||
|
||||
// Set claims as wrong type
|
||||
ctx := context.WithValue(req.Context(), models.ContextKey("claims"), "invalid_claims_type")
|
||||
@@ -266,7 +266,7 @@ func TestAuthorizeHandler_InvalidClaimsType(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthorizeHandler_MalformedJSON(t *testing.T) {
|
||||
func TestAuthorizeHandlerMalformedJSON(t *testing.T) {
|
||||
claims := &models.Claims{
|
||||
UserID: "user123",
|
||||
Username: "testuser",
|
||||
@@ -285,7 +285,7 @@ func TestAuthorizeHandler_MalformedJSON(t *testing.T) {
|
||||
|
||||
for _, tc := range testCases {
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
req := httptest.NewRequest("POST", "/v1/auth/check", bytes.NewBufferString(tc.payload))
|
||||
req := httptest.NewRequest("POST", AuthCheckEndpoint, bytes.NewBufferString(tc.payload))
|
||||
ctx := context.WithValue(req.Context(), models.ContextKey("claims"), claims)
|
||||
req = req.WithContext(ctx)
|
||||
w := httptest.NewRecorder()
|
||||
@@ -299,7 +299,7 @@ func TestAuthorizeHandler_MalformedJSON(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestAuthorizeHandler_SpecialCharactersInFields(t *testing.T) {
|
||||
func TestAuthorizeHandlerSpecialCharactersInFields(t *testing.T) {
|
||||
testCases := []struct {
|
||||
name string
|
||||
userID string
|
||||
@@ -321,7 +321,7 @@ func TestAuthorizeHandler_SpecialCharactersInFields(t *testing.T) {
|
||||
}
|
||||
|
||||
body, _ := json.Marshal(payload)
|
||||
req := httptest.NewRequest("POST", "/v1/auth/check", bytes.NewBuffer(body))
|
||||
req := httptest.NewRequest("POST", AuthCheckEndpoint, bytes.NewBuffer(body))
|
||||
|
||||
// Update claims to match userID
|
||||
testClaims := &models.Claims{
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
package handlers
|
||||
|
||||
const (
|
||||
ExpectedStatusMessage = "Expected status %d, got %d"
|
||||
AuthCheckEndpoint = "/v1/auth/check"
|
||||
HealthCheckEndpoint = "/health"
|
||||
StatusMismatchMessage = "status = %v, want %v"
|
||||
FailedToDecodeResponseMessage = "failed to decode response: %v"
|
||||
FailedToCreateMockDBMessage = "failed to create mock db: %v"
|
||||
ReadyCheckEndpoint = "/ready"
|
||||
ExpectedStatus200Message = "Expected status 200, got %d"
|
||||
)
|
||||
+45
-45
@@ -30,7 +30,7 @@ func TestHealthHandler(t *testing.T) {
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, "/health", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, HealthCheckEndpoint, nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
HealthHandler(w, req)
|
||||
@@ -39,16 +39,16 @@ func TestHealthHandler(t *testing.T) {
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != tt.wantStatus {
|
||||
t.Errorf("status = %v, want %v", resp.StatusCode, tt.wantStatus)
|
||||
t.Errorf(StatusMismatchMessage, resp.StatusCode, tt.wantStatus)
|
||||
}
|
||||
|
||||
var healthResp models.HealthResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&healthResp); err != nil {
|
||||
t.Fatalf("failed to decode response: %v", err)
|
||||
t.Fatalf(FailedToDecodeResponseMessage, err)
|
||||
}
|
||||
|
||||
if healthResp.Status != tt.wantBodyStatus {
|
||||
t.Errorf("status = %v, want %v", healthResp.Status, tt.wantBodyStatus)
|
||||
t.Errorf(StatusMismatchMessage, healthResp.Status, tt.wantBodyStatus)
|
||||
}
|
||||
|
||||
contentType := resp.Header.Get("Content-Type")
|
||||
@@ -59,11 +59,11 @@ func TestHealthHandler(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadyHandler_AllHealthy(t *testing.T) {
|
||||
func TestReadyHandlerAllHealthy(t *testing.T) {
|
||||
// Setup mock DB
|
||||
mockDB, mock, err := sqlmock.New(sqlmock.MonitorPingsOption(true))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create mock db: %v", err)
|
||||
t.Fatalf(FailedToCreateMockDBMessage, err)
|
||||
}
|
||||
defer mockDB.Close()
|
||||
|
||||
@@ -80,7 +80,7 @@ func TestReadyHandler_AllHealthy(t *testing.T) {
|
||||
redisclient.RDB = nil
|
||||
defer func() { redisclient.RDB = originalRedis }()
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/ready", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, ReadyCheckEndpoint, nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
ReadyHandler(w, req)
|
||||
@@ -90,7 +90,7 @@ func TestReadyHandler_AllHealthy(t *testing.T) {
|
||||
|
||||
var healthResp models.HealthResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&healthResp); err != nil {
|
||||
t.Fatalf("failed to decode response: %v", err)
|
||||
t.Fatalf(FailedToDecodeResponseMessage, err)
|
||||
}
|
||||
|
||||
if healthResp.Services["database"] != "healthy" {
|
||||
@@ -103,11 +103,11 @@ func TestReadyHandler_AllHealthy(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadyHandler_DBUnhealthy(t *testing.T) {
|
||||
func TestReadyHandlerDBUnhealthy(t *testing.T) {
|
||||
// Setup mock DB that fails ping
|
||||
mockDB, mock, err := sqlmock.New(sqlmock.MonitorPingsOption(true))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create mock db: %v", err)
|
||||
t.Fatalf(FailedToCreateMockDBMessage, err)
|
||||
}
|
||||
defer mockDB.Close()
|
||||
|
||||
@@ -124,7 +124,7 @@ func TestReadyHandler_DBUnhealthy(t *testing.T) {
|
||||
redisclient.RDB = nil
|
||||
defer func() { redisclient.RDB = originalRedis }()
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/ready", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, ReadyCheckEndpoint, nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
ReadyHandler(w, req)
|
||||
@@ -133,12 +133,12 @@ func TestReadyHandler_DBUnhealthy(t *testing.T) {
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusServiceUnavailable {
|
||||
t.Errorf("status = %v, want %v", resp.StatusCode, http.StatusServiceUnavailable)
|
||||
t.Errorf(StatusMismatchMessage, resp.StatusCode, http.StatusServiceUnavailable)
|
||||
}
|
||||
|
||||
var healthResp models.HealthResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&healthResp); err != nil {
|
||||
t.Fatalf("failed to decode response: %v", err)
|
||||
t.Fatalf(FailedToDecodeResponseMessage, err)
|
||||
}
|
||||
|
||||
if healthResp.Status != "not_ready" {
|
||||
@@ -154,7 +154,7 @@ func TestReadyHandler_DBUnhealthy(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadyHandler_DBNotInitialized(t *testing.T) {
|
||||
func TestReadyHandlerDBNotInitialized(t *testing.T) {
|
||||
// Save original and set to nil
|
||||
originalDB := db.DB
|
||||
db.DB = nil
|
||||
@@ -164,7 +164,7 @@ func TestReadyHandler_DBNotInitialized(t *testing.T) {
|
||||
redisclient.RDB = nil
|
||||
defer func() { redisclient.RDB = originalRedis }()
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/ready", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, ReadyCheckEndpoint, nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
ReadyHandler(w, req)
|
||||
@@ -173,12 +173,12 @@ func TestReadyHandler_DBNotInitialized(t *testing.T) {
|
||||
defer resp.Body.Close()
|
||||
|
||||
if resp.StatusCode != http.StatusServiceUnavailable {
|
||||
t.Errorf("status = %v, want %v", resp.StatusCode, http.StatusServiceUnavailable)
|
||||
t.Errorf(StatusMismatchMessage, resp.StatusCode, http.StatusServiceUnavailable)
|
||||
}
|
||||
|
||||
var healthResp models.HealthResponse
|
||||
if err := json.NewDecoder(resp.Body).Decode(&healthResp); err != nil {
|
||||
t.Fatalf("failed to decode response: %v", err)
|
||||
t.Fatalf(FailedToDecodeResponseMessage, err)
|
||||
}
|
||||
|
||||
if healthResp.Status != "not_ready" {
|
||||
@@ -194,7 +194,7 @@ func TestReadyHandler_DBNotInitialized(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadyHandler_ContentType(t *testing.T) {
|
||||
func TestReadyHandlerContentType(t *testing.T) {
|
||||
originalDB := db.DB
|
||||
db.DB = nil
|
||||
defer func() { db.DB = originalDB }()
|
||||
@@ -203,7 +203,7 @@ func TestReadyHandler_ContentType(t *testing.T) {
|
||||
redisclient.RDB = nil
|
||||
defer func() { redisclient.RDB = originalRedis }()
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/ready", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, ReadyCheckEndpoint, nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
ReadyHandler(w, req)
|
||||
@@ -219,19 +219,19 @@ func TestReadyHandler_ContentType(t *testing.T) {
|
||||
|
||||
// Additional comprehensive test cases
|
||||
|
||||
func TestHealthHandler_MultipleRequests(t *testing.T) {
|
||||
func TestHealthHandlerMultipleRequests(t *testing.T) {
|
||||
// Test that multiple concurrent requests work correctly
|
||||
concurrency := 10
|
||||
done := make(chan bool, concurrency)
|
||||
|
||||
for i := 0; i < concurrency; i++ {
|
||||
go func() {
|
||||
req := httptest.NewRequest(http.MethodGet, "/health", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, HealthCheckEndpoint, nil)
|
||||
w := httptest.NewRecorder()
|
||||
HealthHandler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Expected status 200, got %d", w.Code)
|
||||
t.Errorf(ExpectedStatus200Message, w.Code)
|
||||
}
|
||||
done <- true
|
||||
}()
|
||||
@@ -242,12 +242,12 @@ func TestHealthHandler_MultipleRequests(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHealthHandler_DifferentMethods(t *testing.T) {
|
||||
func TestHealthHandlerDifferentMethods(t *testing.T) {
|
||||
methods := []string{"GET", "POST", "PUT", "DELETE", "PATCH"}
|
||||
|
||||
for _, method := range methods {
|
||||
t.Run(method, func(t *testing.T) {
|
||||
req := httptest.NewRequest(method, "/health", nil)
|
||||
req := httptest.NewRequest(method, HealthCheckEndpoint, nil)
|
||||
w := httptest.NewRecorder()
|
||||
HealthHandler(w, req)
|
||||
|
||||
@@ -259,14 +259,14 @@ func TestHealthHandler_DifferentMethods(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHealthHandler_ResponseFormat(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, "/health", nil)
|
||||
func TestHealthHandlerResponseFormat(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, HealthCheckEndpoint, nil)
|
||||
w := httptest.NewRecorder()
|
||||
HealthHandler(w, req)
|
||||
|
||||
var response models.HealthResponse
|
||||
if err := json.NewDecoder(w.Body).Decode(&response); err != nil {
|
||||
t.Fatalf("Failed to decode response: %v", err)
|
||||
t.Fatalf(FailedToDecodeResponseMessage, err)
|
||||
}
|
||||
|
||||
if response.Status == "" {
|
||||
@@ -278,10 +278,10 @@ func TestHealthHandler_ResponseFormat(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadyHandler_DatabaseTimeout(t *testing.T) {
|
||||
func TestReadyHandlerDatabaseTimeout(t *testing.T) {
|
||||
mockDB, mock, err := sqlmock.New(sqlmock.MonitorPingsOption(true))
|
||||
if err != nil {
|
||||
t.Fatalf("failed to create mock db: %v", err)
|
||||
t.Fatalf(FailedToCreateMockDBMessage, err)
|
||||
}
|
||||
defer mockDB.Close()
|
||||
|
||||
@@ -296,7 +296,7 @@ func TestReadyHandler_DatabaseTimeout(t *testing.T) {
|
||||
redisclient.RDB = nil
|
||||
defer func() { redisclient.RDB = originalRedis }()
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/ready", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, ReadyCheckEndpoint, nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
// This should timeout and return unhealthy
|
||||
@@ -307,7 +307,7 @@ func TestReadyHandler_DatabaseTimeout(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadyHandler_BothServicesHealthy(t *testing.T) {
|
||||
func TestReadyHandlerBothServicesHealthy(t *testing.T) {
|
||||
// Use miniredis for Redis mock
|
||||
mr, err := miniredis.Run()
|
||||
if err != nil {
|
||||
@@ -325,7 +325,7 @@ func TestReadyHandler_BothServicesHealthy(t *testing.T) {
|
||||
// Setup mock database
|
||||
mockDB, mock, err := sqlmock.New(sqlmock.MonitorPingsOption(true))
|
||||
if err != nil {
|
||||
t.Fatalf("Failed to create mock DB: %v", err)
|
||||
t.Fatalf(FailedToCreateMockDBMessage, err)
|
||||
}
|
||||
defer mockDB.Close()
|
||||
|
||||
@@ -336,13 +336,13 @@ func TestReadyHandler_BothServicesHealthy(t *testing.T) {
|
||||
// Expect ping
|
||||
mock.ExpectPing()
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/ready", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, ReadyCheckEndpoint, nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
ReadyHandler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Expected status 200, got %d", w.Code)
|
||||
t.Errorf(ExpectedStatus200Message, w.Code)
|
||||
}
|
||||
|
||||
var response models.HealthResponse
|
||||
@@ -357,7 +357,7 @@ func TestReadyHandler_BothServicesHealthy(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadyHandler_NilDatabaseAndRedis(t *testing.T) {
|
||||
func TestReadyHandlerNilDatabaseAndRedis(t *testing.T) {
|
||||
originalDB := db.DB
|
||||
db.DB = nil
|
||||
defer func() { db.DB = originalDB }()
|
||||
@@ -366,7 +366,7 @@ func TestReadyHandler_NilDatabaseAndRedis(t *testing.T) {
|
||||
redisclient.RDB = nil
|
||||
defer func() { redisclient.RDB = originalRedis }()
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/ready", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, ReadyCheckEndpoint, nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
ReadyHandler(w, req)
|
||||
@@ -377,7 +377,7 @@ func TestReadyHandler_NilDatabaseAndRedis(t *testing.T) {
|
||||
|
||||
var response models.HealthResponse
|
||||
if err := json.NewDecoder(w.Body).Decode(&response); err != nil {
|
||||
t.Fatalf("Failed to decode response: %v", err)
|
||||
t.Fatalf(FailedToDecodeResponseMessage, err)
|
||||
}
|
||||
|
||||
// The handler returns "not_ready" when services are down
|
||||
@@ -386,7 +386,7 @@ func TestReadyHandler_NilDatabaseAndRedis(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadyHandler_ResponseStructure(t *testing.T) {
|
||||
func TestReadyHandlerResponseStructure(t *testing.T) {
|
||||
originalDB := db.DB
|
||||
db.DB = nil
|
||||
defer func() { db.DB = originalDB }()
|
||||
@@ -395,7 +395,7 @@ func TestReadyHandler_ResponseStructure(t *testing.T) {
|
||||
redisclient.RDB = nil
|
||||
defer func() { redisclient.RDB = originalRedis }()
|
||||
|
||||
req := httptest.NewRequest(http.MethodGet, "/ready", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, ReadyCheckEndpoint, nil)
|
||||
w := httptest.NewRecorder()
|
||||
|
||||
ReadyHandler(w, req)
|
||||
@@ -403,7 +403,7 @@ func TestReadyHandler_ResponseStructure(t *testing.T) {
|
||||
// Verify response is valid JSON
|
||||
var response map[string]interface{}
|
||||
if err := json.NewDecoder(w.Body).Decode(&response); err != nil {
|
||||
t.Fatalf("Failed to decode response: %v", err)
|
||||
t.Fatalf(FailedToDecodeResponseMessage, err)
|
||||
}
|
||||
|
||||
// Check that response has expected fields
|
||||
@@ -412,8 +412,8 @@ func TestReadyHandler_ResponseStructure(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestHealthHandler_WithCustomHeaders(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, "/health", nil)
|
||||
func TestHealthHandlerWithCustomHeaders(t *testing.T) {
|
||||
req := httptest.NewRequest(http.MethodGet, HealthCheckEndpoint, nil)
|
||||
req.Header.Set("X-Request-ID", "test-123")
|
||||
req.Header.Set("User-Agent", "Test-Agent/1.0")
|
||||
|
||||
@@ -421,11 +421,11 @@ func TestHealthHandler_WithCustomHeaders(t *testing.T) {
|
||||
HealthHandler(w, req)
|
||||
|
||||
if w.Code != http.StatusOK {
|
||||
t.Errorf("Expected status 200, got %d", w.Code)
|
||||
t.Errorf(ExpectedStatus200Message, w.Code)
|
||||
}
|
||||
}
|
||||
|
||||
func TestReadyHandler_ConcurrentRequests(t *testing.T) {
|
||||
func TestReadyHandlerConcurrentRequests(t *testing.T) {
|
||||
originalDB := db.DB
|
||||
db.DB = nil
|
||||
defer func() { db.DB = originalDB }()
|
||||
@@ -439,7 +439,7 @@ func TestReadyHandler_ConcurrentRequests(t *testing.T) {
|
||||
|
||||
for i := 0; i < concurrency; i++ {
|
||||
go func() {
|
||||
req := httptest.NewRequest(http.MethodGet, "/ready", nil)
|
||||
req := httptest.NewRequest(http.MethodGet, ReadyCheckEndpoint, nil)
|
||||
w := httptest.NewRecorder()
|
||||
ReadyHandler(w, req)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user