@@ -168,36 +168,42 @@ describe("McpOAuthClientProvider", () => {
168168 expect ( provider . hasMetadata ) . toBe ( false )
169169 expect ( McpOAuthClientProvider . isKnownNonOAuth ( "https://no-oauth.example.com/mcp" ) ) . toBe ( true )
170170 } )
171- it ( "should initialize a static client with configured scopes and callback" , async ( ) => {
172- setupCallbackServerMock ( )
173- const provider = await McpOAuthClientProvider . create (
174- "https://example.com/mcp" ,
175- createMockSecretStorage ( ) ,
176- undefined ,
177- {
178- clientId : "registered-client" ,
179- scope : "openid profile offline_access" ,
180- redirectUri : "http://localhost:33418/callback" ,
181- resource : false ,
182- } ,
183- )
171+ it . each ( [
172+ [ "http://localhost:33418/callback" , "localhost" ] ,
173+ [ "http://127.0.0.1:33418/callback" , "127.0.0.1" ] ,
174+ ] as const ) (
175+ "should initialize a static client with configured scopes and callback %s" ,
176+ async ( redirectUri , callbackHost ) => {
177+ setupCallbackServerMock ( )
178+ const provider = await McpOAuthClientProvider . create (
179+ "https://example.com/mcp" ,
180+ createMockSecretStorage ( ) ,
181+ undefined ,
182+ {
183+ clientId : "registered-client" ,
184+ scope : "openid profile offline_access" ,
185+ redirectUri,
186+ resource : false ,
187+ } ,
188+ )
184189
185- expect ( await provider . clientInformation ( ) ) . toMatchObject ( {
186- client_id : "registered-client" ,
187- redirect_uris : [ "http://localhost:33418/callback" ] ,
188- token_endpoint_auth_method : "none" ,
189- } )
190- expect ( provider . redirectUrl ) . toBe ( "http://localhost:33418/callback" )
190+ expect ( await provider . clientInformation ( ) ) . toMatchObject ( {
191+ client_id : "registered-client" ,
192+ redirect_uris : [ redirectUri ] ,
193+ token_endpoint_auth_method : "none" ,
194+ } )
195+ expect ( provider . redirectUrl ) . toBe ( redirectUri )
191196
192- await provider . redirectToAuthorization (
193- new URL ( "https://auth.example.com/authorize?client_id=registered-client" ) ,
194- )
195- await provider . openBrowser ( )
196- const openedUri = vi . mocked ( vscode . env . openExternal ) . mock . calls [ 0 ] [ 0 ] . toString ( )
197- expect ( new URL ( openedUri ) . searchParams . get ( "scope" ) ) . toBe ( "openid profile offline_access" )
198- expect ( startCallbackServer ) . toHaveBeenCalledWith ( 33418 , expect . any ( String ) )
199- await provider . close ( )
200- } )
197+ await provider . redirectToAuthorization (
198+ new URL ( "https://auth.example.com/authorize?client_id=registered-client" ) ,
199+ )
200+ await provider . openBrowser ( )
201+ const openedUri = vi . mocked ( vscode . env . openExternal ) . mock . calls [ 0 ] [ 0 ] . toString ( )
202+ expect ( new URL ( openedUri ) . searchParams . get ( "scope" ) ) . toBe ( "openid profile offline_access" )
203+ expect ( startCallbackServer ) . toHaveBeenCalledWith ( 33418 , expect . any ( String ) , callbackHost )
204+ await provider . close ( )
205+ } ,
206+ )
201207
202208 it . each ( [
203209 "https://example.com:33418/callback" ,
@@ -603,22 +609,51 @@ describe("McpOAuthClientProvider", () => {
603609 } )
604610
605611 it ( "should preserve an SDK resource when provider discovery finds no resource" , async ( ) => {
606- ; ( discoverOAuthProtectedResourceMetadata as any ) . mockResolvedValueOnce ( {
612+ vi . mocked ( discoverOAuthProtectedResourceMetadata ) . mockResolvedValueOnce ( {
613+ resource : "" ,
607614 authorization_servers : [ "https://auth.example.com" ] ,
608615 } )
616+ mockFetch
617+ . mockResolvedValueOnce ( {
618+ ok : true ,
619+ json : ( ) =>
620+ Promise . resolve ( {
621+ issuer : "https://auth.example.com" ,
622+ authorization_endpoint : "https://auth.example.com/authorize" ,
623+ token_endpoint : "https://auth.example.com/token" ,
624+ token_endpoint_auth_methods_supported : [ "none" ] ,
625+ grant_types_supported : [ "authorization_code" ] ,
626+ } ) ,
627+ } )
628+ . mockResolvedValueOnce ( {
629+ ok : true ,
630+ json : ( ) => Promise . resolve ( { access_token : "access-token" , token_type : "Bearer" } ) ,
631+ } )
609632 setupCallbackServerMock ( )
610- const provider = await McpOAuthClientProvider . create ( "https://example.com/mcp" , createMockSecretStorage ( ) )
633+ const provider = await McpOAuthClientProvider . create (
634+ "https://example.com/mcp" ,
635+ createMockSecretStorage ( ) ,
636+ undefined ,
637+ { clientId : "registered-client" } ,
638+ )
611639
640+ const resource = "https://example.com/"
612641 const sdkUrl = new URL (
613- " https://auth.example.com/authorize?client_id=abc &resource=https%3A%2F%2Fexample.com%2F &state=123" ,
642+ ` https://auth.example.com/authorize?client_id=registered-client &resource=${ encodeURIComponent ( resource ) } &state=123` ,
614643 )
615644 await provider . redirectToAuthorization ( sdkUrl )
616645 await provider . openBrowser ( )
646+ await provider . saveCodeVerifier ( "verifier" )
647+ await provider . exchangeCodeForTokens ( "authorization-code" )
617648
618649 const openedUri = vi . mocked ( vscode . env . openExternal ) . mock . calls [ 0 ] [ 0 ] . toString ( )
619650 const openedUrl = new URL ( openedUri )
620- expect ( openedUrl . searchParams . get ( "resource" ) ) . toBe ( "https://example.com/" )
651+ expect ( openedUrl . searchParams . get ( "resource" ) ) . toBe ( resource )
621652 expect ( openedUrl . searchParams . getAll ( "resource" ) ) . toHaveLength ( 1 )
653+
654+ const tokenCall = mockFetch . mock . calls . at ( - 1 )
655+ const tokenBody = new URLSearchParams ( tokenCall ?. [ 1 ] ?. body as string )
656+ expect ( tokenBody . get ( "resource" ) ) . toBe ( resource )
622657 await provider . close ( )
623658 } )
624659
0 commit comments