@@ -24,7 +24,10 @@ import sinonChai from 'sinon-chai';
24
24
import { FirebaseError , getUA } from '@firebase/util' ;
25
25
import * as utils from '@firebase/util' ;
26
26
27
- import { mockEndpoint } from '../../test/helpers/api/helper' ;
27
+ import {
28
+ mockEndpoint ,
29
+ mockRegionalEndpointWithParent
30
+ } from '../../test/helpers/api/helper' ;
28
31
import {
29
32
regionalTestAuth ,
30
33
testAuth ,
@@ -605,26 +608,113 @@ describe('api/_performApiRequest', () => {
605
608
} ) ;
606
609
607
610
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 ( ) => {
609
612
await expect (
610
- _performRegionalApiRequest < typeof request , typeof serverResponse > (
611
- auth ,
613
+ _performApiRequest < typeof request , typeof serverResponse > (
614
+ regionalAuth ,
612
615
HttpMethod . POST ,
613
- RegionalEndpoint . EXCHANGE_TOKEN ,
616
+ Endpoint . SIGN_UP ,
614
617
request
615
618
)
616
619
) . to . be . rejectedWith (
617
620
FirebaseError ,
618
621
'Firebase: Operations not allowed for the auth object initialized. (auth/operation-not-allowed).'
619
622
) ;
620
623
} ) ;
624
+ } ) ;
625
+ } ) ;
621
626
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 ( ) => {
623
713
await expect (
624
- _performApiRequest < typeof request , typeof serverResponse > (
625
- regionalAuth ,
714
+ _performRegionalApiRequest < typeof request , typeof serverResponse > (
715
+ auth ,
626
716
HttpMethod . POST ,
627
- Endpoint . SIGN_UP ,
717
+ RegionalEndpoint . EXCHANGE_TOKEN ,
628
718
request
629
719
)
630
720
) . to . be . rejectedWith (
0 commit comments