@@ -20,25 +20,30 @@ import chaiAsPromised from 'chai-as-promised';
20
20
import * as sinon from 'sinon' ;
21
21
22
22
import { dcFetch , initializeFetch } from '../../src/network/fetch' ;
23
+ import { CallerSdkType , CallerSdkTypeEnum } from '../../src/network/transport' ;
23
24
use ( chaiAsPromised ) ;
24
- function mockFetch ( json : object ) : void {
25
+ function mockFetch ( json : object , reject : boolean ) : sinon . SinonStub {
25
26
const fakeFetchImpl = sinon . stub ( ) . returns (
26
27
Promise . resolve ( {
27
28
json : ( ) => {
28
29
return Promise . resolve ( json ) ;
29
30
} ,
30
- status : 401
31
+ status : reject ? 401 : 200
31
32
} as Response )
32
33
) ;
33
34
initializeFetch ( fakeFetchImpl ) ;
35
+ return fakeFetchImpl ;
34
36
}
35
37
describe ( 'fetch' , ( ) => {
36
38
it ( 'should throw an error with just the message when the server responds with an error with a message property in the body' , async ( ) => {
37
39
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
+ ) ;
42
47
await expect (
43
48
dcFetch (
44
49
'http://localhost' ,
@@ -51,7 +56,8 @@ describe('fetch', () => {
51
56
null ,
52
57
null ,
53
58
null ,
54
- false
59
+ false ,
60
+ CallerSdkTypeEnum . Base
55
61
)
56
62
) . to . eventually . be . rejectedWith ( message ) ;
57
63
} ) ;
@@ -61,7 +67,7 @@ describe('fetch', () => {
61
67
code : 401 ,
62
68
message1 : message
63
69
} ;
64
- mockFetch ( json ) ;
70
+ mockFetch ( json , true ) ;
65
71
await expect (
66
72
dcFetch (
67
73
'http://localhost' ,
@@ -74,8 +80,105 @@ describe('fetch', () => {
74
80
null ,
75
81
null ,
76
82
null ,
77
- false
83
+ false ,
84
+ CallerSdkTypeEnum . Base
78
85
)
79
86
) . to . eventually . be . rejectedWith ( JSON . stringify ( json ) ) ;
80
87
} ) ;
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
+ } ) ;
81
184
} ) ;
0 commit comments