MCPcopy
hub / github.com/googleapis/mcp-toolbox / TestMCPAuthMiddleware

Function TestMCPAuthMiddleware

internal/server/server_test.go:949–1160  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

947}
948
949func TestMCPAuthMiddleware(t *testing.T) {
950 ctx, cancel := context.WithCancel(context.Background())
951 defer cancel()
952
953 // Setup telemetry and logging
954 otelShutdown, err := telemetry.SetupOTel(ctx, "0.0.0", "", false, "", "toolbox")
955 if err != nil {
956 t.Fatalf("unexpected error: %s", err)
957 }
958 defer func() {
959 if err := otelShutdown(ctx); err != nil {
960 t.Fatalf("unexpected error shutting down otel: %s", err)
961 }
962 }()
963
964 testLogger, err := log.NewStdLogger(os.Stdout, os.Stderr, "info")
965 if err != nil {
966 t.Fatalf("unexpected error: %s", err)
967 }
968 ctx = util.WithLogger(ctx, testLogger)
969
970 instrumentation, err := telemetry.CreateTelemetryInstrumentation("0.0.0")
971 if err != nil {
972 t.Fatalf("unexpected error: %s", err)
973 }
974 ctx = util.WithInstrumentation(ctx, instrumentation)
975
976 // Setup mock introspection server
977 var mockResponse map[string]any
978 var mockStatus int
979 var mockRawResponse string
980
981 mockOIDC := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
982 if r.URL.Path == "/.well-known/openid-configuration" {
983 w.Header().Set("Content-Type", "application/json")
984 fmt.Fprintf(w, `{"issuer": "http://%s", "jwks_uri": "http://%s/jwks", "introspection_endpoint": "http://%s/introspect"}`, r.Host, r.Host, r.Host)
985 return
986 }
987 if r.URL.Path == "/jwks" {
988 w.Header().Set("Content-Type", "application/json")
989 fmt.Fprint(w, `{"keys": []}`)
990 return
991 }
992 if r.URL.Path == "/introspect" {
993 w.Header().Set("Content-Type", "application/json")
994 w.WriteHeader(mockStatus)
995 if mockRawResponse != "" {
996 _, _ = w.Write([]byte(mockRawResponse))
997 } else {
998 respCopy := make(map[string]any)
999 for k, v := range mockResponse {
1000 respCopy[k] = v
1001 }
1002 if _, hasIss := respCopy["iss"]; !hasIss {
1003 respCopy["iss"] = "http://" + r.Host
1004 }
1005 _ = json.NewEncoder(w).Encode(respCopy)
1006 }

Callers

nothing calls this directly

Calls 15

ListenMethod · 0.95
ServeMethod · 0.95
ShutdownMethod · 0.95
SetupOTelFunction · 0.92
NewStdLoggerFunction · 0.92
WithLoggerFunction · 0.92
WithInstrumentationFunction · 0.92
NewServerFunction · 0.92
AddMethod · 0.80
DoMethod · 0.80
UnmarshalMethod · 0.80

Tested by

no test coverage detected