@@ -44,34 +44,60 @@ describe("OAuth Authorization", () => {
4444 } ) ; 
4545
4646 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 ; 
4949
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+  } 
5566 } ) ; 
5667
68+  // Should succeed with the second call 
5769 const  metadata  =  await  discoverOAuthMetadata ( "https://auth.example.com" ) ; 
5870 expect ( metadata ) . toEqual ( validMetadata ) ; 
5971
60-  // Verify second call was  made without header  
72+  // Verify both calls were  made 
6173 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" ) ; 
6677 } ) ; 
6778
6879 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+  } ) ; 
7295
96+  // Should fail with the second error 
7397 await  expect ( discoverOAuthMetadata ( "https://auth.example.com" ) ) 
74-  . rejects . toThrow ( "Network error" ) ; 
98+  . rejects . toThrow ( "Second failure" ) ; 
99+  
100+  // Verify both calls were made 
75101 expect ( mockFetch ) . toHaveBeenCalledTimes ( 2 ) ; 
76102 } ) ; 
77103
0 commit comments