Skip to content

Commit 89f7fb7

Browse files
authored
Merge pull request #411 from fredericbarthelet/add-unauthorized-error-test
Add 401 during POST request test case on StreamableHTTP transport to check auth flow
2 parents d13a69b + eb486d5 commit 89f7fb7

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

src/client/streamableHttp.test.ts

+36-1
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
11
import { StreamableHTTPClientTransport, StreamableHTTPReconnectionOptions } from "./streamableHttp.js";
2+
import { OAuthClientProvider, UnauthorizedError } from "./auth.js";
23
import { JSONRPCMessage } from "../types.js";
34

45

56
describe("StreamableHTTPClientTransport", () => {
67
let transport: StreamableHTTPClientTransport;
8+
let mockAuthProvider: jest.Mocked<OAuthClientProvider>;
79

810
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 });
1022
jest.spyOn(global, "fetch");
1123
});
1224

@@ -497,4 +509,27 @@ describe("StreamableHTTPClientTransport", () => {
497509
expect(getDelay(10)).toBe(5000);
498510
});
499511

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+
});
500535
});

0 commit comments

Comments
 (0)