@@ -20,25 +20,30 @@ import chaiAsPromised from 'chai-as-promised';
2020import * as sinon from 'sinon' ;
2121
2222import { dcFetch , initializeFetch } from '../../src/network/fetch' ;
23+ import { CallerSdkType , CallerSdkTypeEnum } from '../../src/network/transport' ;
2324use ( chaiAsPromised ) ;
24- function mockFetch ( json : object ) : void {
25+ function mockFetch ( json : object , reject : boolean ) : sinon . SinonStub {
2526 const fakeFetchImpl = sinon . stub ( ) . returns (
2627 Promise . resolve ( {
2728 json : ( ) => {
2829 return Promise . resolve ( json ) ;
2930 } ,
30- status : 401
31+ status : reject ? 401 : 200
3132 } as Response )
3233 ) ;
3334 initializeFetch ( fakeFetchImpl ) ;
35+ return fakeFetchImpl ;
3436}
3537describe ( 'fetch' , ( ) => {
3638 it ( 'should throw an error with just the message when the server responds with an error with a message property in the body' , async ( ) => {
3739 const message = 'Failed to connect to Postgres instance' ;
38- mockFetch ( {
39- code : 401 ,
40- message
41- } ) ;
40+ mockFetch (
41+ {
42+ code : 401 ,
43+ message
44+ } ,
45+ true
46+ ) ;
4247 await expect (
4348 dcFetch (
4449 'http://localhost' ,
@@ -51,7 +56,8 @@ describe('fetch', () => {
5156 null ,
5257 null ,
5358 null ,
54- false
59+ false ,
60+ CallerSdkTypeEnum . Base
5561 )
5662 ) . to . eventually . be . rejectedWith ( message ) ;
5763 } ) ;
@@ -61,7 +67,7 @@ describe('fetch', () => {
6167 code : 401 ,
6268 message1 : message
6369 } ;
64- mockFetch ( json ) ;
70+ mockFetch ( json , true ) ;
6571 await expect (
6672 dcFetch (
6773 'http://localhost' ,
@@ -74,8 +80,105 @@ describe('fetch', () => {
7480 null ,
7581 null ,
7682 null ,
77- false
83+ false ,
84+ CallerSdkTypeEnum . Base
7885 )
7986 ) . to . eventually . be . rejectedWith ( JSON . stringify ( json ) ) ;
8087 } ) ;
88+ it ( 'should assign different values to custom headers based on the _callerSdkType argument (_isUsingGen is false)' , async ( ) => {
89+ const json = {
90+ code : 200 ,
91+ message1 : 'success'
92+ } ;
93+ const fakeFetchImpl = mockFetch ( json , false ) ;
94+
95+ for ( const callerSdkType in CallerSdkTypeEnum ) {
96+ // this check is done to follow the best practices outlined by the "guard-for-in" eslint rule
97+ if (
98+ Object . prototype . hasOwnProperty . call ( CallerSdkTypeEnum , callerSdkType )
99+ ) {
100+ await dcFetch (
101+ 'http://localhost' ,
102+ {
103+ name : 'n' ,
104+ operationName : 'n' ,
105+ variables : { }
106+ } ,
107+ { } as AbortController ,
108+ null ,
109+ null ,
110+ null ,
111+ false , // _isUsingGen is false
112+ callerSdkType as CallerSdkType
113+ ) ;
114+
115+ let expectedHeaderRegex : RegExp ;
116+ if ( callerSdkType === CallerSdkTypeEnum . Base ) {
117+ // should not contain any "js/xxx" substring
118+ expectedHeaderRegex = RegExp ( / ^ ( (? ! j s \/ \w ) .) * $ / ) ;
119+ } else if ( callerSdkType === CallerSdkTypeEnum . Generated ) {
120+ expectedHeaderRegex = RegExp ( / j s \/ g e n / ) ;
121+ } else {
122+ expectedHeaderRegex = RegExp ( `js\/${ callerSdkType . toLowerCase ( ) } ` ) ;
123+ }
124+ expect (
125+ fakeFetchImpl . calledWithMatch (
126+ 'http://localhost' ,
127+ sinon . match . hasNested (
128+ 'headers.X-Goog-Api-Client' ,
129+ sinon . match ( expectedHeaderRegex )
130+ )
131+ )
132+ ) . to . be . true ;
133+ }
134+ }
135+ } ) ;
136+ it ( 'should assign custom headers based on _callerSdkType before checking to-be-deprecated _isUsingGen' , async ( ) => {
137+ const json = {
138+ code : 200 ,
139+ message1 : 'success'
140+ } ;
141+ const fakeFetchImpl = mockFetch ( json , false ) ;
142+
143+ for ( const callerSdkType in CallerSdkTypeEnum ) {
144+ // this check is done to follow the best practices outlined by the "guard-for-in" eslint rule
145+ if (
146+ Object . prototype . hasOwnProperty . call ( CallerSdkTypeEnum , callerSdkType )
147+ ) {
148+ await dcFetch (
149+ 'http://localhost' ,
150+ {
151+ name : 'n' ,
152+ operationName : 'n' ,
153+ variables : { }
154+ } ,
155+ { } as AbortController ,
156+ null ,
157+ null ,
158+ null ,
159+ true , // _isUsingGen is true
160+ callerSdkType as CallerSdkType
161+ ) ;
162+
163+ let expectedHeaderRegex : RegExp ;
164+ if (
165+ callerSdkType === CallerSdkTypeEnum . Generated ||
166+ callerSdkType === CallerSdkTypeEnum . Base
167+ ) {
168+ expectedHeaderRegex = RegExp ( `js\/gen` ) ;
169+ } else {
170+ expectedHeaderRegex = RegExp ( `js\/${ callerSdkType . toLowerCase ( ) } ` ) ;
171+ }
172+ expect (
173+ fakeFetchImpl . calledWithMatch (
174+ 'http://localhost' ,
175+ sinon . match . hasNested (
176+ 'headers.X-Goog-Api-Client' ,
177+ sinon . match ( expectedHeaderRegex )
178+ )
179+ )
180+ ) . to . be . true ;
181+ }
182+ }
183+ } ) ;
81184} ) ;
0 commit comments