/healthz now returns JSON with three-state JWT status: disabled
(DEX_ISSUER_URL unset), enabled (validator initialized), or
degraded (configured but init failed — only static-token auth
currently accepted). last_error surfaces the init failure so ops
can correlate with Dex outage windows.
Partial fix for #6. The cited internal/auth/jwt.go moved out
of this repo in 658f4ba (mcp-chassis migration); per-attempt
logging and 503 + WWW-Authenticate temporarily_unavailable
require chassis-side changes and a coordinated v0.1.1 bump
across all MCP consumers — tracked separately.
Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
61 lines
1.5 KiB
Go
61 lines
1.5 KiB
Go
package main
|
|
|
|
import (
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestHealthzHandler(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
dexConfigured bool
|
|
validatorReady bool
|
|
initErr error
|
|
wantStatus string
|
|
wantLastError string
|
|
}{
|
|
{
|
|
name: "disabled when DEX_ISSUER_URL unset",
|
|
wantStatus: "disabled",
|
|
wantLastError: "",
|
|
},
|
|
{
|
|
name: "enabled when validator initialized",
|
|
dexConfigured: true,
|
|
validatorReady: true,
|
|
wantStatus: "enabled",
|
|
wantLastError: "",
|
|
},
|
|
{
|
|
name: "degraded when Dex configured but init failed",
|
|
dexConfigured: true,
|
|
initErr: errors.New("fetch oidc discovery: dial tcp: connection refused"),
|
|
wantStatus: "degraded",
|
|
wantLastError: "fetch oidc discovery: dial tcp: connection refused",
|
|
},
|
|
}
|
|
|
|
for _, tc := range tests {
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
h := newHealthzHandler(tc.dexConfigured, tc.validatorReady, tc.initErr)
|
|
rec := httptest.NewRecorder()
|
|
h.ServeHTTP(rec, httptest.NewRequest(http.MethodGet, "/healthz", nil))
|
|
|
|
require.Equal(t, http.StatusOK, rec.Code)
|
|
assert.Equal(t, "application/json", rec.Header().Get("Content-Type"))
|
|
|
|
var got healthStatus
|
|
require.NoError(t, json.Unmarshal(rec.Body.Bytes(), &got))
|
|
assert.True(t, got.OK)
|
|
assert.Equal(t, tc.wantStatus, got.JWT.Status)
|
|
assert.Equal(t, tc.wantLastError, got.JWT.LastError)
|
|
})
|
|
}
|
|
}
|