Skip to content

Commit 8037de7

Browse files
committed
test: move tests of get auth server metadata
1 parent 76c429b commit 8037de7

2 files changed

Lines changed: 101 additions & 106 deletions

File tree

auth/authorization_code_test.go

Lines changed: 0 additions & 106 deletions
Original file line numberDiff line numberDiff line change
@@ -440,94 +440,6 @@ func TestGetProtectedResourceMetadata_Error(t *testing.T) {
440440
}
441441
}
442442

443-
func TestGetAuthServerMetadata(t *testing.T) {
444-
tests := []struct {
445-
name string
446-
issuerPath string
447-
endpointConfig *oauthtest.MetadataEndpointConfig
448-
wantFallback bool
449-
}{
450-
{
451-
name: "OAuthEndpoint_Root",
452-
issuerPath: "",
453-
endpointConfig: &oauthtest.MetadataEndpointConfig{
454-
ServeOAuthInsertedEndpoint: true,
455-
},
456-
},
457-
{
458-
name: "OpenIDEndpoint_Root",
459-
issuerPath: "",
460-
endpointConfig: &oauthtest.MetadataEndpointConfig{
461-
ServeOpenIDInsertedEndpoint: true,
462-
},
463-
},
464-
{
465-
name: "OAuthEndpoint_Path",
466-
issuerPath: "/oauth",
467-
endpointConfig: &oauthtest.MetadataEndpointConfig{
468-
ServeOAuthInsertedEndpoint: true,
469-
},
470-
},
471-
{
472-
name: "OpenIDEndpoint_Path",
473-
issuerPath: "/openid",
474-
endpointConfig: &oauthtest.MetadataEndpointConfig{
475-
ServeOpenIDInsertedEndpoint: true,
476-
},
477-
},
478-
{
479-
name: "OpenIDAppendedEndpoint_Path",
480-
issuerPath: "/openid",
481-
endpointConfig: &oauthtest.MetadataEndpointConfig{
482-
ServeOpenIDAppendedEndpoint: true,
483-
},
484-
},
485-
{
486-
name: "NoMetadata",
487-
issuerPath: "",
488-
endpointConfig: &oauthtest.MetadataEndpointConfig{
489-
// All metadata endpoints disabled.
490-
ServeOAuthInsertedEndpoint: false,
491-
ServeOpenIDInsertedEndpoint: false,
492-
},
493-
wantFallback: true,
494-
},
495-
}
496-
497-
for _, tt := range tests {
498-
t.Run(tt.name, func(t *testing.T) {
499-
s := oauthtest.NewFakeAuthorizationServer(oauthtest.Config{
500-
IssuerPath: tt.issuerPath,
501-
MetadataEndpointConfig: tt.endpointConfig,
502-
})
503-
s.Start(t)
504-
issuerURL := s.URL() + tt.issuerPath
505-
506-
got, err := GetAuthServerMetadata(t.Context(), issuerURL, http.DefaultClient)
507-
if tt.wantFallback {
508-
// When no metadata is found, GetAuthServerMetadata returns (nil, nil).
509-
// The fallback logic is now inline in Authorize.
510-
if err != nil {
511-
t.Fatalf("GetAuthServerMetadata() unexpected error = %v, want nil", err)
512-
}
513-
if got != nil {
514-
t.Fatal("GetAuthServerMetadata() expected nil for no metadata, got metadata")
515-
}
516-
return
517-
}
518-
if err != nil {
519-
t.Fatalf("GetAuthServerMetadata() error = %v, want nil", err)
520-
}
521-
if got == nil {
522-
t.Fatal("GetAuthServerMetadata() got nil, want metadata")
523-
}
524-
if got.Issuer != issuerURL {
525-
t.Errorf("GetAuthServerMetadata() issuer = %q, want %q", got.Issuer, issuerURL)
526-
}
527-
})
528-
}
529-
}
530-
531443
func TestSelectTokenAuthMethod(t *testing.T) {
532444
tests := []struct {
533445
name string
@@ -632,15 +544,6 @@ func TestHandleRegistration(t *testing.T) {
632544
if err != nil {
633545
t.Fatalf("GetAuthServerMetadata() unexpected error = %v", err)
634546
}
635-
if asm == nil {
636-
// Fallback to predefined endpoints for testing
637-
asm = &oauthex.AuthServerMeta{
638-
Issuer: s.URL(),
639-
AuthorizationEndpoint: s.URL() + "/authorize",
640-
TokenEndpoint: s.URL() + "/token",
641-
RegistrationEndpoint: s.URL() + "/register",
642-
}
643-
}
644547
got, err := handler.handleRegistration(t.Context(), asm)
645548
if err != nil {
646549
if !tt.wantError {
@@ -689,15 +592,6 @@ func TestDynamicRegistration(t *testing.T) {
689592
if err != nil {
690593
t.Fatalf("GetAuthServerMetadata() unexpected error = %v", err)
691594
}
692-
if asm == nil {
693-
// Fallback to predefined endpoints for testing
694-
asm = &oauthex.AuthServerMeta{
695-
Issuer: s.URL(),
696-
AuthorizationEndpoint: s.URL() + "/authorize",
697-
TokenEndpoint: s.URL() + "/token",
698-
RegistrationEndpoint: s.URL() + "/register",
699-
}
700-
}
701595
got, err := handler.handleRegistration(t.Context(), asm)
702596
if err != nil {
703597
t.Fatalf("handleRegistration() error = %v, want nil", err)

auth/shared_test.go

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
// Copyright 2026 The Go MCP SDK Authors. All rights reserved.
2+
// Use of this source code is governed by an MIT-style
3+
// license that can be found in the LICENSE file.
4+
5+
//go:build mcp_go_client_oauth
6+
7+
package auth
8+
9+
import (
10+
"net/http"
11+
"testing"
12+
13+
"github.com/modelcontextprotocol/go-sdk/internal/oauthtest"
14+
)
15+
16+
func TestGetAuthServerMetadata(t *testing.T) {
17+
tests := []struct {
18+
name string
19+
issuerPath string
20+
endpointConfig *oauthtest.MetadataEndpointConfig
21+
wantNil bool
22+
}{
23+
{
24+
name: "OAuthEndpoint_Root",
25+
issuerPath: "",
26+
endpointConfig: &oauthtest.MetadataEndpointConfig{
27+
ServeOAuthInsertedEndpoint: true,
28+
},
29+
},
30+
{
31+
name: "OpenIDEndpoint_Root",
32+
issuerPath: "",
33+
endpointConfig: &oauthtest.MetadataEndpointConfig{
34+
ServeOpenIDInsertedEndpoint: true,
35+
},
36+
},
37+
{
38+
name: "OAuthEndpoint_Path",
39+
issuerPath: "/oauth",
40+
endpointConfig: &oauthtest.MetadataEndpointConfig{
41+
ServeOAuthInsertedEndpoint: true,
42+
},
43+
},
44+
{
45+
name: "OpenIDEndpoint_Path",
46+
issuerPath: "/openid",
47+
endpointConfig: &oauthtest.MetadataEndpointConfig{
48+
ServeOpenIDInsertedEndpoint: true,
49+
},
50+
},
51+
{
52+
name: "OpenIDAppendedEndpoint_Path",
53+
issuerPath: "/openid",
54+
endpointConfig: &oauthtest.MetadataEndpointConfig{
55+
ServeOpenIDAppendedEndpoint: true,
56+
},
57+
},
58+
{
59+
name: "NoMetadata",
60+
issuerPath: "",
61+
endpointConfig: &oauthtest.MetadataEndpointConfig{
62+
// All metadata endpoints disabled.
63+
ServeOAuthInsertedEndpoint: false,
64+
ServeOpenIDInsertedEndpoint: false,
65+
},
66+
wantNil: true,
67+
},
68+
}
69+
70+
for _, tt := range tests {
71+
t.Run(tt.name, func(t *testing.T) {
72+
s := oauthtest.NewFakeAuthorizationServer(oauthtest.Config{
73+
IssuerPath: tt.issuerPath,
74+
MetadataEndpointConfig: tt.endpointConfig,
75+
})
76+
s.Start(t)
77+
issuerURL := s.URL() + tt.issuerPath
78+
79+
got, err := GetAuthServerMetadata(t.Context(), issuerURL, http.DefaultClient)
80+
if tt.wantNil {
81+
// When no metadata is found, GetAuthServerMetadata returns (nil, nil).
82+
if err != nil {
83+
t.Fatalf("GetAuthServerMetadata() unexpected error = %v, want nil", err)
84+
}
85+
if got != nil {
86+
t.Fatal("GetAuthServerMetadata() expected nil for no metadata, got metadata")
87+
}
88+
return
89+
}
90+
if err != nil {
91+
t.Fatalf("GetAuthServerMetadata() error = %v, want nil", err)
92+
}
93+
if got == nil {
94+
t.Fatal("GetAuthServerMetadata() got nil, want metadata")
95+
}
96+
if got.Issuer != issuerURL {
97+
t.Errorf("GetAuthServerMetadata() issuer = %q, want %q", got.Issuer, issuerURL)
98+
}
99+
})
100+
}
101+
}

0 commit comments

Comments
 (0)