@@ -1704,3 +1704,54 @@ func TestOAuthHandler_RFC8707_ResourceParameter(t *testing.T) {
17041704 assert .Equal (t , server .URL , capturedResource , "resource should fall back to baseURL" )
17051705 })
17061706}
1707+
1708+ // TestOAuthHandler_GetServerMetadata_AuthServerReturnsHTML tests that when the
1709+ // authorization server's .well-known endpoint returns 200 with HTML (e.g. a login
1710+ // page) instead of JSON, the fallback chain is not poisoned and default endpoints
1711+ // are used successfully.
1712+ func TestOAuthHandler_GetServerMetadata_AuthServerReturnsHTML (t * testing.T ) {
1713+ // Create a separate "auth server" that returns HTML at its .well-known endpoints
1714+ authServer := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
1715+ // Return 200 with HTML for all requests (simulating a login page)
1716+ w .Header ().Set ("Content-Type" , "text/html; charset=utf-8" )
1717+ w .WriteHeader (http .StatusOK )
1718+ _ , _ = w .Write ([]byte ("<html><body>Login Page</body></html>" ))
1719+ }))
1720+ defer authServer .Close ()
1721+
1722+ // Create the MCP server that returns valid protected resource metadata
1723+ // pointing to the auth server above
1724+ var mcpServerURL string
1725+ mcpServer := httptest .NewServer (http .HandlerFunc (func (w http.ResponseWriter , r * http.Request ) {
1726+ if r .URL .Path == "/.well-known/oauth-protected-resource" {
1727+ w .Header ().Set ("Content-Type" , "application/json" )
1728+ _ = json .NewEncoder (w ).Encode (map [string ]any {
1729+ "resource" : mcpServerURL ,
1730+ "authorization_servers" : []string {authServer .URL },
1731+ })
1732+ return
1733+ }
1734+ w .WriteHeader (http .StatusUnauthorized )
1735+ }))
1736+ mcpServerURL = mcpServer .URL
1737+ defer mcpServer .Close ()
1738+
1739+ config := OAuthConfig {
1740+ ClientID : "test-client" ,
1741+ RedirectURI : mcpServer .URL + "/callback" ,
1742+ Scopes : []string {"mcp.read" },
1743+ TokenStore : NewMemoryTokenStore (),
1744+ PKCEEnabled : true ,
1745+ }
1746+
1747+ handler := NewOAuthHandler (config )
1748+ handler .SetBaseURL (mcpServer .URL )
1749+
1750+ metadata , err := handler .GetServerMetadata (context .Background ())
1751+ require .NoError (t , err , "Should fall back to default endpoints when auth server returns HTML" )
1752+
1753+ // Verify default endpoints were derived from the auth server URL
1754+ assert .Equal (t , authServer .URL + "/authorize" , metadata .AuthorizationEndpoint )
1755+ assert .Equal (t , authServer .URL + "/token" , metadata .TokenEndpoint )
1756+ assert .Equal (t , authServer .URL + "/register" , metadata .RegistrationEndpoint )
1757+ }
0 commit comments