Skip to content

Commit a635205

Browse files
committed
Fix tests
1 parent b065dc9 commit a635205

3 files changed

Lines changed: 39 additions & 57 deletions

File tree

packages/mcp-server/src/tests/server/transport/factory.test.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ describe('Transport Factory', () => {
7070

7171
const transport = createTransport(config);
7272

73-
expect(StreamableHttpTransport).toHaveBeenCalledWith(8080, '0.0.0.0', '/api/mcp');
73+
expect(StreamableHttpTransport).toHaveBeenCalledWith(8080, '0.0.0.0', '/api/mcp', 'private-key');
7474
expect(transport).toBe(mockStreamableInstance);
7575
});
7676

@@ -88,7 +88,7 @@ describe('Transport Factory', () => {
8888

8989
const transport = createTransport(config);
9090

91-
expect(HttpSseTransport).toHaveBeenCalledWith(9000, '127.0.0.1', '/sse');
91+
expect(HttpSseTransport).toHaveBeenCalledWith(9000, '127.0.0.1', '/sse', 'disabled');
9292
expect(transport).toBe(mockSseInstance);
9393
});
9494

@@ -123,7 +123,7 @@ describe('Transport Factory', () => {
123123

124124
const transport = createTransport(config);
125125

126-
expect(StreamableHttpTransport).toHaveBeenCalledWith(params.port, params.host, params.path);
126+
expect(StreamableHttpTransport).toHaveBeenCalledWith(params.port, params.host, params.path, 'disabled');
127127
expect(transport).toBe(mockInstance);
128128

129129
jest.clearAllMocks();
@@ -145,7 +145,7 @@ describe('Transport Factory', () => {
145145

146146
const transport1 = createTransport(config1);
147147

148-
expect(HttpSseTransport).toHaveBeenCalledWith(1, '::1', '/');
148+
expect(HttpSseTransport).toHaveBeenCalledWith(1, '::1', '/', 'private-key');
149149
expect(transport1).toBe(mockInstance1);
150150

151151
jest.clearAllMocks();
@@ -164,7 +164,7 @@ describe('Transport Factory', () => {
164164

165165
const transport2 = createTransport(config2);
166166

167-
expect(StreamableHttpTransport).toHaveBeenCalledWith(65535, '0.0.0.0', '/very/long/path/to/test/edge/cases');
167+
expect(StreamableHttpTransport).toHaveBeenCalledWith(65535, '0.0.0.0', '/very/long/path/to/test/edge/cases', 'disabled');
168168
expect(transport2).toBe(mockInstance2);
169169
});
170170
});

packages/mcp-server/src/tests/server/transport/http-sse.test.ts

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,10 @@ jest.mock('express', () => {
1616
return express;
1717
});
1818

19-
jest.mock('cors', () => jest.fn(() => 'cors-middleware'));
19+
jest.mock('../../../server/transport/security.js', () => ({
20+
createCorsMiddleware: jest.fn(() => 'cors-middleware'),
21+
validateSecurityConfig: jest.fn()
22+
}));
2023

2124
jest.mock('@modelcontextprotocol/sdk/server/sse.js', () => ({
2225
SSEServerTransport: jest.fn()
@@ -27,7 +30,8 @@ describe('HttpSseTransport', () => {
2730
let mockExpress: jest.MockedFunction<any>;
2831
let mockApp: any;
2932
let mockServer: any;
30-
let mockCors: jest.MockedFunction<any>;
33+
let mockCreateCorsMiddleware: jest.MockedFunction<any>;
34+
let mockValidateSecurityConfig: jest.MockedFunction<any>;
3135
let mockSSEServerTransport: jest.MockedFunction<any>;
3236
let mockTransport: any;
3337
let mockMcpServer: any;
@@ -38,11 +42,12 @@ describe('HttpSseTransport', () => {
3842

3943
// Import mocked modules
4044
const expressModule = await import('express');
41-
const corsModule = await import('cors');
45+
const securityModule = await import('../../../server/transport/security.js');
4246
const { SSEServerTransport } = await import('@modelcontextprotocol/sdk/server/sse.js');
4347

4448
mockExpress = expressModule.default as jest.MockedFunction<any>;
45-
mockCors = corsModule.default as jest.MockedFunction<any>;
49+
mockCreateCorsMiddleware = securityModule.createCorsMiddleware as jest.MockedFunction<any>;
50+
mockValidateSecurityConfig = securityModule.validateSecurityConfig as jest.MockedFunction<any>;
4651
mockSSEServerTransport = SSEServerTransport as jest.MockedFunction<any>;
4752

4853
// Setup mock objects
@@ -70,7 +75,7 @@ describe('HttpSseTransport', () => {
7075
// Configure mocks
7176
mockExpress.mockReturnValue(mockApp);
7277
mockExpress.json = jest.fn().mockReturnValue('json-middleware');
73-
mockCors.mockReturnValue('cors-middleware');
78+
mockCreateCorsMiddleware.mockReturnValue('cors-middleware');
7479
mockSSEServerTransport.mockImplementation(() => mockTransport);
7580

7681
// Import the class after mocks are set up
@@ -96,14 +101,8 @@ describe('HttpSseTransport', () => {
96101

97102
expect(mockExpress).toHaveBeenCalled();
98103
expect(mockApp.use).toHaveBeenCalledWith('json-middleware');
99-
expect(mockCors).toHaveBeenCalledWith({
100-
origin: '*',
101-
methods: ['GET', 'POST', 'OPTIONS'],
102-
allowedHeaders: ['Content-Type', 'Authorization'],
103-
credentials: true,
104-
exposedHeaders: ['Content-Type', 'Access-Control-Allow-Origin']
105-
});
106-
expect(mockApp.options).toHaveBeenCalledWith('*', 'cors-middleware');
104+
expect(mockCreateCorsMiddleware).toHaveBeenCalled();
105+
expect(mockApp.use).toHaveBeenCalledWith('cors-middleware');
107106
expect(mockApp.get).toHaveBeenCalledWith('/health', expect.any(Function));
108107
expect(mockApp.get).toHaveBeenCalledWith('/sse', expect.any(Function));
109108
expect(mockApp.post).toHaveBeenCalledWith('/sse/message', expect.any(Function));

packages/mcp-server/src/tests/server/transport/streamable-http.test.ts

Lines changed: 22 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import type { Server } from 'node:http';
66
jest.mock('express', () => {
77
const mockApp = {
88
use: jest.fn(),
9+
get: jest.fn(),
910
post: jest.fn(),
1011
listen: jest.fn()
1112
};
@@ -14,6 +15,11 @@ jest.mock('express', () => {
1415
return express;
1516
});
1617

18+
jest.mock('../../../server/transport/security.js', () => ({
19+
createCorsMiddleware: jest.fn(() => 'cors-middleware'),
20+
validateSecurityConfig: jest.fn()
21+
}));
22+
1723
jest.mock('@modelcontextprotocol/sdk/server/streamableHttp.js', () => ({
1824
StreamableHTTPServerTransport: jest.fn()
1925
}));
@@ -47,6 +53,7 @@ describe('StreamableHttpTransport', () => {
4753
// Setup mock objects
4854
mockApp = {
4955
use: jest.fn(),
56+
get: jest.fn(),
5057
post: jest.fn(),
5158
listen: jest.fn()
5259
};
@@ -63,9 +70,14 @@ describe('StreamableHttpTransport', () => {
6370
close: jest.fn()
6471
};
6572

73+
// Import security mock
74+
const securityModule = await import('../../../server/transport/security.js');
75+
const mockCreateCorsMiddleware = securityModule.createCorsMiddleware as jest.MockedFunction<any>;
76+
mockCreateCorsMiddleware.mockReturnValue('cors-middleware');
77+
6678
// Setup default mocks
6779
mockExpress.mockReturnValue(mockApp);
68-
mockExpress.json = jest.fn();
80+
mockExpress.json = jest.fn().mockReturnValue('json-middleware');
6981
mockApp.listen.mockReturnValue(mockServer);
7082
mockGetServer.mockResolvedValue(mockMcpServer);
7183
(StreamableHTTPServerTransport as jest.Mock).mockImplementation(() => mockStreamableTransport);
@@ -106,10 +118,10 @@ describe('StreamableHttpTransport', () => {
106118
// Verify express setup
107119
expect(mockExpress).toHaveBeenCalled();
108120
expect(mockExpress.json).toHaveBeenCalled();
109-
expect(mockApp.use).toHaveBeenCalledWith(mockExpress.json());
121+
expect(mockApp.use).toHaveBeenCalledWith('json-middleware');
110122

111123
// Verify CORS middleware setup
112-
expect(mockApp.use).toHaveBeenCalledWith(expect.any(Function));
124+
expect(mockApp.use).toHaveBeenCalledWith('cors-middleware');
113125

114126
// Verify POST route setup
115127
expect(mockApp.post).toHaveBeenCalledWith('/mcp', expect.any(Function));
@@ -121,49 +133,20 @@ describe('StreamableHttpTransport', () => {
121133
expect(mockServer.on).toHaveBeenCalledWith('error', expect.any(Function));
122134
});
123135

124-
it('should handle CORS preflight requests', () => {
136+
it('should setup CORS middleware', async () => {
125137
const transport = new StreamableHttpTransport();
126-
transport.start();
127-
128-
// Get the middleware function
129-
const middlewareFunction = mockApp.use.mock.calls[1][0];
138+
await transport.start({ mock: 'server' });
130139

131-
// Mock request and response for OPTIONS request
132-
const mockReq = { method: 'OPTIONS' } as any as Request;
133-
const mockRes = {
134-
header: jest.fn(),
135-
sendStatus: jest.fn()
136-
} as any as Response;
137-
mockRes.sendStatus.mockReturnValue(mockRes);
138-
const mockNext = jest.fn();
139-
140-
// Call the middleware
141-
middlewareFunction(mockReq, mockRes, mockNext);
142-
143-
// Verify CORS headers are set
144-
expect(mockRes.header).toHaveBeenCalledWith('Access-Control-Allow-Origin', '*');
145-
expect(mockRes.header).toHaveBeenCalledWith('Access-Control-Allow-Methods', 'POST, OPTIONS');
146-
expect(mockRes.header).toHaveBeenCalledWith('Access-Control-Allow-Headers', 'Content-Type');
147-
expect(mockRes.sendStatus).toHaveBeenCalledWith(200);
148-
expect(mockNext).not.toHaveBeenCalled();
140+
// Verify CORS middleware was set up
141+
expect(mockApp.use).toHaveBeenCalledWith('cors-middleware');
149142
});
150143

151-
it('should call next for non-OPTIONS requests', async () => {
144+
it('should setup health endpoint', async () => {
152145
const transport = new StreamableHttpTransport();
153146
await transport.start({ mock: 'server' });
154147

155-
// Get the CORS middleware function
156-
const corsMiddleware = mockApp.use.mock.calls.find(call =>
157-
typeof call[0] === 'function' && call[0].length === 3
158-
)?.[0];
159-
160-
const mockReq = { method: 'POST' } as Request;
161-
const mockRes = { header: jest.fn() } as any as Response;
162-
const mockNext = jest.fn();
163-
164-
corsMiddleware(mockReq, mockRes, mockNext);
165-
166-
expect(mockNext).toHaveBeenCalled();
148+
// Verify health endpoint was set up
149+
expect(mockApp.get).toHaveBeenCalledWith('/health', expect.any(Function));
167150
});
168151

169152
it('should handle successful MCP requests', async () => {

0 commit comments

Comments
 (0)