From 8dfa64338e072178b6cab2f56a9cb11bec4608c5 Mon Sep 17 00:00:00 2001 From: Jerome Date: Mon, 24 Feb 2025 13:48:24 +0000 Subject: [PATCH] Fixing up tests --- src/client/auth.test.ts | 58 +++++++++++++++++++++++++++++------------ 1 file changed, 42 insertions(+), 16 deletions(-) diff --git a/src/client/auth.test.ts b/src/client/auth.test.ts index daa98de5..1e892308 100644 --- a/src/client/auth.test.ts +++ b/src/client/auth.test.ts @@ -44,34 +44,60 @@ describe("OAuth Authorization", () => { }); it("returns metadata when first fetch fails but second without MCP header succeeds", async () => { - // First request with MCP header fails - mockFetch.mockRejectedValueOnce(new Error("Network error")); + // Set up a counter to control behavior + let callCount = 0; - // Second request without header succeeds - mockFetch.mockResolvedValueOnce({ - ok: true, - status: 200, - json: async () => validMetadata, + // Mock implementation that changes behavior based on call count + mockFetch.mockImplementation((url, options) => { + callCount++; + + if (callCount === 1) { + // First call with MCP header - fail with TypeError (simulating CORS error) + // We need to use TypeError specifically because that's what the implementation checks for + return Promise.reject(new TypeError("Network error")); + } else { + // Second call without header - succeed + return Promise.resolve({ + ok: true, + status: 200, + json: async () => validMetadata + }); + } }); + // Should succeed with the second call const metadata = await discoverOAuthMetadata("https://auth.example.com"); expect(metadata).toEqual(validMetadata); - // Verify second call was made without header + // Verify both calls were made expect(mockFetch).toHaveBeenCalledTimes(2); - const secondCallOptions = mockFetch.mock.calls[1][1]; - // The second call still has options but doesn't include MCP-Protocol-Version header - expect(secondCallOptions).toBeDefined(); - expect(secondCallOptions?.headers).toBeUndefined(); + + // Verify first call had MCP header + expect(mockFetch.mock.calls[0][1]?.headers).toHaveProperty("MCP-Protocol-Version"); }); it("throws an error when all fetch attempts fail", async () => { - // Both requests fail - mockFetch.mockRejectedValueOnce(new Error("Network error")); - mockFetch.mockRejectedValueOnce(new Error("Network error")); + // Set up a counter to control behavior + let callCount = 0; + + // Mock implementation that changes behavior based on call count + mockFetch.mockImplementation((url, options) => { + callCount++; + + if (callCount === 1) { + // First call - fail with TypeError + return Promise.reject(new TypeError("First failure")); + } else { + // Second call - fail with different error + return Promise.reject(new Error("Second failure")); + } + }); + // Should fail with the second error await expect(discoverOAuthMetadata("https://auth.example.com")) - .rejects.toThrow("Network error"); + .rejects.toThrow("Second failure"); + + // Verify both calls were made expect(mockFetch).toHaveBeenCalledTimes(2); });