@@ -12,16 +12,16 @@ jest.mock("../client/auth.js", () => {
12
12
return {
13
13
...actual ,
14
14
auth : jest . fn ( ) ,
15
- extractResourceMetadataUrl : jest . fn ( ) ,
15
+ extractWWWAuthenticateParams : jest . fn ( ) ,
16
16
} ;
17
17
} ) ;
18
18
19
- import { auth , extractResourceMetadataUrl } from "./auth.js" ;
19
+ import { auth , extractWWWAuthenticateParams } from "./auth.js" ;
20
20
21
21
const 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
25
25
> ;
26
26
27
27
describe ( "withOAuth" , ( ) => {
@@ -142,10 +142,13 @@ describe("withOAuth", () => {
142
142
. mockResolvedValueOnce ( unauthorizedResponse )
143
143
. mockResolvedValueOnce ( successResponse ) ;
144
144
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 ) ;
149
152
mockAuth . mockResolvedValue ( "AUTHORIZED" ) ;
150
153
151
154
const enhancedFetch = withOAuth (
@@ -159,7 +162,8 @@ describe("withOAuth", () => {
159
162
expect ( mockFetch ) . toHaveBeenCalledTimes ( 2 ) ;
160
163
expect ( mockAuth ) . toHaveBeenCalledWith ( mockProvider , {
161
164
serverUrl : "https://api.example.com" ,
162
- resourceMetadataUrl : mockResourceUrl ,
165
+ resourceMetadataUrl : mockWWWAuthenticateParams . resourceMetadataUrl ,
166
+ scope : mockWWWAuthenticateParams . scope ,
163
167
fetchFn : mockFetch ,
164
168
} ) ;
165
169
@@ -192,10 +196,13 @@ describe("withOAuth", () => {
192
196
. mockResolvedValueOnce ( unauthorizedResponse )
193
197
. mockResolvedValueOnce ( successResponse ) ;
194
198
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 ) ;
199
206
mockAuth . mockResolvedValue ( "AUTHORIZED" ) ;
200
207
201
208
// Test without baseUrl - should extract from request URL
@@ -207,7 +214,8 @@ describe("withOAuth", () => {
207
214
expect ( mockFetch ) . toHaveBeenCalledTimes ( 2 ) ;
208
215
expect ( mockAuth ) . toHaveBeenCalledWith ( mockProvider , {
209
216
serverUrl : "https://api.example.com" , // Should be extracted from request URL
210
- resourceMetadataUrl : mockResourceUrl ,
217
+ resourceMetadataUrl : mockWWWAuthenticateParams . resourceMetadataUrl ,
218
+ scope : mockWWWAuthenticateParams . scope ,
211
219
fetchFn : mockFetch ,
212
220
} ) ;
213
221
@@ -225,7 +233,7 @@ describe("withOAuth", () => {
225
233
} ) ;
226
234
227
235
mockFetch . mockResolvedValue ( new Response ( "Unauthorized" , { status : 401 } ) ) ;
228
- mockExtractResourceMetadataUrl . mockReturnValue ( undefined ) ;
236
+ mockExtractWWWAuthenticateParams . mockReturnValue ( { } ) ;
229
237
mockAuth . mockResolvedValue ( "REDIRECT" ) ;
230
238
231
239
// Test without baseUrl
@@ -244,7 +252,7 @@ describe("withOAuth", () => {
244
252
} ) ;
245
253
246
254
mockFetch . mockResolvedValue ( new Response ( "Unauthorized" , { status : 401 } ) ) ;
247
- mockExtractResourceMetadataUrl . mockReturnValue ( undefined ) ;
255
+ mockExtractWWWAuthenticateParams . mockReturnValue ( { } ) ;
248
256
mockAuth . mockRejectedValue ( new Error ( "Network error" ) ) ;
249
257
250
258
const enhancedFetch = withOAuth (
@@ -266,7 +274,7 @@ describe("withOAuth", () => {
266
274
267
275
// Always return 401
268
276
mockFetch . mockResolvedValue ( new Response ( "Unauthorized" , { status : 401 } ) ) ;
269
- mockExtractResourceMetadataUrl . mockReturnValue ( undefined ) ;
277
+ mockExtractWWWAuthenticateParams . mockReturnValue ( { } ) ;
270
278
mockAuth . mockResolvedValue ( "AUTHORIZED" ) ;
271
279
272
280
const enhancedFetch = withOAuth (
@@ -383,7 +391,7 @@ describe("withOAuth", () => {
383
391
. mockResolvedValueOnce ( unauthorizedResponse )
384
392
. mockResolvedValueOnce ( successResponse ) ;
385
393
386
- mockExtractResourceMetadataUrl . mockReturnValue ( undefined ) ;
394
+ mockExtractWWWAuthenticateParams . mockReturnValue ( { } ) ;
387
395
mockAuth . mockResolvedValue ( "AUTHORIZED" ) ;
388
396
389
397
const enhancedFetch = withOAuth ( mockProvider ) ( mockFetch ) ;
@@ -964,8 +972,11 @@ describe("Integration Tests", () => {
964
972
. mockResolvedValueOnce ( unauthorizedResponse )
965
973
. mockResolvedValueOnce ( successResponse ) ;
966
974
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
+ }
969
980
) ;
970
981
mockAuth . mockResolvedValue ( "AUTHORIZED" ) ;
971
982
@@ -994,6 +1005,7 @@ describe("Integration Tests", () => {
994
1005
resourceMetadataUrl : new URL (
995
1006
"https://auth.example.com/.well-known/oauth-protected-resource" ,
996
1007
) ,
1008
+ scope : "read" ,
997
1009
fetchFn : mockFetch ,
998
1010
} ) ;
999
1011
} ) ;
0 commit comments