sabisan/internal/app/handlers_health_test.go
2026-05-17 13:36:50 +01:00

46 lines
1.1 KiB
Go

package app
import (
"encoding/json"
"net/http"
"net/http/httptest"
"testing"
)
func TestHealthzReturnsOK(t *testing.T) {
srv := newTestServer(t)
req := httptest.NewRequest(http.MethodGet, "/healthz", nil)
rec := httptest.NewRecorder()
srv.Routes().ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("expected ok, got %d", rec.Code)
}
}
func TestReadyzReturnsVersionAndDBHealth(t *testing.T) {
srv := newTestServer(t)
req := httptest.NewRequest(http.MethodGet, "/readyz", nil)
rec := httptest.NewRecorder()
srv.Routes().ServeHTTP(rec, req)
if rec.Code != http.StatusOK {
t.Fatalf("expected ok, got %d", rec.Code)
}
if got := rec.Header().Get("Content-Type"); got != "application/json; charset=utf-8" {
t.Fatalf("expected json content type, got %q", got)
}
var body struct {
Version string `json:"version"`
DBHealthy bool `json:"db_healthy"`
}
if err := json.NewDecoder(rec.Result().Body).Decode(&body); err != nil {
t.Fatal(err)
}
if body.Version != "test-version" || !body.DBHealthy {
t.Fatalf("unexpected readyz body: %+v", body)
}
}