(t *testing.T)
| 83 | } |
| 84 | |
| 85 | func 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 != "*" { |
nothing calls this directly
no test coverage detected
searching dependent graphs…