|
1 | 1 | package oauth |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "context" |
4 | 5 | "crypto/tls" |
5 | 6 | "encoding/json" |
6 | 7 | "net/http" |
7 | 8 | "net/http/httptest" |
| 9 | + "net/url" |
8 | 10 | "testing" |
9 | 11 |
|
10 | 12 | "github.com/github/github-mcp-server/pkg/http/headers" |
| 13 | + "github.com/github/github-mcp-server/pkg/utils" |
11 | 14 | "github.com/go-chi/chi/v5" |
12 | 15 | "github.com/stretchr/testify/assert" |
13 | 16 | "github.com/stretchr/testify/require" |
14 | 17 | ) |
15 | 18 |
|
| 19 | +// mockAPIHostResolver is a test implementation of utils.APIHostResolver |
| 20 | +type mockAPIHostResolver struct { |
| 21 | + oauthURL string |
| 22 | +} |
| 23 | + |
| 24 | +func (m mockAPIHostResolver) BaseRESTURL(_ context.Context) (*url.URL, error) { |
| 25 | + return nil, nil |
| 26 | +} |
| 27 | + |
| 28 | +func (m mockAPIHostResolver) GraphqlURL(_ context.Context) (*url.URL, error) { |
| 29 | + return nil, nil |
| 30 | +} |
| 31 | + |
| 32 | +func (m mockAPIHostResolver) UploadURL(_ context.Context) (*url.URL, error) { |
| 33 | + return nil, nil |
| 34 | +} |
| 35 | + |
| 36 | +func (m mockAPIHostResolver) RawURL(_ context.Context) (*url.URL, error) { |
| 37 | + return nil, nil |
| 38 | +} |
| 39 | + |
| 40 | +func (m mockAPIHostResolver) OAuthURL(_ context.Context) (*url.URL, error) { |
| 41 | + return url.Parse(m.oauthURL) |
| 42 | +} |
| 43 | + |
| 44 | +// Ensure mockAPIHostResolver implements utils.APIHostResolver |
| 45 | +var _ utils.APIHostResolver = mockAPIHostResolver{} |
| 46 | + |
16 | 47 | func TestNewAuthHandler(t *testing.T) { |
17 | 48 | t.Parallel() |
18 | 49 |
|
@@ -51,6 +82,31 @@ func TestNewAuthHandler(t *testing.T) { |
51 | 82 | expectedAuthServer: DefaultAuthorizationServer, |
52 | 83 | expectedResourcePath: "/mcp", |
53 | 84 | }, |
| 85 | + { |
| 86 | + name: "APIHost with HTTPS GHES", |
| 87 | + cfg: &Config{ |
| 88 | + APIHost: mockAPIHostResolver{oauthURL: "https://ghes.example.com/login/oauth"}, |
| 89 | + }, |
| 90 | + expectedAuthServer: "https://ghes.example.com/login/oauth", |
| 91 | + expectedResourcePath: "", |
| 92 | + }, |
| 93 | + { |
| 94 | + name: "APIHost with HTTP GHES", |
| 95 | + cfg: &Config{ |
| 96 | + APIHost: mockAPIHostResolver{oauthURL: "http://ghes.local/login/oauth"}, |
| 97 | + }, |
| 98 | + expectedAuthServer: "http://ghes.local/login/oauth", |
| 99 | + expectedResourcePath: "", |
| 100 | + }, |
| 101 | + { |
| 102 | + name: "APIHost takes precedence over AuthorizationServer", |
| 103 | + cfg: &Config{ |
| 104 | + APIHost: mockAPIHostResolver{oauthURL: "https://ghes.example.com/login/oauth"}, |
| 105 | + AuthorizationServer: "https://should-be-ignored.example.com/oauth", |
| 106 | + }, |
| 107 | + expectedAuthServer: "https://ghes.example.com/login/oauth", |
| 108 | + expectedResourcePath: "", |
| 109 | + }, |
54 | 110 | } |
55 | 111 |
|
56 | 112 | for _, tc := range tests { |
|
0 commit comments