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