Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 8 additions & 4 deletions internal/test/mcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,13 @@ import (
"golang.org/x/net/context"
)

func McpInitRequest() mcp.InitializeRequest {
initRequest := mcp.InitializeRequest{}
initRequest.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just curiois: new sdk requires 2025-06-18 ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, that was already there:

initRequest := mcp.InitializeRequest{}
initRequest.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION
initRequest.Params.ClientInfo = mcp.Implementation{Name: "test", Version: "1.33.7"}

Just moved it around and made it reusable.

initRequest.Params.ClientInfo = mcp.Implementation{Name: "test", Version: "1.33.7"}
return initRequest
}

type McpClient struct {
ctx context.Context
testServer *httptest.Server
Expand All @@ -28,10 +35,7 @@ func NewMcpClient(t *testing.T, mcpHttpServer http.Handler, options ...transport
require.NoError(t, err, "Expected no error creating MCP client")
err = ret.Start(t.Context())
require.NoError(t, err, "Expected no error starting MCP client")
initRequest := mcp.InitializeRequest{}
initRequest.Params.ProtocolVersion = mcp.LATEST_PROTOCOL_VERSION
initRequest.Params.ClientInfo = mcp.Implementation{Name: "test", Version: "1.33.7"}
_, err = ret.Initialize(t.Context(), initRequest)
_, err = ret.Initialize(t.Context(), McpInitRequest())
require.NoError(t, err, "Expected no error initializing MCP client")
return ret
}
Expand Down
30 changes: 30 additions & 0 deletions internal/test/mock_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,33 @@ func (h *InOpenShiftHandler) ServeHTTP(w http.ResponseWriter, req *http.Request)
return
}
}

const tokenReviewSuccessful = `
{
"kind": "TokenReview",
"apiVersion": "authentication.k8s.io/v1",
"spec": {"token": "valid-token"},
"status": {
"authenticated": true,
"user": {
"username": "test-user",
"groups": ["system:authenticated"]
},
"audiences": ["the-audience"]
}
}`

type TokenReviewHandler struct {
TokenReviewed bool
}

var _ http.Handler = (*TokenReviewHandler)(nil)

func (h *TokenReviewHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
if req.URL.EscapedPath() == "/apis/authentication.k8s.io/v1/tokenreviews" {
w.Header().Set("Content-Type", "application/json")
_, _ = w.Write([]byte(tokenReviewSuccessful))
h.TokenReviewed = true
return
}
}
30 changes: 30 additions & 0 deletions internal/test/test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package test

import (
"fmt"
"net"
"os"
"path/filepath"
"runtime"
"time"
)

func Must[T any](v T, err error) T {
Expand All @@ -19,3 +22,30 @@ func ReadFile(path ...string) string {
fileBytes := Must(os.ReadFile(filePath))
return string(fileBytes)
}

func RandomPortAddress() (*net.TCPAddr, error) {
ln, err := net.Listen("tcp", "0.0.0.0:0")
if err != nil {
return nil, fmt.Errorf("failed to find random port for HTTP server: %v", err)
}
defer func() { _ = ln.Close() }()
tcpAddr, ok := ln.Addr().(*net.TCPAddr)
if !ok {
return nil, fmt.Errorf("failed to cast listener address to TCPAddr")
}
return tcpAddr, nil
}

func WaitForServer(tcpAddr *net.TCPAddr) error {
var conn *net.TCPConn
var err error
for i := 0; i < 10; i++ {
conn, err = net.DialTCP("tcp", nil, tcpAddr)
if err == nil {
_ = conn.Close()
break
}
time.Sleep(50 * time.Millisecond)
}
return err
}
Loading
Loading