MCPcopy Create free account
hub / github.com/modelcontextprotocol/go-sdk / TestProtectedResourceMetadataHandler

Function TestProtectedResourceMetadataHandler

auth/auth_test.go:85–190  ·  view source on GitHub ↗
(t *testing.T)

Source from the content-addressed store, hash-verified

83}
84
85func TestProtectedResourceMetadataHandler(t *testing.T) {
86 metadata := &oauthex.ProtectedResourceMetadata{
87 Resource: "https://example.com/mcp",
88 AuthorizationServers: []string{
89 "https://auth.example.com/.well-known/openid-configuration",
90 },
91 ScopesSupported: []string{"read", "write"},
92 }
93
94 handler := ProtectedResourceMetadataHandler(metadata)
95
96 tests := []struct {
97 name string
98 method string
99 wantStatus int
100 checkJSON bool
101 }{
102 {
103 name: "GET returns metadata",
104 method: http.MethodGet,
105 wantStatus: http.StatusOK,
106 checkJSON: true,
107 },
108 {
109 name: "OPTIONS for CORS preflight",
110 method: http.MethodOptions,
111 wantStatus: http.StatusNoContent,
112 },
113 {
114 name: "POST not allowed",
115 method: http.MethodPost,
116 wantStatus: http.StatusMethodNotAllowed,
117 },
118 {
119 name: "PUT not allowed",
120 method: http.MethodPut,
121 wantStatus: http.StatusMethodNotAllowed,
122 },
123 {
124 name: "DELETE not allowed",
125 method: http.MethodDelete,
126 wantStatus: http.StatusMethodNotAllowed,
127 },
128 }
129
130 for _, tt := range tests {
131 t.Run(tt.name, func(t *testing.T) {
132 req := httptest.NewRequest(tt.method, "/.well-known/oauth-protected-resource", nil)
133 rec := httptest.NewRecorder()
134
135 handler.ServeHTTP(rec, req)
136
137 if rec.Code != tt.wantStatus {
138 t.Errorf("status = %d, want %d", rec.Code, tt.wantStatus)
139 }
140
141 // All responses should have CORS headers
142 if got := rec.Header().Get("Access-Control-Allow-Origin"); got != "*" {

Callers

nothing calls this directly

Calls 4

RunMethod · 0.80
DecodeMethod · 0.80
ServeHTTPMethod · 0.45

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…