@@ -469,4 +469,141 @@ 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+ // Create mock messaging instance
478+ const mockMessagingInstance = {
479+ enableLegacyHttpTransport : mockEnableLegacyHttpTransport ,
480+ sendEachForMulticast : ( ) =>
481+ Promise . resolve ( {
482+ successCount : 1 ,
483+ failureCount : 0 ,
484+ responses : [ { error : null } ] ,
485+ } ) ,
486+ } ;
487+
488+ // Stub initializeApp and INTERNAL.appStore.removeApp
489+ const initStub = sinon . stub ( firebaseAdmin , 'initializeApp' ) . returns ( { } ) ;
490+ const removeStub = sinon . stub ( firebaseAdmin . INTERNAL . appStore , 'removeApp' ) ;
491+
492+ // Use sinon.stub with an object and property descriptor
493+ const getMessagingStub = sinon . stub ( ) ;
494+ getMessagingStub . returns ( mockMessagingInstance ) ;
495+
496+ // Replace the getter for messaging
497+ const descriptor = Object . getOwnPropertyDescriptor (
498+ Object . getPrototypeOf ( firebaseAdmin ) ,
499+ 'messaging'
500+ ) ;
501+ const originalMessaging = descriptor . get ;
502+ Object . defineProperty ( firebaseAdmin , 'messaging' , {
503+ value : getMessagingStub ,
504+ configurable : true ,
505+ } ) ;
506+
507+ const fcmOptsWithLegacy = {
508+ fcm : {
509+ name : 'testAppNameLegacy' ,
510+ credential : { getAccessToken : ( ) => Promise . resolve ( { } ) } ,
511+ legacyHttpTransport : true ,
512+ } ,
513+ } ;
514+
515+ const pnWithLegacy = new PN ( fcmOptsWithLegacy ) ;
516+
517+ pnWithLegacy
518+ . send ( regIds , message )
519+ . then ( ( ) => {
520+ expect ( mockEnableLegacyHttpTransport . called ) . to . be . true ;
521+ // Restore
522+ initStub . restore ( ) ;
523+ removeStub . restore ( ) ;
524+ Object . defineProperty ( firebaseAdmin , 'messaging' , {
525+ get : originalMessaging ,
526+ configurable : true ,
527+ } ) ;
528+ done ( ) ;
529+ } )
530+ . catch ( ( err ) => {
531+ // Restore
532+ initStub . restore ( ) ;
533+ removeStub . restore ( ) ;
534+ Object . defineProperty ( firebaseAdmin , 'messaging' , {
535+ get : originalMessaging ,
536+ configurable : true ,
537+ } ) ;
538+ done ( err ) ;
539+ } ) ;
540+ } ) ;
541+
542+ it ( 'should not enable legacyHttpTransport when not configured' , ( done ) => {
543+ const mockEnableLegacyHttpTransport = sinon . stub ( ) ;
544+
545+ // Create mock messaging instance
546+ const mockMessagingInstance = {
547+ enableLegacyHttpTransport : mockEnableLegacyHttpTransport ,
548+ sendEachForMulticast : ( ) =>
549+ Promise . resolve ( {
550+ successCount : 1 ,
551+ failureCount : 0 ,
552+ responses : [ { error : null } ] ,
553+ } ) ,
554+ } ;
555+
556+ // Stub initializeApp and INTERNAL.appStore.removeApp
557+ const initStub = sinon . stub ( firebaseAdmin , 'initializeApp' ) . returns ( { } ) ;
558+ const removeStub = sinon . stub ( firebaseAdmin . INTERNAL . appStore , 'removeApp' ) ;
559+
560+ // Use sinon.stub with an object and property descriptor
561+ const getMessagingStub = sinon . stub ( ) ;
562+ getMessagingStub . returns ( mockMessagingInstance ) ;
563+
564+ // Replace the getter for messaging
565+ const descriptor = Object . getOwnPropertyDescriptor (
566+ Object . getPrototypeOf ( firebaseAdmin ) ,
567+ 'messaging'
568+ ) ;
569+ const originalMessaging = descriptor . get ;
570+ Object . defineProperty ( firebaseAdmin , 'messaging' , {
571+ value : getMessagingStub ,
572+ configurable : true ,
573+ } ) ;
574+
575+ const fcmOptsWithoutLegacy = {
576+ fcm : {
577+ name : 'testAppNameNoLegacy' ,
578+ credential : { getAccessToken : ( ) => Promise . resolve ( { } ) } ,
579+ } ,
580+ } ;
581+
582+ const pnWithoutLegacy = new PN ( fcmOptsWithoutLegacy ) ;
583+
584+ pnWithoutLegacy
585+ . send ( regIds , message )
586+ . then ( ( ) => {
587+ expect ( mockEnableLegacyHttpTransport . called ) . to . be . false ;
588+ // Restore
589+ initStub . restore ( ) ;
590+ removeStub . restore ( ) ;
591+ Object . defineProperty ( firebaseAdmin , 'messaging' , {
592+ get : originalMessaging ,
593+ configurable : true ,
594+ } ) ;
595+ done ( ) ;
596+ } )
597+ . catch ( ( err ) => {
598+ // Restore
599+ initStub . restore ( ) ;
600+ removeStub . restore ( ) ;
601+ Object . defineProperty ( firebaseAdmin , 'messaging' , {
602+ get : originalMessaging ,
603+ configurable : true ,
604+ } ) ;
605+ done ( err ) ;
606+ } ) ;
607+ } ) ;
608+ } ) ;
472609} ) ;
0 commit comments