Skip to content

Commit 617facc

Browse files
ochafikclaude
andcommitted
Add test for default protocol version negotiation in bearerAuth middleware
- Tests that when mcp-protocol-version header is missing, the middleware uses DEFAULT_NEGOTIATED_PROTOCOL_VERSION when calling verifyAccessToken - Ensures proper fallback behavior for protocol version negotiation 🤖 Generated with [Claude Code](https://claude.ai/code) Co-Authored-By: Claude <[email protected]>
1 parent 40f61d8 commit 617facc

File tree

1 file changed

+23
-1
lines changed

1 file changed

+23
-1
lines changed

src/server/auth/middleware/bearerAuth.test.ts

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { requireBearerAuth } from "./bearerAuth.js";
33
import { AuthInfo } from "../types.js";
44
import { InsufficientScopeError, InvalidTokenError, OAuthError, ServerError } from "../errors.js";
55
import { OAuthTokenVerifier } from "../provider.js";
6-
import { LATEST_PROTOCOL_VERSION } from '../../../types.js';
6+
import { LATEST_PROTOCOL_VERSION, DEFAULT_NEGOTIATED_PROTOCOL_VERSION } from '../../../types.js';
77

88
// Mock verifier
99
const mockVerifyAccessToken = jest.fn();
@@ -56,6 +56,28 @@ describe("requireBearerAuth middleware", () => {
5656
expect(mockResponse.json).not.toHaveBeenCalled();
5757
});
5858

59+
it("should use default negotiated protocol version when mcp-protocol-version header is missing", async () => {
60+
const validAuthInfo: AuthInfo = {
61+
token: "valid-token",
62+
clientId: "client-123",
63+
scopes: ["read", "write"],
64+
};
65+
mockVerifyAccessToken.mockResolvedValue(validAuthInfo);
66+
67+
mockRequest.headers = {
68+
authorization: "Bearer valid-token",
69+
};
70+
71+
const middleware = requireBearerAuth({ verifier: mockVerifier });
72+
await middleware(mockRequest as Request, mockResponse as Response, nextFunction);
73+
74+
expect(mockVerifyAccessToken).toHaveBeenCalledWith("valid-token", DEFAULT_NEGOTIATED_PROTOCOL_VERSION);
75+
expect(mockRequest.auth).toEqual(validAuthInfo);
76+
expect(nextFunction).toHaveBeenCalled();
77+
expect(mockResponse.status).not.toHaveBeenCalled();
78+
expect(mockResponse.json).not.toHaveBeenCalled();
79+
});
80+
5981
it("should reject expired tokens", async () => {
6082
const expiredAuthInfo: AuthInfo = {
6183
token: "expired-token",

0 commit comments

Comments
 (0)