package httpserver import ( "context" "errors" "net/http" "net/http/httptest" "testing" "git.toki-labs.com/toki/ariadne/internal/platform/config" "go.uber.org/zap" ) type readinessStub bool func (stub readinessStub) Check(context.Context) error { if !stub { return errors.New("not ready") } return nil } func TestHealth(t *testing.T) { t.Parallel() server := New( config.Config{}, readinessStub(false), zap.NewNop(), BuildInfo{Version: "test"}, ) request := httptest.NewRequest(http.MethodGet, "/healthz", nil) response := httptest.NewRecorder() server.routes().ServeHTTP(response, request) if response.Code != http.StatusOK { t.Fatalf("status = %d, want %d", response.Code, http.StatusOK) } if contentType := response.Header().Get("Content-Type"); contentType != "application/json" { t.Fatalf("Content-Type = %q, want application/json", contentType) } } func TestReadiness(t *testing.T) { t.Parallel() tests := []struct { name string readiness readinessStub want int }{ {name: "ready", readiness: true, want: http.StatusOK}, {name: "not ready", readiness: false, want: http.StatusServiceUnavailable}, } for _, test := range tests { test := test t.Run(test.name, func(t *testing.T) { t.Parallel() server := New( config.Config{}, test.readiness, zap.NewNop(), BuildInfo{Version: "test"}, ) request := httptest.NewRequest(http.MethodGet, "/readyz", nil) response := httptest.NewRecorder() server.routes().ServeHTTP(response, request) if response.Code != test.want { t.Fatalf("status = %d, want %d", response.Code, test.want) } }) } }