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
This commit is contained in:
Arthur Belleville 2026-05-15 18:08:16 +02:00
parent 8ae83f6c50
commit aa3429717f
No known key found for this signature in database

View file

@ -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)