Fixing up tests

This commit is contained in:
Jerome 2025-02-24 13:48:24 +00:00
commit 8dfa64338e
No known key found for this signature in database

View file

@ -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);
});