@@ -79,6 +79,7 @@ describe("fetchOAuthAuthServerMetadata", () => {
7979 expect ( probeUrl ) . toBe ( "https://example.com/core/mcp" )
8080 expect ( probeInit ) . toMatchObject ( {
8181 method : "POST" ,
82+ redirect : "manual" ,
8283 headers : { "Content-Type" : "application/json" , Accept : "application/json, text/event-stream" } ,
8384 } )
8485 expect ( JSON . parse ( probeInit . body as string ) ) . toMatchObject ( { jsonrpc : "2.0" , method : "initialize" } )
@@ -163,30 +164,44 @@ describe("fetchOAuthAuthServerMetadata", () => {
163164 expect ( mockFetch ) . toHaveBeenCalledTimes ( 1 )
164165 } )
165166
166- /** Makes the next SDK lookup fetch through its hook and never settle; returns the request signal. */
167+ it ( "returns null when the probe is redirected" , async ( ) => {
168+ vi . mocked ( discoverOAuthProtectedResourceMetadata ) . mockRejectedValueOnce ( new Error ( "not found" ) )
169+ mockFetch . mockResolvedValueOnce (
170+ new Response ( null , { status : 307 , headers : { Location : "http://127.0.0.1:8080/mcp" } } ) ,
171+ )
172+
173+ const result = await fetchOAuthAuthServerMetadata ( "https://example.com/mcp" )
174+
175+ expect ( result ) . toBeNull ( )
176+ expect ( mockFetch ) . toHaveBeenCalledTimes ( 1 )
177+ expect ( discoverOAuthProtectedResourceMetadata ) . toHaveBeenCalledTimes ( 1 )
178+ } )
179+
180+ /** Makes the next SDK lookup fetch through its hook and never settle; returns the request init. */
167181 const hangNextLookup = ( ) => {
168- const signal : { current ?: AbortSignal } = { }
182+ const request : { init ?: RequestInit } = { }
169183 vi . mocked ( discoverOAuthProtectedResourceMetadata ) . mockImplementationOnce ( async ( url , _opts , fetchFn ) => {
170184 await fetchFn ! ( url )
171185 throw new Error ( "unreachable" )
172186 } )
173187 mockFetch . mockImplementationOnce ( ( _url : string , init : RequestInit ) => {
174- signal . current = init . signal ?? undefined
188+ request . init = init
175189 return new Promise ( ( ) => { } )
176190 } )
177- return signal
191+ return request
178192 }
179193
180194 it ( "aborts a timed-out well-known lookup and does not start the fallback" , async ( ) => {
181195 vi . useFakeTimers ( )
182196 try {
183- const lookupSignal = hangNextLookup ( )
197+ const lookup = hangNextLookup ( )
184198
185199 const result = fetchOAuthAuthServerMetadata ( "https://example.com/mcp" )
186200 await vi . advanceTimersByTimeAsync ( 5_000 )
187201
188202 await expect ( result ) . resolves . toBeNull ( )
189- expect ( lookupSignal . current ?. aborted ) . toBe ( true )
203+ expect ( lookup . init ?. signal ?. aborted ) . toBe ( true )
204+ expect ( lookup . init ?. redirect ) . toBeUndefined ( )
190205 expect ( mockFetch ) . toHaveBeenCalledTimes ( 1 )
191206 expect ( discoverOAuthProtectedResourceMetadata ) . toHaveBeenCalledTimes ( 1 )
192207 } finally {
@@ -199,13 +214,14 @@ describe("fetchOAuthAuthServerMetadata", () => {
199214 try {
200215 vi . mocked ( discoverOAuthProtectedResourceMetadata ) . mockRejectedValueOnce ( new Error ( "not found" ) )
201216 mockFetch . mockResolvedValueOnce ( unauthorized ( 'Bearer resource_metadata="https://example.com/metadata"' ) )
202- const lookupSignal = hangNextLookup ( )
217+ const lookup = hangNextLookup ( )
203218
204219 const result = fetchOAuthAuthServerMetadata ( "https://example.com/mcp" )
205220 await vi . advanceTimersByTimeAsync ( 5_000 )
206221
207222 await expect ( result ) . resolves . toBeNull ( )
208- expect ( lookupSignal . current ?. aborted ) . toBe ( true )
223+ expect ( lookup . init ?. signal ?. aborted ) . toBe ( true )
224+ expect ( lookup . init ?. redirect ) . toBe ( "manual" )
209225 expect ( discoverOAuthProtectedResourceMetadata ) . toHaveBeenCalledTimes ( 2 )
210226 } finally {
211227 vi . useRealTimers ( )
0 commit comments