| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package auth |
| 6 |
|
| 7 |
import ( |
| 8 |
"net/http" |
| 9 |
"testing" |
| 10 |
|
| 11 |
"github.com/modelcontextprotocol/go-sdk/internal/oauthtest" |
| 12 |
) |
| 13 |
|
| 14 |
func TestGetAuthServerMetadata(t *testing.T) { |
| 15 |
tests := []struct { |
| 16 |
name string |
| 17 |
issuerPath string |
| 18 |
endpointConfig *oauthtest.MetadataEndpointConfig |
| 19 |
wantNil bool |
| 20 |
}{ |
| 21 |
{ |
| 22 |
name: "OAuthEndpoint_Root", |
| 23 |
issuerPath: "", |
| 24 |
endpointConfig: &oauthtest.MetadataEndpointConfig{ |
| 25 |
ServeOAuthInsertedEndpoint: true, |
| 26 |
}, |
| 27 |
}, |
| 28 |
{ |
| 29 |
name: "OpenIDEndpoint_Root", |
| 30 |
issuerPath: "", |
| 31 |
endpointConfig: &oauthtest.MetadataEndpointConfig{ |
| 32 |
ServeOpenIDInsertedEndpoint: true, |
| 33 |
}, |
| 34 |
}, |
| 35 |
{ |
| 36 |
name: "OAuthEndpoint_Path", |
| 37 |
issuerPath: "/oauth", |
| 38 |
endpointConfig: &oauthtest.MetadataEndpointConfig{ |
| 39 |
ServeOAuthInsertedEndpoint: true, |
| 40 |
}, |
| 41 |
}, |
| 42 |
{ |
| 43 |
name: "OpenIDEndpoint_Path", |
| 44 |
issuerPath: "/openid", |
| 45 |
endpointConfig: &oauthtest.MetadataEndpointConfig{ |
| 46 |
ServeOpenIDInsertedEndpoint: true, |
| 47 |
}, |
| 48 |
}, |
| 49 |
{ |
| 50 |
name: "OpenIDAppendedEndpoint_Path", |
| 51 |
issuerPath: "/openid", |
| 52 |
endpointConfig: &oauthtest.MetadataEndpointConfig{ |
| 53 |
ServeOpenIDAppendedEndpoint: true, |
| 54 |
}, |
| 55 |
}, |
| 56 |
{ |
| 57 |
name: "NoMetadata", |
| 58 |
issuerPath: "", |
| 59 |
endpointConfig: &oauthtest.MetadataEndpointConfig{ |
| 60 |
|
| 61 |
ServeOAuthInsertedEndpoint: false, |
| 62 |
ServeOpenIDInsertedEndpoint: false, |
| 63 |
}, |
| 64 |
wantNil: true, |
| 65 |
}, |
| 66 |
} |
| 67 |
|
| 68 |
for _, tt := range tests { |
| 69 |
t.Run(tt.name, func(t *testing.T) { |
| 70 |
s := oauthtest.NewFakeAuthorizationServer(oauthtest.Config{ |
| 71 |
IssuerPath: tt.issuerPath, |
| 72 |
MetadataEndpointConfig: tt.endpointConfig, |
| 73 |
}) |
| 74 |
s.Start(t) |
| 75 |
issuerURL := s.URL() + tt.issuerPath |
| 76 |
|
| 77 |
got, err := GetAuthServerMetadata(t.Context(), issuerURL, http.DefaultClient) |
| 78 |
if tt.wantNil { |
| 79 |
|
| 80 |
if err != nil { |
| 81 |
t.Fatalf("GetAuthServerMetadata() unexpected error = %v, want nil", err) |
| 82 |
} |
| 83 |
if got != nil { |
| 84 |
t.Fatal("GetAuthServerMetadata() expected nil for no metadata, got metadata") |
| 85 |
} |
| 86 |
return |
| 87 |
} |
| 88 |
if err != nil { |
| 89 |
t.Fatalf("GetAuthServerMetadata() error = %v, want nil", err) |
| 90 |
} |
| 91 |
if got == nil { |
| 92 |
t.Fatal("GetAuthServerMetadata() got nil, want metadata") |
| 93 |
} |
| 94 |
if got.Issuer != issuerURL { |
| 95 |
t.Errorf("GetAuthServerMetadata() issuer = %q, want %q", got.Issuer, issuerURL) |
| 96 |
} |
| 97 |
}) |
| 98 |
} |
| 99 |
} |