Skip to content

Commit 9803d0f

Browse files
authored
feat: add unauthenticated /healthz endpoint for health checks (#131)
1 parent 48805e7 commit 9803d0f

2 files changed

Lines changed: 33 additions & 0 deletions

File tree

pkg/mcp-proxy/main.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,10 @@ func Run(
300300
router := gin.New()
301301
router.SetTrustedProxies(trustedProxy)
302302

303+
router.GET("/healthz", func(c *gin.Context) {
304+
c.JSON(http.StatusOK, gin.H{"status": "ok"})
305+
})
306+
303307
router.Use(ginzap.Ginzap(logger, time.RFC3339, true))
304308
router.Use(ginzap.RecoveryWithZap(logger, true))
305309
store := cookie.NewStore(secret)

pkg/mcp-proxy/main_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,13 @@ package mcpproxy
22

33
import (
44
"crypto/rsa"
5+
"encoding/json"
56
"errors"
67
"net/http"
8+
"net/http/httptest"
79
"testing"
810

11+
"github.com/gin-gonic/gin"
912
"github.com/sigbit/mcp-auth-proxy/pkg/proxy"
1013
"github.com/stretchr/testify/require"
1114
)
@@ -117,3 +120,29 @@ func TestRun_PassesHTTPStreamingOnlyToProxyRouter(t *testing.T) {
117120
require.Contains(t, err.Error(), "failed to create proxy router")
118121
require.True(t, streamingOnlyReceived, "httpStreamingOnly should be forwarded to proxy router")
119122
}
123+
124+
func TestHealthzEndpoint(t *testing.T) {
125+
gin.SetMode(gin.TestMode)
126+
router := gin.New()
127+
128+
// Register healthz before auth middleware, same as in Run()
129+
router.GET("/healthz", func(c *gin.Context) {
130+
c.JSON(http.StatusOK, gin.H{"status": "ok"})
131+
})
132+
133+
// Add a catch-all that returns 401 to simulate auth middleware
134+
router.Use(func(c *gin.Context) {
135+
c.AbortWithStatus(http.StatusUnauthorized)
136+
})
137+
138+
w := httptest.NewRecorder()
139+
req, _ := http.NewRequest("GET", "/healthz", nil)
140+
router.ServeHTTP(w, req)
141+
142+
require.Equal(t, http.StatusOK, w.Code)
143+
144+
var body map[string]string
145+
err := json.Unmarshal(w.Body.Bytes(), &body)
146+
require.NoError(t, err)
147+
require.Equal(t, "ok", body["status"])
148+
}

0 commit comments

Comments
 (0)