@@ -44,34 +44,60 @@ describe("OAuth Authorization", () => {
44
44
} ) ;
45
45
46
46
it ( "returns metadata when first fetch fails but second without MCP header succeeds" , async ( ) => {
47
- // First request with MCP header fails
48
- mockFetch . mockRejectedValueOnce ( new Error ( "Network error" ) ) ;
47
+ // Set up a counter to control behavior
48
+ let callCount = 0 ;
49
49
50
- // Second request without header succeeds
51
- mockFetch . mockResolvedValueOnce ( {
52
- ok : true ,
53
- status : 200 ,
54
- json : async ( ) => validMetadata ,
50
+ // Mock implementation that changes behavior based on call count
51
+ mockFetch . mockImplementation ( ( url , options ) => {
52
+ callCount ++ ;
53
+
54
+ if ( callCount === 1 ) {
55
+ // First call with MCP header - fail with TypeError (simulating CORS error)
56
+ // We need to use TypeError specifically because that's what the implementation checks for
57
+ return Promise . reject ( new TypeError ( "Network error" ) ) ;
58
+ } else {
59
+ // Second call without header - succeed
60
+ return Promise . resolve ( {
61
+ ok : true ,
62
+ status : 200 ,
63
+ json : async ( ) => validMetadata
64
+ } ) ;
65
+ }
55
66
} ) ;
56
67
68
+ // Should succeed with the second call
57
69
const metadata = await discoverOAuthMetadata ( "https://auth.example.com" ) ;
58
70
expect ( metadata ) . toEqual ( validMetadata ) ;
59
71
60
- // Verify second call was made without header
72
+ // Verify both calls were made
61
73
expect ( mockFetch ) . toHaveBeenCalledTimes ( 2 ) ;
62
- const secondCallOptions = mockFetch . mock . calls [ 1 ] [ 1 ] ;
63
- // The second call still has options but doesn't include MCP-Protocol-Version header
64
- expect ( secondCallOptions ) . toBeDefined ( ) ;
65
- expect ( secondCallOptions ?. headers ) . toBeUndefined ( ) ;
74
+
75
+ // Verify first call had MCP header
76
+ expect ( mockFetch . mock . calls [ 0 ] [ 1 ] ?. headers ) . toHaveProperty ( "MCP-Protocol-Version" ) ;
66
77
} ) ;
67
78
68
79
it ( "throws an error when all fetch attempts fail" , async ( ) => {
69
- // Both requests fail
70
- mockFetch . mockRejectedValueOnce ( new Error ( "Network error" ) ) ;
71
- mockFetch . mockRejectedValueOnce ( new Error ( "Network error" ) ) ;
80
+ // Set up a counter to control behavior
81
+ let callCount = 0 ;
82
+
83
+ // Mock implementation that changes behavior based on call count
84
+ mockFetch . mockImplementation ( ( url , options ) => {
85
+ callCount ++ ;
86
+
87
+ if ( callCount === 1 ) {
88
+ // First call - fail with TypeError
89
+ return Promise . reject ( new TypeError ( "First failure" ) ) ;
90
+ } else {
91
+ // Second call - fail with different error
92
+ return Promise . reject ( new Error ( "Second failure" ) ) ;
93
+ }
94
+ } ) ;
72
95
96
+ // Should fail with the second error
73
97
await expect ( discoverOAuthMetadata ( "https://auth.example.com" ) )
74
- . rejects . toThrow ( "Network error" ) ;
98
+ . rejects . toThrow ( "Second failure" ) ;
99
+
100
+ // Verify both calls were made
75
101
expect ( mockFetch ) . toHaveBeenCalledTimes ( 2 ) ;
76
102
} ) ;
77
103
0 commit comments