88	"crypto/rsa" 
99	"flag" 
1010	"fmt" 
11+ 	"io" 
1112	"net" 
1213	"net/http" 
1314	"net/http/httptest" 
@@ -334,7 +335,28 @@ func TestWellKnownReverseProxy(t *testing.T) {
334335			})
335336		}
336337	})
337- 	// With Authorization URL configured 
338+ 	// With Authorization URL configured but invalid payload 
339+ 	invalidPayloadServer  :=  httptest .NewServer (http .HandlerFunc (func (w  http.ResponseWriter , r  * http.Request ) {
340+ 		w .Header ().Set ("Content-Type" , "application/json" )
341+ 		_ , _  =  w .Write ([]byte (`NOT A JSON PAYLOAD` ))
342+ 	}))
343+ 	t .Cleanup (invalidPayloadServer .Close )
344+ 	invalidPayloadConfig  :=  & config.StaticConfig {AuthorizationURL : invalidPayloadServer .URL , RequireOAuth : true , ValidateToken : true }
345+ 	testCaseWithContext (t , & httpContext {StaticConfig : invalidPayloadConfig }, func (ctx  * httpContext ) {
346+ 		for  _ , path  :=  range  cases  {
347+ 			resp , err  :=  http .Get (fmt .Sprintf ("http://%s/%s" , ctx .HttpAddress , path ))
348+ 			t .Cleanup (func () { _  =  resp .Body .Close () })
349+ 			t .Run ("Protected resource '" + path + "' with invalid Authorization URL payload returns 500 - Internal Server Error" , func (t  * testing.T ) {
350+ 				if  err  !=  nil  {
351+ 					t .Fatalf ("Failed to get %s endpoint: %v" , path , err )
352+ 				}
353+ 				if  resp .StatusCode  !=  http .StatusInternalServerError  {
354+ 					t .Errorf ("Expected HTTP 500 Internal Server Error, got %d" , resp .StatusCode )
355+ 				}
356+ 			})
357+ 		}
358+ 	})
359+ 	// With Authorization URL configured and valid payload 
338360	testServer  :=  httptest .NewServer (http .HandlerFunc (func (w  http.ResponseWriter , r  * http.Request ) {
339361		if  ! strings .HasPrefix (r .URL .EscapedPath (), "/.well-known/" ) {
340362			http .NotFound (w , r )
@@ -344,7 +366,8 @@ func TestWellKnownReverseProxy(t *testing.T) {
344366		_ , _  =  w .Write ([]byte (`{"issuer": "https://example.com","scopes_supported":["mcp-server"]}` ))
345367	}))
346368	t .Cleanup (testServer .Close )
347- 	testCaseWithContext (t , & httpContext {StaticConfig : & config.StaticConfig {AuthorizationURL : testServer .URL , RequireOAuth : true , ValidateToken : true }}, func (ctx  * httpContext ) {
369+ 	staticConfig  :=  & config.StaticConfig {AuthorizationURL : testServer .URL , RequireOAuth : true , ValidateToken : true }
370+ 	testCaseWithContext (t , & httpContext {StaticConfig : staticConfig }, func (ctx  * httpContext ) {
348371		for  _ , path  :=  range  cases  {
349372			resp , err  :=  http .Get (fmt .Sprintf ("http://%s/%s" , ctx .HttpAddress , path ))
350373			t .Cleanup (func () { _  =  resp .Body .Close () })
@@ -365,6 +388,87 @@ func TestWellKnownReverseProxy(t *testing.T) {
365388	})
366389}
367390
391+ func  TestWellKnownOverrides (t  * testing.T ) {
392+ 	cases  :=  []string {
393+ 		".well-known/oauth-authorization-server" ,
394+ 		".well-known/oauth-protected-resource" ,
395+ 		".well-known/openid-configuration" ,
396+ 	}
397+ 	testServer  :=  httptest .NewServer (http .HandlerFunc (func (w  http.ResponseWriter , r  * http.Request ) {
398+ 		if  ! strings .HasPrefix (r .URL .EscapedPath (), "/.well-known/" ) {
399+ 			http .NotFound (w , r )
400+ 			return 
401+ 		}
402+ 		w .Header ().Set ("Content-Type" , "application/json" )
403+ 		_ , _  =  w .Write ([]byte (` 
404+ 			{ 
405+ 				"issuer": "https://localhost", 
406+ 				"registration_endpoint": "https://localhost/clients-registrations/openid-connect", 
407+ 				"require_request_uri_registration": true, 
408+ 				"scopes_supported":["scope-1", "scope-2"] 
409+ 			}` ))
410+ 	}))
411+ 	t .Cleanup (testServer .Close )
412+ 	baseConfig  :=  config.StaticConfig {AuthorizationURL : testServer .URL , RequireOAuth : true , ValidateToken : true }
413+ 	// With Dynamic Client Registration disabled 
414+ 	disableDynamicRegistrationConfig  :=  baseConfig 
415+ 	disableDynamicRegistrationConfig .DisableDynamicClientRegistration  =  true 
416+ 	testCaseWithContext (t , & httpContext {StaticConfig : & disableDynamicRegistrationConfig }, func (ctx  * httpContext ) {
417+ 		for  _ , path  :=  range  cases  {
418+ 			resp , _  :=  http .Get (fmt .Sprintf ("http://%s/%s" , ctx .HttpAddress , path ))
419+ 			t .Cleanup (func () { _  =  resp .Body .Close () })
420+ 			body , err  :=  io .ReadAll (resp .Body )
421+ 			if  err  !=  nil  {
422+ 				t .Fatalf ("Failed to read response body: %v" , err )
423+ 			}
424+ 			t .Run ("DisableDynamicClientRegistration removes registration_endpoint field" , func (t  * testing.T ) {
425+ 				if  strings .Contains (string (body ), "registration_endpoint" ) {
426+ 					t .Error ("Expected registration_endpoint to be removed, but it was found in the response" )
427+ 				}
428+ 			})
429+ 			t .Run ("DisableDynamicClientRegistration sets require_request_uri_registration = false" , func (t  * testing.T ) {
430+ 				if  ! strings .Contains (string (body ), `"require_request_uri_registration":false` ) {
431+ 					t .Error ("Expected require_request_uri_registration to be false, but it was not found in the response" )
432+ 				}
433+ 			})
434+ 			t .Run ("DisableDynamicClientRegistration includes/preserves scopes_supported" , func (t  * testing.T ) {
435+ 				if  ! strings .Contains (string (body ), `"scopes_supported":["scope-1","scope-2"]` ) {
436+ 					t .Error ("Expected scopes_supported to be present, but it was not found in the response" )
437+ 				}
438+ 			})
439+ 		}
440+ 	})
441+ 	// With overrides for OAuth scopes (client/frontend) 
442+ 	oAuthScopesConfig  :=  baseConfig 
443+ 	oAuthScopesConfig .OAuthScopes  =  []string {"openid" , "mcp-server" }
444+ 	testCaseWithContext (t , & httpContext {StaticConfig : & oAuthScopesConfig }, func (ctx  * httpContext ) {
445+ 		for  _ , path  :=  range  cases  {
446+ 			resp , _  :=  http .Get (fmt .Sprintf ("http://%s/%s" , ctx .HttpAddress , path ))
447+ 			t .Cleanup (func () { _  =  resp .Body .Close () })
448+ 			body , err  :=  io .ReadAll (resp .Body )
449+ 			if  err  !=  nil  {
450+ 				t .Fatalf ("Failed to read response body: %v" , err )
451+ 			}
452+ 			t .Run ("OAuthScopes overrides scopes_supported" , func (t  * testing.T ) {
453+ 				if  ! strings .Contains (string (body ), `"scopes_supported":["openid","mcp-server"]` ) {
454+ 					t .Errorf ("Expected scopes_supported to be overridden, but original was preserved, response: %s" , string (body ))
455+ 				}
456+ 			})
457+ 			t .Run ("OAuthScopes preserves other fields" , func (t  * testing.T ) {
458+ 				if  ! strings .Contains (string (body ), `"issuer":"https://localhost"` ) {
459+ 					t .Errorf ("Expected issuer to be preserved, but got: %s" , string (body ))
460+ 				}
461+ 				if  ! strings .Contains (string (body ), `"registration_endpoint":"https://localhost` ) {
462+ 					t .Errorf ("Expected registration_endpoint to be preserved, but got: %s" , string (body ))
463+ 				}
464+ 				if  ! strings .Contains (string (body ), `"require_request_uri_registration":true` ) {
465+ 					t .Error ("Expected require_request_uri_registration to be true, but it was not found in the response" )
466+ 				}
467+ 			})
468+ 		}
469+ 	})
470+ }
471+ 
368472func  TestMiddlewareLogging (t  * testing.T ) {
369473	testCase (t , func (ctx  * httpContext ) {
370474		_ , _  =  http .Get (fmt .Sprintf ("http://%s/.well-known/oauth-protected-resource" , ctx .HttpAddress ))
0 commit comments