Skip to content

Commit 7e9e176

Browse files
authored
test(authorization): improved tests to use real MCP clients (#443)
Signed-off-by: Marc Nuri <marc@marcnuri.com>
1 parent 6342379 commit 7e9e176

File tree

8 files changed

+660
-538
lines changed

8 files changed

+660
-538
lines changed

internal/test/mcp.go

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,13 @@ import (
1212
"golang.org/x/net/context"
1313
)
1414

15+
func McpInitRequest() mcp.InitializeRequest {
16+
initRequest := mcp.InitializeRequest{}
17+
initRequest.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION
18+
initRequest.Params.ClientInfo = mcp.Implementation{Name: "test", Version: "1.33.7"}
19+
return initRequest
20+
}
21+
1522
type McpClient struct {
1623
ctx context.Context
1724
testServer *httptest.Server
@@ -28,10 +35,7 @@ func NewMcpClient(t *testing.T, mcpHttpServer http.Handler, options ...transport
2835
require.NoError(t, err, "Expected no error creating MCP client")
2936
err = ret.Start(t.Context())
3037
require.NoError(t, err, "Expected no error starting MCP client")
31-
initRequest := mcp.InitializeRequest{}
32-
initRequest.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION
33-
initRequest.Params.ClientInfo = mcp.Implementation{Name: "test", Version: "1.33.7"}
34-
_, err = ret.Initialize(t.Context(), initRequest)
38+
_, err = ret.Initialize(t.Context(), McpInitRequest())
3539
require.NoError(t, err, "Expected no error initializing MCP client")
3640
return ret
3741
}

internal/test/mock_server.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,3 +216,33 @@ func (h *InOpenShiftHandler) ServeHTTP(w http.ResponseWriter, req *http.Request)
216216
return
217217
}
218218
}
219+
220+
const tokenReviewSuccessful = `
221+
{
222+
"kind": "TokenReview",
223+
"apiVersion": "authentication.k8s.io/v1",
224+
"spec": {"token": "valid-token"},
225+
"status": {
226+
"authenticated": true,
227+
"user": {
228+
"username": "test-user",
229+
"groups": ["system:authenticated"]
230+
},
231+
"audiences": ["the-audience"]
232+
}
233+
}`
234+
235+
type TokenReviewHandler struct {
236+
TokenReviewed bool
237+
}
238+
239+
var _ http.Handler = (*TokenReviewHandler)(nil)
240+
241+
func (h *TokenReviewHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
242+
if req.URL.EscapedPath() == "/apis/authentication.k8s.io/v1/tokenreviews" {
243+
w.Header().Set("Content-Type", "application/json")
244+
_, _ = w.Write([]byte(tokenReviewSuccessful))
245+
h.TokenReviewed = true
246+
return
247+
}
248+
}

internal/test/test.go

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
package test
22

33
import (
4+
"fmt"
5+
"net"
46
"os"
57
"path/filepath"
68
"runtime"
9+
"time"
710
)
811

912
func Must[T any](v T, err error) T {
@@ -19,3 +22,30 @@ func ReadFile(path ...string) string {
1922
fileBytes := Must(os.ReadFile(filePath))
2023
return string(fileBytes)
2124
}
25+
26+
func RandomPortAddress() (*net.TCPAddr, error) {
27+
ln, err := net.Listen("tcp", "0.0.0.0:0")
28+
if err != nil {
29+
return nil, fmt.Errorf("failed to find random port for HTTP server: %v", err)
30+
}
31+
defer func() { _ = ln.Close() }()
32+
tcpAddr, ok := ln.Addr().(*net.TCPAddr)
33+
if !ok {
34+
return nil, fmt.Errorf("failed to cast listener address to TCPAddr")
35+
}
36+
return tcpAddr, nil
37+
}
38+
39+
func WaitForServer(tcpAddr *net.TCPAddr) error {
40+
var conn *net.TCPConn
41+
var err error
42+
for i := 0; i < 10; i++ {
43+
conn, err = net.DialTCP("tcp", nil, tcpAddr)
44+
if err == nil {
45+
_ = conn.Close()
46+
break
47+
}
48+
time.Sleep(50 * time.Millisecond)
49+
}
50+
return err
51+
}

0 commit comments

Comments
 (0)