-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathmain_test.go
More file actions
98 lines (86 loc) · 1.88 KB
/
main_test.go
File metadata and controls
98 lines (86 loc) · 1.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package mcpproxy
import (
"crypto/rsa"
"encoding/json"
"errors"
"net/http"
"net/http/httptest"
"testing"
"github.com/gin-gonic/gin"
"github.com/sigbit/mcp-auth-proxy/pkg/proxy"
"github.com/stretchr/testify/require"
)
func TestRun_PassesHTTPStreamingOnlyToProxyRouter(t *testing.T) {
originalNewProxyRouter := newProxyRouter
t.Cleanup(func() {
newProxyRouter = originalNewProxyRouter
})
var streamingOnlyReceived bool
newProxyRouter = func(externalURL string, proxyHandler http.Handler, publicKey *rsa.PublicKey, proxyHeaders http.Header, httpStreamingOnly bool) (*proxy.ProxyRouter, error) {
streamingOnlyReceived = httpStreamingOnly
return nil, errors.New("proxy router init failed")
}
err := Run(
":0",
":0",
false,
"",
"",
false,
"",
"",
t.TempDir(),
"local",
"",
"http://localhost",
"",
"",
nil,
nil,
"",
"",
nil,
nil,
"",
"",
"",
nil,
"",
"",
nil,
nil,
nil,
nil,
false,
"",
"",
nil,
nil,
"",
[]string{"http://example.com"},
true,
)
require.Error(t, err)
require.Contains(t, err.Error(), "failed to create proxy router")
require.True(t, streamingOnlyReceived, "httpStreamingOnly should be forwarded to proxy router")
}
func TestHealthzEndpoint(t *testing.T) {
gin.SetMode(gin.TestMode)
router := gin.New()
// Register healthz before auth middleware, same as in Run()
router.GET("/healthz", func(c *gin.Context) {
c.JSON(http.StatusOK, gin.H{"status": "ok"})
})
// Add a catch-all that returns 401 to simulate auth middleware
router.Use(func(c *gin.Context) {
c.AbortWithStatus(http.StatusUnauthorized)
})
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/healthz", nil)
router.ServeHTTP(w, req)
require.Equal(t, http.StatusOK, w.Code)
var body map[string]string
err := json.Unmarshal(w.Body.Bytes(), &body)
require.NoError(t, err)
require.Equal(t, "ok", body["status"])
}