From aa3429717f5a22fc3f834d078dc2a41843dd5704 Mon Sep 17 00:00:00 2001 From: Arthur Belleville Date: Fri, 15 May 2026 18:08:16 +0200 Subject: [PATCH] test(07-01): add failing tests for HealthzHandler (no-arg) and ReadyzHandler split - TestHealthz_OK now calls HealthzHandler() with no args (liveness, no db field) - TestHealthz_Down deleted (new HealthzHandler has no failure mode) - TestReadyz_OK added: ReadyzHandler(stubPinger{err: nil}) -> 200 + db:ok - TestReadyz_Down added: ReadyzHandler(stubPinger{err: ...}) -> 503 + degraded --- backend/internal/web/handlers_test.go | 33 +++++++++++++++++++++++---- 1 file changed, 29 insertions(+), 4 deletions(-) diff --git a/backend/internal/web/handlers_test.go b/backend/internal/web/handlers_test.go index 54cf20d..e20006b 100644 --- a/backend/internal/web/handlers_test.go +++ b/backend/internal/web/handlers_test.go @@ -25,7 +25,32 @@ func TestHealthz_OK(t *testing.T) { rec := httptest.NewRecorder() req := httptest.NewRequest(http.MethodGet, "/healthz", nil) - HealthzHandler(stubPinger{err: nil}).ServeHTTP(rec, req) + // HealthzHandler takes no args — pure liveness, no DB ping (D-12). + HealthzHandler().ServeHTTP(rec, req) + + if rec.Code != http.StatusOK { + t.Fatalf("status = %d; want 200", rec.Code) + } + if ct := rec.Header().Get("Content-Type"); !strings.Contains(ct, "application/json") { + t.Errorf("Content-Type = %q; want application/json", ct) + } + body := rec.Body.String() + if !strings.Contains(body, `"status":"ok"`) { + t.Errorf("body missing status:ok; got: %s", body) + } + // Liveness endpoint must NOT expose db field. + if strings.Contains(body, `"db"`) { + t.Errorf("liveness body must not contain db field; got: %s", body) + } +} + +// TestHealthz_Down is deleted — new HealthzHandler has no failure mode (D-12). + +func TestReadyz_OK(t *testing.T) { + rec := httptest.NewRecorder() + req := httptest.NewRequest(http.MethodGet, "/readyz", nil) + + ReadyzHandler(stubPinger{err: nil}).ServeHTTP(rec, req) if rec.Code != http.StatusOK { t.Fatalf("status = %d; want 200", rec.Code) @@ -42,11 +67,11 @@ func TestHealthz_OK(t *testing.T) { } } -func TestHealthz_Down(t *testing.T) { +func TestReadyz_Down(t *testing.T) { rec := httptest.NewRecorder() - req := httptest.NewRequest(http.MethodGet, "/healthz", nil) + req := httptest.NewRequest(http.MethodGet, "/readyz", nil) - HealthzHandler(stubPinger{err: errors.New("conn refused")}).ServeHTTP(rec, req) + ReadyzHandler(stubPinger{err: errors.New("conn refused")}).ServeHTTP(rec, req) if rec.Code != http.StatusServiceUnavailable { t.Fatalf("status = %d; want 503", rec.Code)