@@ -3,6 +3,7 @@ import { Hub } from '@sentry/hub';
33import * as sentryHub from '@sentry/hub' ;
44import { Transaction } from '@sentry/tracing' ;
55import { Runtime } from '@sentry/types' ;
6+ import { SentryError } from '@sentry/utils' ;
67import * as http from 'http' ;
78import * as net from 'net' ;
89
@@ -16,6 +17,7 @@ import {
1617 requestHandler ,
1718 tracingHandler ,
1819} from '../src/handlers' ;
20+ import * as SDK from '../src/sdk' ;
1921
2022describe ( 'parseRequest' , ( ) => {
2123 let mockReq : { [ key : string ] : any } ;
@@ -281,6 +283,31 @@ describe('requestHandler', () => {
281283 done ( ) ;
282284 } ) ;
283285 } ) ;
286+
287+ it ( 'patches `res.end` when `flushTimeout` is specified' , ( ) => {
288+ const flush = jest . spyOn ( SDK , 'flush' ) . mockResolvedValue ( true ) ;
289+
290+ const sentryRequestMiddleware = requestHandler ( { flushTimeout : 1337 } ) ;
291+ sentryRequestMiddleware ( req , res , next ) ;
292+ res . end ( 'ok' ) ;
293+
294+ setImmediate ( ( ) => {
295+ expect ( flush ) . toHaveBeenCalledWith ( 1337 ) ;
296+ expect ( res . finished ) . toBe ( true ) ;
297+ } ) ;
298+ } ) ;
299+
300+ it ( 'prevents errors thrown during `flush` from breaking the response' , async ( ) => {
301+ jest . spyOn ( SDK , 'flush' ) . mockRejectedValue ( new SentryError ( 'HTTP Error (429)' ) ) ;
302+
303+ const sentryRequestMiddleware = requestHandler ( { flushTimeout : 1337 } ) ;
304+ sentryRequestMiddleware ( req , res , next ) ;
305+ res . end ( 'ok' ) ;
306+
307+ setImmediate ( ( ) => {
308+ expect ( res . finished ) . toBe ( true ) ;
309+ } ) ;
310+ } ) ;
284311} ) ;
285312
286313describe ( 'tracingHandler' , ( ) => {
0 commit comments