|
| 1 | +package httpapi |
| 2 | + |
| 3 | +import ( |
| 4 | + "net/http" |
| 5 | + "net/http/httptest" |
| 6 | + "testing" |
| 7 | + |
| 8 | + "github.com/stretchr/testify/assert" |
| 9 | + "github.com/stretchr/testify/require" |
| 10 | + "go.uber.org/zap" |
| 11 | + |
| 12 | + "github.com/smart-mcp-proxy/mcpproxy-go/internal/observability" |
| 13 | +) |
| 14 | + |
| 15 | +// notReadyController reports IsReady()==false so we can distinguish the |
| 16 | +// controller-backed readiness handler (503) from the observability health |
| 17 | +// manager's vacuous always-ready handler (200). |
| 18 | +type notReadyController struct{ MockServerController } |
| 19 | + |
| 20 | +func (m *notReadyController) IsReady() bool { return false } |
| 21 | + |
| 22 | +// MCP-32 regression: enabling the metrics exporter must NOT take over /readyz. |
| 23 | +// When the observability health manager is not active, /readyz must keep |
| 24 | +// reflecting controller readiness (a config-gated feature is a no-op for |
| 25 | +// readiness). Previously passing a non-nil observability manager skipped the |
| 26 | +// controller health block entirely. |
| 27 | +func TestReadyz_StaysControllerBackedWhenObservabilityHasNoHealth(t *testing.T) { |
| 28 | + obsCfg := observability.Config{ |
| 29 | + Metrics: observability.MetricsConfig{Enabled: true}, |
| 30 | + Health: observability.HealthConfig{Enabled: false}, |
| 31 | + Tracing: observability.TracingConfig{Enabled: false}, |
| 32 | + } |
| 33 | + mgr, err := observability.NewManager(zap.NewNop().Sugar(), &obsCfg) |
| 34 | + require.NoError(t, err) |
| 35 | + require.NotNil(t, mgr.Metrics()) |
| 36 | + require.Nil(t, mgr.Health()) |
| 37 | + |
| 38 | + srv := NewServer(¬ReadyController{}, zap.NewNop().Sugar(), mgr) |
| 39 | + |
| 40 | + // /readyz reflects the controller (not ready -> 503), not a vacuous 200. |
| 41 | + rec := httptest.NewRecorder() |
| 42 | + srv.router.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/readyz", http.NoBody)) |
| 43 | + assert.Equal(t, http.StatusServiceUnavailable, rec.Code) |
| 44 | + assert.Contains(t, rec.Body.String(), `"ready":false`) |
| 45 | + |
| 46 | + // Liveness aliases remain registered too. |
| 47 | + for _, path := range []string{"/healthz", "/livez", "/health"} { |
| 48 | + r := httptest.NewRecorder() |
| 49 | + srv.router.ServeHTTP(r, httptest.NewRequest(http.MethodGet, path, http.NoBody)) |
| 50 | + assert.Equalf(t, http.StatusOK, r.Code, "liveness endpoint %s should be registered", path) |
| 51 | + } |
| 52 | + |
| 53 | + // And the metrics exporter is still served. |
| 54 | + rm := httptest.NewRecorder() |
| 55 | + srv.router.ServeHTTP(rm, httptest.NewRequest(http.MethodGet, "/metrics", http.NoBody)) |
| 56 | + assert.Equal(t, http.StatusOK, rm.Code) |
| 57 | + assert.Contains(t, rm.Body.String(), "mcpproxy_uptime_seconds") |
| 58 | +} |
0 commit comments