@@ -6,6 +6,7 @@ import type { Server } from 'node:http';
66jest . 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+
1723jest . 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