@@ -356,6 +356,62 @@ describe('Client', () => {
356356 expect ( errorMessages ) . to . be . empty ;
357357 } ) ;
358358
359+ it ( 'Returns APNs notification ID in responses' , async ( ) => {
360+ let didRequest = false ;
361+ let establishedConnections = 0 ;
362+ let requestsServed = 0 ;
363+ const method = HTTP2_METHOD_POST ;
364+ const path = PATH_DEVICE ;
365+ const notificationId = '7dc35f9f-58d4-40dd-8c08-38ab811f57df' ;
366+ const additionalHeaderInfo = { 'apns-id' : notificationId } ;
367+ server = createAndStartMockServer ( TEST_PORT , ( req , res , requestBody ) => {
368+ expect ( req . headers ) . to . deep . equal ( {
369+ ':authority' : '127.0.0.1' ,
370+ ':method' : method ,
371+ ':path' : path ,
372+ ':scheme' : 'https' ,
373+ 'apns-someheader' : 'somevalue' ,
374+ } ) ;
375+ expect ( requestBody ) . to . equal ( MOCK_BODY ) ;
376+ // res.setHeader('X-Foo', 'bar');
377+ // res.writeHead(200, { 'Content-Type': 'text/plain; charset=utf-8' });
378+ res . writeHead ( 200 , additionalHeaderInfo ) ;
379+ res . end ( '' ) ;
380+ requestsServed += 1 ;
381+ didRequest = true ;
382+ } ) ;
383+ server . on ( 'connection' , ( ) => ( establishedConnections += 1 ) ) ;
384+ await new Promise ( resolve => server . on ( 'listening' , resolve ) ) ;
385+
386+ client = createClient ( CLIENT_TEST_PORT ) ;
387+
388+ const runSuccessfulRequest = async ( ) => {
389+ const mockHeaders = { 'apns-someheader' : 'somevalue' } ;
390+ const mockNotification = {
391+ headers : mockHeaders ,
392+ body : MOCK_BODY ,
393+ } ;
394+ const device = MOCK_DEVICE_TOKEN ;
395+ const result = await client . write ( mockNotification , device , 'device' , 'post' ) ;
396+ expect ( result ) . to . deep . equal ( { ...additionalHeaderInfo , device } ) ;
397+ expect ( didRequest ) . to . be . true ;
398+ } ;
399+ expect ( establishedConnections ) . to . equal ( 0 ) ; // should not establish a connection until it's needed
400+ // Validate that when multiple valid requests arrive concurrently,
401+ // only one HTTP/2 connection gets established
402+ await Promise . all ( [
403+ runSuccessfulRequest ( ) ,
404+ runSuccessfulRequest ( ) ,
405+ runSuccessfulRequest ( ) ,
406+ runSuccessfulRequest ( ) ,
407+ runSuccessfulRequest ( ) ,
408+ ] ) ;
409+ didRequest = false ;
410+ await runSuccessfulRequest ( ) ;
411+ expect ( establishedConnections ) . to . equal ( 1 ) ; // should establish a connection to the server and reuse it
412+ expect ( requestsServed ) . to . equal ( 6 ) ;
413+ } ) ;
414+
359415 // https://developer.apple.com/documentation/usernotifications/setting_up_a_remote_notification_server/handling_notification_responses_from_apns
360416 it ( 'JSON decodes HTTP 400 responses' , async ( ) => {
361417 let didRequest = false ;
0 commit comments