@@ -2932,6 +2932,89 @@ describe.each(zodTestMatrix)('$zodVersionLabel', (entry: ZodMatrixEntry) => {
29322932 } ) ;
29332933} ) ;
29342934
2935+ describe ( 'NodeStreamableHTTPServerTransport global Response preservation' , ( ) => {
2936+ it ( 'should not override the global Response object' , ( ) => {
2937+ // Store reference to the original global Response constructor
2938+ const OriginalResponse = globalThis . Response ;
2939+
2940+ // Create a custom class that extends Response (similar to Next.js's NextResponse)
2941+ class CustomResponse extends Response {
2942+ customProperty = 'test' ;
2943+ }
2944+
2945+ // Verify instanceof works before creating transport
2946+ const customResponseBefore = new CustomResponse ( 'test body' ) ;
2947+ expect ( customResponseBefore instanceof Response ) . toBe ( true ) ;
2948+ expect ( customResponseBefore instanceof OriginalResponse ) . toBe ( true ) ;
2949+
2950+ // Create the transport - this should NOT override globalThis.Response
2951+ const transport = new NodeStreamableHTTPServerTransport ( {
2952+ sessionIdGenerator : ( ) => randomUUID ( )
2953+ } ) ;
2954+
2955+ // Verify the global Response is still the original
2956+ expect ( globalThis . Response ) . toBe ( OriginalResponse ) ;
2957+
2958+ // Verify instanceof still works after creating transport
2959+ const customResponseAfter = new CustomResponse ( 'test body' ) ;
2960+ expect ( customResponseAfter instanceof Response ) . toBe ( true ) ;
2961+ expect ( customResponseAfter instanceof OriginalResponse ) . toBe ( true ) ;
2962+
2963+ // Verify that instances created before transport initialization still work
2964+ expect ( customResponseBefore instanceof Response ) . toBe ( true ) ;
2965+
2966+ // Clean up
2967+ transport . close ( ) ;
2968+ } ) ;
2969+
2970+ it ( 'should not override the global Response object when calling handleRequest' , async ( ) => {
2971+ // Store reference to the original global Response constructor
2972+ const OriginalResponse = globalThis . Response ;
2973+
2974+ // Create a custom class that extends Response
2975+ class CustomResponse extends Response {
2976+ customProperty = 'test' ;
2977+ }
2978+
2979+ const transport = new NodeStreamableHTTPServerTransport ( {
2980+ sessionIdGenerator : ( ) => randomUUID ( )
2981+ } ) ;
2982+
2983+ // Create a mock server to test handleRequest
2984+ const port = await getFreePort ( ) ;
2985+ const httpServer = createServer ( async ( req , res ) => {
2986+ await transport . handleRequest ( req as IncomingMessage & { auth ?: AuthInfo } , res ) ;
2987+ } ) ;
2988+
2989+ await new Promise < void > ( resolve => {
2990+ httpServer . listen ( port , ( ) => resolve ( ) ) ;
2991+ } ) ;
2992+
2993+ try {
2994+ // Make a request to trigger handleRequest
2995+ await fetch ( `http://localhost:${ port } ` , {
2996+ method : 'POST' ,
2997+ headers : {
2998+ 'Content-Type' : 'application/json' ,
2999+ Accept : 'application/json, text/event-stream'
3000+ } ,
3001+ body : JSON . stringify ( TEST_MESSAGES . initialize )
3002+ } ) ;
3003+
3004+ // Verify the global Response is still the original after handleRequest
3005+ expect ( globalThis . Response ) . toBe ( OriginalResponse ) ;
3006+
3007+ // Verify instanceof still works
3008+ const customResponse = new CustomResponse ( 'test body' ) ;
3009+ expect ( customResponse instanceof Response ) . toBe ( true ) ;
3010+ expect ( customResponse instanceof OriginalResponse ) . toBe ( true ) ;
3011+ } finally {
3012+ await transport . close ( ) ;
3013+ httpServer . close ( ) ;
3014+ }
3015+ } ) ;
3016+ } ) ;
3017+
29353018/**
29363019 * Helper to create test server with DNS rebinding protection options
29373020 */
0 commit comments