@@ -469,4 +469,123 @@ describe('push-notifications-fcm', () => {
469469 } ) ;
470470 } ) ;
471471 } ) ;
472+
473+ describe ( 'Legacy HTTP transport support' , ( ) => {
474+ it ( 'should enable legacyHttpTransport when configured' , ( done ) => {
475+ const mockEnableLegacyHttpTransport = sinon . stub ( ) ;
476+
477+ // Stub messaging to track calls and return mock instance
478+ const mockMessagingStub = sinon . stub ( ) . returns ( {
479+ enableLegacyHttpTransport : mockEnableLegacyHttpTransport ,
480+ sendEachForMulticast : ( ) =>
481+ Promise . resolve ( {
482+ successCount : 1 ,
483+ failureCount : 0 ,
484+ responses : [ { error : null } ] ,
485+ } ) ,
486+ } ) ;
487+
488+ // Use Object.defineProperty to override the messaging getter
489+ const proto = Object . getPrototypeOf ( firebaseAdmin ) ;
490+ const propertyDescriptor = Object . getOwnPropertyDescriptor ( proto , 'messaging' ) ;
491+
492+ // eslint-disable-next-line no-import-assign
493+ Object . defineProperty ( firebaseAdmin , 'messaging' , {
494+ value : mockMessagingStub ,
495+ configurable : true ,
496+ writable : true ,
497+ } ) ;
498+
499+ sinon . stub ( firebaseAdmin , 'initializeApp' ) . returns ( { } ) ;
500+ sinon . stub ( firebaseAdmin . INTERNAL . appStore , 'removeApp' ) ;
501+
502+ const fcmOptsWithLegacy = {
503+ fcm : {
504+ name : 'testAppNameLegacy' ,
505+ credential : { getAccessToken : ( ) => Promise . resolve ( { } ) } ,
506+ legacyHttpTransport : true ,
507+ } ,
508+ } ;
509+
510+ const pnWithLegacy = new PN ( fcmOptsWithLegacy ) ;
511+
512+ pnWithLegacy
513+ . send ( regIds , message )
514+ . then ( ( ) => {
515+ expect ( mockEnableLegacyHttpTransport . called ) . to . be . true ;
516+ // Restore
517+ firebaseAdmin . initializeApp . restore ( ) ;
518+ firebaseAdmin . INTERNAL . appStore . removeApp . restore ( ) ;
519+ // eslint-disable-next-line no-import-assign
520+ Object . defineProperty ( firebaseAdmin , 'messaging' , propertyDescriptor ) ;
521+ done ( ) ;
522+ } )
523+ . catch ( ( err ) => {
524+ // Restore
525+ firebaseAdmin . initializeApp . restore ( ) ;
526+ firebaseAdmin . INTERNAL . appStore . removeApp . restore ( ) ;
527+ // eslint-disable-next-line no-import-assign
528+ Object . defineProperty ( firebaseAdmin , 'messaging' , propertyDescriptor ) ;
529+ done ( err ) ;
530+ } ) ;
531+ } ) ;
532+
533+ it ( 'should not enable legacyHttpTransport when not configured' , ( done ) => {
534+ const mockEnableLegacyHttpTransport = sinon . stub ( ) ;
535+
536+ // Stub messaging to track calls and return mock instance
537+ const mockMessagingStub = sinon . stub ( ) . returns ( {
538+ enableLegacyHttpTransport : mockEnableLegacyHttpTransport ,
539+ sendEachForMulticast : ( ) =>
540+ Promise . resolve ( {
541+ successCount : 1 ,
542+ failureCount : 0 ,
543+ responses : [ { error : null } ] ,
544+ } ) ,
545+ } ) ;
546+
547+ // Use Object.defineProperty to override the messaging getter
548+ const proto = Object . getPrototypeOf ( firebaseAdmin ) ;
549+ const propertyDescriptor = Object . getOwnPropertyDescriptor ( proto , 'messaging' ) ;
550+
551+ // eslint-disable-next-line no-import-assign
552+ Object . defineProperty ( firebaseAdmin , 'messaging' , {
553+ value : mockMessagingStub ,
554+ configurable : true ,
555+ writable : true ,
556+ } ) ;
557+
558+ sinon . stub ( firebaseAdmin , 'initializeApp' ) . returns ( { } ) ;
559+ sinon . stub ( firebaseAdmin . INTERNAL . appStore , 'removeApp' ) ;
560+
561+ const fcmOptsWithoutLegacy = {
562+ fcm : {
563+ name : 'testAppNameNoLegacy' ,
564+ credential : { getAccessToken : ( ) => Promise . resolve ( { } ) } ,
565+ } ,
566+ } ;
567+
568+ const pnWithoutLegacy = new PN ( fcmOptsWithoutLegacy ) ;
569+
570+ pnWithoutLegacy
571+ . send ( regIds , message )
572+ . then ( ( ) => {
573+ expect ( mockEnableLegacyHttpTransport . called ) . to . be . false ;
574+ // Restore
575+ firebaseAdmin . initializeApp . restore ( ) ;
576+ firebaseAdmin . INTERNAL . appStore . removeApp . restore ( ) ;
577+ // eslint-disable-next-line no-import-assign
578+ Object . defineProperty ( firebaseAdmin , 'messaging' , propertyDescriptor ) ;
579+ done ( ) ;
580+ } )
581+ . catch ( ( err ) => {
582+ // Restore
583+ firebaseAdmin . initializeApp . restore ( ) ;
584+ firebaseAdmin . INTERNAL . appStore . removeApp . restore ( ) ;
585+ // eslint-disable-next-line no-import-assign
586+ Object . defineProperty ( firebaseAdmin , 'messaging' , propertyDescriptor ) ;
587+ done ( err ) ;
588+ } ) ;
589+ } ) ;
590+ } ) ;
472591} ) ;
0 commit comments