@@ -7,14 +7,14 @@ jest.mock('../client/auth.js', () => {
77 return {
88 ...actual ,
99 auth : jest . fn ( ) ,
10- extractResourceMetadataUrl : jest . fn ( )
10+ extractWWWAuthenticateParams : jest . fn ( )
1111 } ;
1212} ) ;
1313
14- import { auth , extractResourceMetadataUrl } from './auth.js' ;
14+ import { auth , extractWWWAuthenticateParams } from './auth.js' ;
1515
1616const mockAuth = auth as jest . MockedFunction < typeof auth > ;
17- const mockExtractResourceMetadataUrl = extractResourceMetadataUrl as jest . MockedFunction < typeof extractResourceMetadataUrl > ;
17+ const mockExtractWWWAuthenticateParams = extractWWWAuthenticateParams as jest . MockedFunction < typeof extractWWWAuthenticateParams > ;
1818
1919describe ( 'withOAuth' , ( ) => {
2020 let mockProvider : jest . Mocked < OAuthClientProvider > ;
@@ -129,8 +129,11 @@ describe('withOAuth', () => {
129129
130130 mockFetch . mockResolvedValueOnce ( unauthorizedResponse ) . mockResolvedValueOnce ( successResponse ) ;
131131
132- const mockResourceUrl = new URL ( 'https://oauth.example.com/.well-known/oauth-protected-resource' ) ;
133- mockExtractResourceMetadataUrl . mockReturnValue ( mockResourceUrl ) ;
132+ const mockWWWAuthenticateParams = {
133+ resourceMetadataUrl : new URL ( 'https://oauth.example.com/.well-known/oauth-protected-resource' ) ,
134+ scope : 'read'
135+ } ;
136+ mockExtractWWWAuthenticateParams . mockReturnValue ( mockWWWAuthenticateParams ) ;
134137 mockAuth . mockResolvedValue ( 'AUTHORIZED' ) ;
135138
136139 const enhancedFetch = withOAuth ( mockProvider , 'https://api.example.com' ) ( mockFetch ) ;
@@ -141,7 +144,8 @@ describe('withOAuth', () => {
141144 expect ( mockFetch ) . toHaveBeenCalledTimes ( 2 ) ;
142145 expect ( mockAuth ) . toHaveBeenCalledWith ( mockProvider , {
143146 serverUrl : 'https://api.example.com' ,
144- resourceMetadataUrl : mockResourceUrl ,
147+ resourceMetadataUrl : mockWWWAuthenticateParams . resourceMetadataUrl ,
148+ scope : mockWWWAuthenticateParams . scope ,
145149 fetchFn : mockFetch
146150 } ) ;
147151
@@ -172,8 +176,11 @@ describe('withOAuth', () => {
172176
173177 mockFetch . mockResolvedValueOnce ( unauthorizedResponse ) . mockResolvedValueOnce ( successResponse ) ;
174178
175- const mockResourceUrl = new URL ( 'https://oauth.example.com/.well-known/oauth-protected-resource' ) ;
176- mockExtractResourceMetadataUrl . mockReturnValue ( mockResourceUrl ) ;
179+ const mockWWWAuthenticateParams = {
180+ resourceMetadataUrl : new URL ( 'https://oauth.example.com/.well-known/oauth-protected-resource' ) ,
181+ scope : 'read'
182+ } ;
183+ mockExtractWWWAuthenticateParams . mockReturnValue ( mockWWWAuthenticateParams ) ;
177184 mockAuth . mockResolvedValue ( 'AUTHORIZED' ) ;
178185
179186 // Test without baseUrl - should extract from request URL
@@ -185,7 +192,8 @@ describe('withOAuth', () => {
185192 expect ( mockFetch ) . toHaveBeenCalledTimes ( 2 ) ;
186193 expect ( mockAuth ) . toHaveBeenCalledWith ( mockProvider , {
187194 serverUrl : 'https://api.example.com' , // Should be extracted from request URL
188- resourceMetadataUrl : mockResourceUrl ,
195+ resourceMetadataUrl : mockWWWAuthenticateParams . resourceMetadataUrl ,
196+ scope : mockWWWAuthenticateParams . scope ,
189197 fetchFn : mockFetch
190198 } ) ;
191199
@@ -203,7 +211,7 @@ describe('withOAuth', () => {
203211 } ) ;
204212
205213 mockFetch . mockResolvedValue ( new Response ( 'Unauthorized' , { status : 401 } ) ) ;
206- mockExtractResourceMetadataUrl . mockReturnValue ( undefined ) ;
214+ mockExtractWWWAuthenticateParams . mockReturnValue ( { } ) ;
207215 mockAuth . mockResolvedValue ( 'REDIRECT' ) ;
208216
209217 // Test without baseUrl
@@ -222,7 +230,7 @@ describe('withOAuth', () => {
222230 } ) ;
223231
224232 mockFetch . mockResolvedValue ( new Response ( 'Unauthorized' , { status : 401 } ) ) ;
225- mockExtractResourceMetadataUrl . mockReturnValue ( undefined ) ;
233+ mockExtractWWWAuthenticateParams . mockReturnValue ( { } ) ;
226234 mockAuth . mockRejectedValue ( new Error ( 'Network error' ) ) ;
227235
228236 const enhancedFetch = withOAuth ( mockProvider , 'https://api.example.com' ) ( mockFetch ) ;
@@ -239,7 +247,7 @@ describe('withOAuth', () => {
239247
240248 // Always return 401
241249 mockFetch . mockResolvedValue ( new Response ( 'Unauthorized' , { status : 401 } ) ) ;
242- mockExtractResourceMetadataUrl . mockReturnValue ( undefined ) ;
250+ mockExtractWWWAuthenticateParams . mockReturnValue ( { } ) ;
243251 mockAuth . mockResolvedValue ( 'AUTHORIZED' ) ;
244252
245253 const enhancedFetch = withOAuth ( mockProvider , 'https://api.example.com' ) ( mockFetch ) ;
@@ -345,7 +353,7 @@ describe('withOAuth', () => {
345353
346354 mockFetch . mockResolvedValueOnce ( unauthorizedResponse ) . mockResolvedValueOnce ( successResponse ) ;
347355
348- mockExtractResourceMetadataUrl . mockReturnValue ( undefined ) ;
356+ mockExtractWWWAuthenticateParams . mockReturnValue ( { } ) ;
349357 mockAuth . mockResolvedValue ( 'AUTHORIZED' ) ;
350358
351359 const enhancedFetch = withOAuth ( mockProvider ) ( mockFetch ) ;
@@ -876,7 +884,10 @@ describe('Integration Tests', () => {
876884
877885 mockFetch . mockResolvedValueOnce ( unauthorizedResponse ) . mockResolvedValueOnce ( successResponse ) ;
878886
879- mockExtractResourceMetadataUrl . mockReturnValue ( new URL ( 'https://auth.example.com/.well-known/oauth-protected-resource' ) ) ;
887+ mockExtractWWWAuthenticateParams . mockReturnValue ( {
888+ resourceMetadataUrl : new URL ( 'https://auth.example.com/.well-known/oauth-protected-resource' ) ,
889+ scope : 'read'
890+ } ) ;
880891 mockAuth . mockResolvedValue ( 'AUTHORIZED' ) ;
881892
882893 // Use custom logger to avoid console output
@@ -896,6 +907,7 @@ describe('Integration Tests', () => {
896907 expect ( mockAuth ) . toHaveBeenCalledWith ( mockProvider , {
897908 serverUrl : 'https://mcp-server.example.com' ,
898909 resourceMetadataUrl : new URL ( 'https://auth.example.com/.well-known/oauth-protected-resource' ) ,
910+ scope : 'read' ,
899911 fetchFn : mockFetch
900912 } ) ;
901913 } ) ;
0 commit comments