@@ -12,16 +12,16 @@ jest.mock("../client/auth.js", () => {
1212 return {
1313 ...actual ,
1414 auth : jest . fn ( ) ,
15- extractResourceMetadataUrl : jest . fn ( ) ,
15+ extractWWWAuthenticateParams : jest . fn ( ) ,
1616 } ;
1717} ) ;
1818
19- import { auth , extractResourceMetadataUrl } from "./auth.js" ;
19+ import { auth , extractWWWAuthenticateParams } from "./auth.js" ;
2020
2121const mockAuth = auth as jest . MockedFunction < typeof auth > ;
22- const mockExtractResourceMetadataUrl =
23- extractResourceMetadataUrl as jest . MockedFunction <
24- typeof extractResourceMetadataUrl
22+ const mockExtractWWWAuthenticateParams =
23+ extractWWWAuthenticateParams as jest . MockedFunction <
24+ typeof extractWWWAuthenticateParams
2525 > ;
2626
2727describe ( "withOAuth" , ( ) => {
@@ -142,10 +142,13 @@ describe("withOAuth", () => {
142142 . mockResolvedValueOnce ( unauthorizedResponse )
143143 . mockResolvedValueOnce ( successResponse ) ;
144144
145- const mockResourceUrl = new URL (
146- "https://oauth.example.com/.well-known/oauth-protected-resource" ,
147- ) ;
148- mockExtractResourceMetadataUrl . mockReturnValue ( mockResourceUrl ) ;
145+ const mockWWWAuthenticateParams = {
146+ resourceMetadataUrl : new URL (
147+ "https://oauth.example.com/.well-known/oauth-protected-resource" ,
148+ ) ,
149+ scope : "read" ,
150+ }
151+ mockExtractWWWAuthenticateParams . mockReturnValue ( mockWWWAuthenticateParams ) ;
149152 mockAuth . mockResolvedValue ( "AUTHORIZED" ) ;
150153
151154 const enhancedFetch = withOAuth (
@@ -159,7 +162,8 @@ describe("withOAuth", () => {
159162 expect ( mockFetch ) . toHaveBeenCalledTimes ( 2 ) ;
160163 expect ( mockAuth ) . toHaveBeenCalledWith ( mockProvider , {
161164 serverUrl : "https://api.example.com" ,
162- resourceMetadataUrl : mockResourceUrl ,
165+ resourceMetadataUrl : mockWWWAuthenticateParams . resourceMetadataUrl ,
166+ scope : mockWWWAuthenticateParams . scope ,
163167 fetchFn : mockFetch ,
164168 } ) ;
165169
@@ -192,10 +196,13 @@ describe("withOAuth", () => {
192196 . mockResolvedValueOnce ( unauthorizedResponse )
193197 . mockResolvedValueOnce ( successResponse ) ;
194198
195- const mockResourceUrl = new URL (
196- "https://oauth.example.com/.well-known/oauth-protected-resource" ,
197- ) ;
198- mockExtractResourceMetadataUrl . mockReturnValue ( mockResourceUrl ) ;
199+ const mockWWWAuthenticateParams = {
200+ resourceMetadataUrl : new URL (
201+ "https://oauth.example.com/.well-known/oauth-protected-resource" ,
202+ ) ,
203+ scope : "read" ,
204+ }
205+ mockExtractWWWAuthenticateParams . mockReturnValue ( mockWWWAuthenticateParams ) ;
199206 mockAuth . mockResolvedValue ( "AUTHORIZED" ) ;
200207
201208 // Test without baseUrl - should extract from request URL
@@ -207,7 +214,8 @@ describe("withOAuth", () => {
207214 expect ( mockFetch ) . toHaveBeenCalledTimes ( 2 ) ;
208215 expect ( mockAuth ) . toHaveBeenCalledWith ( mockProvider , {
209216 serverUrl : "https://api.example.com" , // Should be extracted from request URL
210- resourceMetadataUrl : mockResourceUrl ,
217+ resourceMetadataUrl : mockWWWAuthenticateParams . resourceMetadataUrl ,
218+ scope : mockWWWAuthenticateParams . scope ,
211219 fetchFn : mockFetch ,
212220 } ) ;
213221
@@ -225,7 +233,7 @@ describe("withOAuth", () => {
225233 } ) ;
226234
227235 mockFetch . mockResolvedValue ( new Response ( "Unauthorized" , { status : 401 } ) ) ;
228- mockExtractResourceMetadataUrl . mockReturnValue ( undefined ) ;
236+ mockExtractWWWAuthenticateParams . mockReturnValue ( { } ) ;
229237 mockAuth . mockResolvedValue ( "REDIRECT" ) ;
230238
231239 // Test without baseUrl
@@ -244,7 +252,7 @@ describe("withOAuth", () => {
244252 } ) ;
245253
246254 mockFetch . mockResolvedValue ( new Response ( "Unauthorized" , { status : 401 } ) ) ;
247- mockExtractResourceMetadataUrl . mockReturnValue ( undefined ) ;
255+ mockExtractWWWAuthenticateParams . mockReturnValue ( { } ) ;
248256 mockAuth . mockRejectedValue ( new Error ( "Network error" ) ) ;
249257
250258 const enhancedFetch = withOAuth (
@@ -266,7 +274,7 @@ describe("withOAuth", () => {
266274
267275 // Always return 401
268276 mockFetch . mockResolvedValue ( new Response ( "Unauthorized" , { status : 401 } ) ) ;
269- mockExtractResourceMetadataUrl . mockReturnValue ( undefined ) ;
277+ mockExtractWWWAuthenticateParams . mockReturnValue ( { } ) ;
270278 mockAuth . mockResolvedValue ( "AUTHORIZED" ) ;
271279
272280 const enhancedFetch = withOAuth (
@@ -383,7 +391,7 @@ describe("withOAuth", () => {
383391 . mockResolvedValueOnce ( unauthorizedResponse )
384392 . mockResolvedValueOnce ( successResponse ) ;
385393
386- mockExtractResourceMetadataUrl . mockReturnValue ( undefined ) ;
394+ mockExtractWWWAuthenticateParams . mockReturnValue ( { } ) ;
387395 mockAuth . mockResolvedValue ( "AUTHORIZED" ) ;
388396
389397 const enhancedFetch = withOAuth ( mockProvider ) ( mockFetch ) ;
@@ -964,8 +972,11 @@ describe("Integration Tests", () => {
964972 . mockResolvedValueOnce ( unauthorizedResponse )
965973 . mockResolvedValueOnce ( successResponse ) ;
966974
967- mockExtractResourceMetadataUrl . mockReturnValue (
968- new URL ( "https://auth.example.com/.well-known/oauth-protected-resource" ) ,
975+ mockExtractWWWAuthenticateParams . mockReturnValue (
976+ {
977+ resourceMetadataUrl : new URL ( "https://auth.example.com/.well-known/oauth-protected-resource" ) ,
978+ scope : "read" ,
979+ }
969980 ) ;
970981 mockAuth . mockResolvedValue ( "AUTHORIZED" ) ;
971982
@@ -994,6 +1005,7 @@ describe("Integration Tests", () => {
9941005 resourceMetadataUrl : new URL (
9951006 "https://auth.example.com/.well-known/oauth-protected-resource" ,
9961007 ) ,
1008+ scope : "read" ,
9971009 fetchFn : mockFetch ,
9981010 } ) ;
9991011 } ) ;
0 commit comments