@@ -75,9 +75,11 @@ describe("fetchOAuthAuthServerMetadata", () => {
7575 headers : { "Content-Type" : "application/json" , Accept : "application/json, text/event-stream" } ,
7676 } )
7777 expect ( JSON . parse ( probeInit . body as string ) ) . toMatchObject ( { jsonrpc : "2.0" , method : "initialize" } )
78- expect ( discoverOAuthProtectedResourceMetadata ) . toHaveBeenLastCalledWith ( "https://example.com/core/mcp" , {
79- resourceMetadataUrl : new URL ( "https://example.com/core/.well-known/oauth-protected-resource/mcp" ) ,
80- } )
78+ expect ( discoverOAuthProtectedResourceMetadata ) . toHaveBeenLastCalledWith (
79+ "https://example.com/core/mcp" ,
80+ { resourceMetadataUrl : new URL ( "https://example.com/core/.well-known/oauth-protected-resource/mcp" ) } ,
81+ expect . any ( Function ) ,
82+ )
8183 expect ( result ) . toEqual ( { authServerMeta : meta , resourceIndicator : "https://example.com/core/mcp" } )
8284 } )
8385
@@ -124,21 +126,60 @@ describe("fetchOAuthAuthServerMetadata", () => {
124126 }
125127 } )
126128
127- it ( "returns null when a protected resource metadata lookup exceeds its timeout" , async ( ) => {
129+ it ( "aborts a protected resource metadata lookup that exceeds its timeout" , async ( ) => {
128130 vi . useFakeTimers ( )
129131 try {
130- vi . mocked ( discoverOAuthProtectedResourceMetadata ) . mockImplementationOnce ( ( ) => new Promise ( ( ) => { } ) )
131- mockFetch . mockResolvedValueOnce ( new Response ( null , { status : 200 } ) )
132+ vi . mocked ( discoverOAuthProtectedResourceMetadata ) . mockImplementationOnce ( async ( url , _opts , fetchFn ) => {
133+ await fetchFn ! ( url )
134+ throw new Error ( "unreachable" )
135+ } )
136+ let lookupSignal : AbortSignal | undefined
137+ mockFetch
138+ . mockImplementationOnce ( ( _url : string , init : RequestInit ) => {
139+ lookupSignal = init . signal ?? undefined
140+ return new Promise ( ( ) => { } )
141+ } )
142+ . mockResolvedValueOnce ( new Response ( null , { status : 200 } ) )
132143
133144 const result = fetchOAuthAuthServerMetadata ( "https://example.com/mcp" )
134145 await vi . advanceTimersByTimeAsync ( 5_000 )
135146
136147 await expect ( result ) . resolves . toBeNull ( )
148+ expect ( lookupSignal ?. aborted ) . toBe ( true )
137149 } finally {
138150 vi . useRealTimers ( )
139151 }
140152 } )
141153
154+ it ( "rejects well-known metadata whose resource does not match the server URL" , async ( ) => {
155+ vi . mocked ( discoverOAuthProtectedResourceMetadata ) . mockResolvedValueOnce ( {
156+ resource : "https://other.example.com/mcp" ,
157+ authorization_servers : [ "https://auth.example.com" ] ,
158+ } )
159+ mockFetch . mockResolvedValueOnce ( new Response ( null , { status : 200 } ) )
160+
161+ const result = await fetchOAuthAuthServerMetadata ( "https://example.com/mcp" )
162+
163+ expect ( result ) . toBeNull ( )
164+ expect ( mockFetch ) . toHaveBeenCalledTimes ( 1 )
165+ } )
166+
167+ it ( "rejects fallback metadata whose resource does not match the server URL" , async ( ) => {
168+ vi . mocked ( discoverOAuthProtectedResourceMetadata )
169+ . mockRejectedValueOnce ( new Error ( "not found" ) )
170+ . mockResolvedValueOnce ( {
171+ resource : "https://example.com/other" ,
172+ authorization_servers : [ "https://auth.example.com" ] ,
173+ } )
174+ mockFetch . mockResolvedValueOnce ( unauthorized ( 'Bearer resource_metadata="https://example.com/metadata"' ) )
175+
176+ const result = await fetchOAuthAuthServerMetadata ( "https://example.com/mcp" )
177+
178+ expect ( result ) . toBeNull ( )
179+ expect ( discoverOAuthProtectedResourceMetadata ) . toHaveBeenCalledTimes ( 2 )
180+ expect ( mockFetch ) . toHaveBeenCalledTimes ( 1 )
181+ } )
182+
142183 it ( "constructs the RFC 8414 discovery URL correctly for an issuer with a path" , async ( ) => {
143184 ; ( discoverOAuthProtectedResourceMetadata as any ) . mockResolvedValue ( {
144185 resource : "https://mcp.kapa.ai/" ,
0 commit comments