@@ -24,7 +24,10 @@ import sinonChai from 'sinon-chai';
2424import { FirebaseError , getUA } from '@firebase/util' ;
2525import * as utils from '@firebase/util' ;
2626
27- import { mockEndpoint } from '../../test/helpers/api/helper' ;
27+ import {
28+ mockEndpoint ,
29+ mockRegionalEndpointWithParent
30+ } from '../../test/helpers/api/helper' ;
2831import {
2932 regionalTestAuth ,
3033 testAuth ,
@@ -605,26 +608,113 @@ describe('api/_performApiRequest', () => {
605608 } ) ;
606609
607610 context ( 'throws Operation not allowed exception' , ( ) => {
608- it ( 'when tenantConfig is not initialized and Regional Endpoint is used' , async ( ) => {
611+ it ( 'when tenantConfig is initialized and default Endpoint is used' , async ( ) => {
609612 await expect (
610- _performRegionalApiRequest < typeof request , typeof serverResponse > (
611- auth ,
613+ _performApiRequest < typeof request , typeof serverResponse > (
614+ regionalAuth ,
612615 HttpMethod . POST ,
613- RegionalEndpoint . EXCHANGE_TOKEN ,
616+ Endpoint . SIGN_UP ,
614617 request
615618 )
616619 ) . to . be . rejectedWith (
617620 FirebaseError ,
618621 'Firebase: Operations not allowed for the auth object initialized. (auth/operation-not-allowed).'
619622 ) ;
620623 } ) ;
624+ } ) ;
625+ } ) ;
621626
622- it ( 'when tenantConfig is initialized and default Endpoint is used' , async ( ) => {
627+ describe ( 'api/_performRegionalApiRequest' , ( ) => {
628+ const request = {
629+ requestKey : 'request-value'
630+ } ;
631+
632+ const serverResponse = {
633+ responseKey : 'response-value'
634+ } ;
635+
636+ let auth : TestAuth ;
637+ let regionalAuth : TestAuth ;
638+
639+ beforeEach ( async ( ) => {
640+ auth = await testAuth ( ) ;
641+ regionalAuth = await regionalTestAuth ( ) ;
642+ } ) ;
643+
644+ afterEach ( ( ) => {
645+ sinon . restore ( ) ;
646+ } ) ;
647+
648+ context ( 'with regular requests' , ( ) => {
649+ beforeEach ( mockFetch . setUp ) ;
650+ afterEach ( mockFetch . tearDown ) ;
651+ it ( 'should set the correct request, method and HTTP Headers' , async ( ) => {
652+ const mock = mockRegionalEndpointWithParent (
653+ RegionalEndpoint . EXCHANGE_TOKEN ,
654+ 'test-parent' ,
655+ serverResponse
656+ ) ;
657+ const response = await _performRegionalApiRequest <
658+ typeof request ,
659+ typeof serverResponse
660+ > (
661+ regionalAuth ,
662+ HttpMethod . POST ,
663+ RegionalEndpoint . EXCHANGE_TOKEN ,
664+ request ,
665+ { } ,
666+ 'test-parent'
667+ ) ;
668+ expect ( response ) . to . eql ( serverResponse ) ;
669+ expect ( mock . calls . length ) . to . eq ( 1 ) ;
670+ expect ( mock . calls [ 0 ] . method ) . to . eq ( HttpMethod . POST ) ;
671+ expect ( mock . calls [ 0 ] . request ) . to . eql ( request ) ;
672+ expect ( mock . calls [ 0 ] . headers ! . get ( HttpHeader . CONTENT_TYPE ) ) . to . eq (
673+ 'application/json'
674+ ) ;
675+ expect ( mock . calls [ 0 ] . headers ! . get ( HttpHeader . X_CLIENT_VERSION ) ) . to . eq (
676+ 'testSDK/0.0.0'
677+ ) ;
678+ expect ( mock . calls [ 0 ] . fullRequest ?. credentials ) . to . be . undefined ;
679+ } ) ;
680+
681+ it ( 'should include whatever headers the auth impl attaches' , async ( ) => {
682+ sinon . stub ( regionalAuth , '_getAdditionalHeaders' ) . returns (
683+ Promise . resolve ( {
684+ 'look-at-me-im-a-header' : 'header-value' ,
685+ 'anotherheader' : 'header-value-2'
686+ } )
687+ ) ;
688+
689+ const mock = mockRegionalEndpointWithParent (
690+ RegionalEndpoint . EXCHANGE_TOKEN ,
691+ 'test-parent' ,
692+ serverResponse
693+ ) ;
694+ await _performRegionalApiRequest < typeof request , typeof serverResponse > (
695+ regionalAuth ,
696+ HttpMethod . POST ,
697+ RegionalEndpoint . EXCHANGE_TOKEN ,
698+ request ,
699+ { } ,
700+ 'test-parent'
701+ ) ;
702+ expect ( mock . calls [ 0 ] . headers . get ( 'look-at-me-im-a-header' ) ) . to . eq (
703+ 'header-value'
704+ ) ;
705+ expect ( mock . calls [ 0 ] . headers . get ( 'anotherheader' ) ) . to . eq (
706+ 'header-value-2'
707+ ) ;
708+ } ) ;
709+ } ) ;
710+
711+ context ( 'throws Operation not allowed exception' , ( ) => {
712+ it ( 'when tenantConfig is not initialized and Regional Endpoint is used' , async ( ) => {
623713 await expect (
624- _performApiRequest < typeof request , typeof serverResponse > (
625- regionalAuth ,
714+ _performRegionalApiRequest < typeof request , typeof serverResponse > (
715+ auth ,
626716 HttpMethod . POST ,
627- Endpoint . SIGN_UP ,
717+ RegionalEndpoint . EXCHANGE_TOKEN ,
628718 request
629719 )
630720 ) . to . be . rejectedWith (
0 commit comments