@@ -72,6 +72,7 @@ class MyReporter {
7272 this . _testEnv = getTestEnv ( ) ;
7373 this . _paths = new PathHelper ( { cwd : process . cwd ( ) } , this . _testEnv . location_prefix ) ;
7474 this . currentTestSteps = [ ] ;
75+ this . httpServer = null ;
7576 this . currentTestCucumberSteps = [ ] ;
7677 this . hooksStarted = { } ;
7778 this . beforeHooks = [ ] ;
@@ -219,48 +220,62 @@ class MyReporter {
219220 }
220221
221222 async startHttpServer ( ) {
222- this . httpServer = http . createServer ( async ( req , res ) => {
223- // Set CORS headers
224- res . setHeader ( 'Access-Control-Allow-Origin' , '*' ) ;
225- res . setHeader ( 'Access-Control-Allow-Methods' , 'GET, POST, OPTIONS' ) ;
226- res . setHeader ( 'Access-Control-Allow-Headers' , 'Content-Type' ) ;
227-
228- if ( req . method === 'OPTIONS' ) {
229- res . writeHead ( 200 ) ;
230- res . end ( ) ;
231- return ;
232- }
233- const parsedUrl = new URL ( req . url , `http://${ req . headers . host } ` ) ;
234- const pathname = parsedUrl . pathname ;
235- const query = parsedUrl . searchParams ;
236-
237- if ( pathname === '/test-uuid' && req . method === 'GET' ) {
238- const testIdentifier = query . get ( 'testIdentifier' ) ;
239-
240- if ( ! testIdentifier ) {
241- res . writeHead ( 400 , { 'Content-Type' : 'application/json' } ) ;
242- res . end ( JSON . stringify ( {
243- error : 'testIdentifier parameter is required' ,
244- testRunUuid : null
245- } ) ) ;
246- return ;
247- }
248- const testRunUuid = this . getTestId ( testIdentifier ) ;
223+ if ( this . httpServer !== null ) return ;
224+
225+ try {
226+ this . httpServer = http . createServer ( async ( req , res ) => {
227+ try {
228+ // Set CORS headers
229+ res . setHeader ( 'Access-Control-Allow-Origin' , '*' ) ;
230+ res . setHeader ( 'Access-Control-Allow-Methods' , 'GET, POST, OPTIONS' ) ;
231+ res . setHeader ( 'Access-Control-Allow-Headers' , 'Content-Type' ) ;
232+
233+ if ( req . method === 'OPTIONS' ) {
234+ res . writeHead ( 200 ) ;
235+ res . end ( ) ;
236+ return ;
237+ }
238+ const parsedUrl = new URL ( req . url , `http://${ req . headers . host } ` ) ;
239+ const pathname = parsedUrl . pathname ;
240+ const query = parsedUrl . searchParams ;
241+
242+ if ( pathname === '/test-uuid' && req . method === 'GET' ) {
243+ const testIdentifier = query . get ( 'testIdentifier' ) ;
244+
245+ if ( ! testIdentifier ) {
246+ res . writeHead ( 400 , { 'Content-Type' : 'application/json' } ) ;
247+ res . end ( JSON . stringify ( {
248+ error : 'testIdentifier parameter is required' ,
249+ testRunUuid : null
250+ } ) ) ;
251+ return ;
252+ }
253+ const testRunUuid = this . getTestId ( testIdentifier ) ;
249254
250255
251- res . writeHead ( 200 , { 'Content-Type' : 'application/json' } ) ;
252- res . end ( JSON . stringify ( { testRunUuid : testRunUuid } ) ) ;
253- } else {
254- res . writeHead ( 404 , { 'Content-Type' : 'text/plain' } ) ;
255- res . end ( 'Not Found' ) ;
256- }
257- } ) ;
256+ res . writeHead ( 200 , { 'Content-Type' : 'application/json' } ) ;
257+ res . end ( JSON . stringify ( { testRunUuid : testRunUuid } ) ) ;
258+ } else {
259+ res . writeHead ( 404 , { 'Content-Type' : 'text/plain' } ) ;
260+ res . end ( 'Not Found' ) ;
261+ }
262+ } catch ( error ) {
263+ debugOnConsole ( `Exception in handling HTTP request : ${ error } ` ) ;
264+ debug ( `Exception in handling HTTP request : ${ error } ` , true , error ) ;
265+ res . writeHead ( 500 , { 'Content-Type' : 'application/json' } ) ;
266+ res . end ( JSON . stringify ( { testRunUuid : null } ) ) ;
267+ }
268+ } ) ;
258269
259- const port = process . env . REPORTER_API_PORT_NO ;
270+ const port = process . env . REPORTER_API_PORT_NO ;
260271
261- this . httpServer . listen ( port , '127.0.0.1' , async ( ) => {
262- console . log ( `Reporter HTTP server listening on port ${ port } ` ) ;
263- } ) ;
272+ this . httpServer . listen ( port , '127.0.0.1' , async ( ) => {
273+ console . log ( `Reporter HTTP server listening on port ${ port } ` ) ;
274+ } ) ;
275+ } catch ( error ) {
276+ debugOnConsole ( `Exception in starting reporter server : ${ error } ` ) ;
277+ debug ( `Exception in starting reporter server : ${ error } ` , true , error ) ;
278+ }
264279 }
265280
266281 registerListeners ( ) {
0 commit comments