|
1 | 1 | import { StreamableHTTPClientTransport, StreamableHTTPReconnectionOptions } from "./streamableHttp.js";
|
| 2 | +import { OAuthClientProvider, UnauthorizedError } from "./auth.js"; |
2 | 3 | import { JSONRPCMessage } from "../types.js";
|
3 | 4 |
|
4 | 5 |
|
5 | 6 | describe("StreamableHTTPClientTransport", () => {
|
6 | 7 | let transport: StreamableHTTPClientTransport;
|
| 8 | + let mockAuthProvider: jest.Mocked<OAuthClientProvider>; |
7 | 9 |
|
8 | 10 | beforeEach(() => {
|
9 |
| - transport = new StreamableHTTPClientTransport(new URL("http://localhost:1234/mcp")); |
| 11 | + mockAuthProvider = { |
| 12 | + get redirectUrl() { return "http://localhost/callback"; }, |
| 13 | + get clientMetadata() { return { redirect_uris: ["http://localhost/callback"] }; }, |
| 14 | + clientInformation: jest.fn(() => ({ client_id: "test-client-id", client_secret: "test-client-secret" })), |
| 15 | + tokens: jest.fn(), |
| 16 | + saveTokens: jest.fn(), |
| 17 | + redirectToAuthorization: jest.fn(), |
| 18 | + saveCodeVerifier: jest.fn(), |
| 19 | + codeVerifier: jest.fn(), |
| 20 | + }; |
| 21 | + transport = new StreamableHTTPClientTransport(new URL("http://localhost:1234/mcp"), { authProvider: mockAuthProvider }); |
10 | 22 | jest.spyOn(global, "fetch");
|
11 | 23 | });
|
12 | 24 |
|
@@ -497,4 +509,27 @@ describe("StreamableHTTPClientTransport", () => {
|
497 | 509 | expect(getDelay(10)).toBe(5000);
|
498 | 510 | });
|
499 | 511 |
|
| 512 | + it("attempts auth flow on 401 during POST request", async () => { |
| 513 | + const message: JSONRPCMessage = { |
| 514 | + jsonrpc: "2.0", |
| 515 | + method: "test", |
| 516 | + params: {}, |
| 517 | + id: "test-id" |
| 518 | + }; |
| 519 | + |
| 520 | + (global.fetch as jest.Mock) |
| 521 | + .mockResolvedValueOnce({ |
| 522 | + ok: false, |
| 523 | + status: 401, |
| 524 | + statusText: "Unauthorized", |
| 525 | + headers: new Headers() |
| 526 | + }) |
| 527 | + .mockResolvedValue({ |
| 528 | + ok: false, |
| 529 | + status: 404 |
| 530 | + }); |
| 531 | + |
| 532 | + await expect(transport.send(message)).rejects.toThrow(UnauthorizedError); |
| 533 | + expect(mockAuthProvider.redirectToAuthorization.mock.calls).toHaveLength(1); |
| 534 | + }); |
500 | 535 | });
|
0 commit comments