@@ -25,6 +25,21 @@ import * as phishingprotectionservicev1beta1Module from '../src';
25
25
26
26
import { protobuf } from 'google-gax' ;
27
27
28
+ // Dynamically loaded proto JSON is needed to get the type information
29
+ // to fill in default values for request objects
30
+ const root = protobuf . Root . fromJSON (
31
+ require ( '../protos/protos.json' )
32
+ ) . resolveAll ( ) ;
33
+
34
+ // eslint-disable-next-line @typescript-eslint/no-unused-vars
35
+ function getTypeDefaultValue ( typeName : string , fields : string [ ] ) {
36
+ let type = root . lookupType ( typeName ) as protobuf . Type ;
37
+ for ( const field of fields . slice ( 0 , - 1 ) ) {
38
+ type = type . fields [ field ] ?. resolvedType as protobuf . Type ;
39
+ }
40
+ return type . fields [ fields [ fields . length - 1 ] ] ?. defaultValue ;
41
+ }
42
+
28
43
function generateSampleMessage < T extends object > ( instance : T ) {
29
44
const filledObject = (
30
45
instance . constructor as typeof protobuf . Message
@@ -191,26 +206,25 @@ describe('v1beta1.PhishingProtectionServiceV1Beta1Client', () => {
191
206
const request = generateSampleMessage (
192
207
new protos . google . cloud . phishingprotection . v1beta1 . ReportPhishingRequest ( )
193
208
) ;
194
- request . parent = '' ;
195
- const expectedHeaderRequestParams = 'parent=' ;
196
- const expectedOptions = {
197
- otherArgs : {
198
- headers : {
199
- 'x-goog-request-params' : expectedHeaderRequestParams ,
200
- } ,
201
- } ,
202
- } ;
209
+ const defaultValue1 = getTypeDefaultValue ( 'ReportPhishingRequest' , [
210
+ 'parent' ,
211
+ ] ) ;
212
+ request . parent = defaultValue1 ;
213
+ const expectedHeaderRequestParams = `parent=${ defaultValue1 } ` ;
203
214
const expectedResponse = generateSampleMessage (
204
215
new protos . google . cloud . phishingprotection . v1beta1 . ReportPhishingResponse ( )
205
216
) ;
206
217
client . innerApiCalls . reportPhishing = stubSimpleCall ( expectedResponse ) ;
207
218
const [ response ] = await client . reportPhishing ( request ) ;
208
219
assert . deepStrictEqual ( response , expectedResponse ) ;
209
- assert (
210
- ( client . innerApiCalls . reportPhishing as SinonStub )
211
- . getCall ( 0 )
212
- . calledWith ( request , expectedOptions , undefined )
213
- ) ;
220
+ const actualRequest = (
221
+ client . innerApiCalls . reportPhishing as SinonStub
222
+ ) . getCall ( 0 ) . args [ 0 ] ;
223
+ assert . deepStrictEqual ( actualRequest , request ) ;
224
+ const actualHeaderRequestParams = (
225
+ client . innerApiCalls . reportPhishing as SinonStub
226
+ ) . getCall ( 0 ) . args [ 1 ] . otherArgs . headers [ 'x-goog-request-params' ] ;
227
+ assert ( actualHeaderRequestParams . includes ( expectedHeaderRequestParams ) ) ;
214
228
} ) ;
215
229
216
230
it ( 'invokes reportPhishing without error using callback' , async ( ) => {
@@ -225,15 +239,11 @@ describe('v1beta1.PhishingProtectionServiceV1Beta1Client', () => {
225
239
const request = generateSampleMessage (
226
240
new protos . google . cloud . phishingprotection . v1beta1 . ReportPhishingRequest ( )
227
241
) ;
228
- request . parent = '' ;
229
- const expectedHeaderRequestParams = 'parent=' ;
230
- const expectedOptions = {
231
- otherArgs : {
232
- headers : {
233
- 'x-goog-request-params' : expectedHeaderRequestParams ,
234
- } ,
235
- } ,
236
- } ;
242
+ const defaultValue1 = getTypeDefaultValue ( 'ReportPhishingRequest' , [
243
+ 'parent' ,
244
+ ] ) ;
245
+ request . parent = defaultValue1 ;
246
+ const expectedHeaderRequestParams = `parent=${ defaultValue1 } ` ;
237
247
const expectedResponse = generateSampleMessage (
238
248
new protos . google . cloud . phishingprotection . v1beta1 . ReportPhishingResponse ( )
239
249
) ;
@@ -256,11 +266,14 @@ describe('v1beta1.PhishingProtectionServiceV1Beta1Client', () => {
256
266
} ) ;
257
267
const response = await promise ;
258
268
assert . deepStrictEqual ( response , expectedResponse ) ;
259
- assert (
260
- ( client . innerApiCalls . reportPhishing as SinonStub )
261
- . getCall ( 0 )
262
- . calledWith ( request , expectedOptions /*, callback defined above */ )
263
- ) ;
269
+ const actualRequest = (
270
+ client . innerApiCalls . reportPhishing as SinonStub
271
+ ) . getCall ( 0 ) . args [ 0 ] ;
272
+ assert . deepStrictEqual ( actualRequest , request ) ;
273
+ const actualHeaderRequestParams = (
274
+ client . innerApiCalls . reportPhishing as SinonStub
275
+ ) . getCall ( 0 ) . args [ 1 ] . otherArgs . headers [ 'x-goog-request-params' ] ;
276
+ assert ( actualHeaderRequestParams . includes ( expectedHeaderRequestParams ) ) ;
264
277
} ) ;
265
278
266
279
it ( 'invokes reportPhishing with error' , async ( ) => {
@@ -275,26 +288,25 @@ describe('v1beta1.PhishingProtectionServiceV1Beta1Client', () => {
275
288
const request = generateSampleMessage (
276
289
new protos . google . cloud . phishingprotection . v1beta1 . ReportPhishingRequest ( )
277
290
) ;
278
- request . parent = '' ;
279
- const expectedHeaderRequestParams = 'parent=' ;
280
- const expectedOptions = {
281
- otherArgs : {
282
- headers : {
283
- 'x-goog-request-params' : expectedHeaderRequestParams ,
284
- } ,
285
- } ,
286
- } ;
291
+ const defaultValue1 = getTypeDefaultValue ( 'ReportPhishingRequest' , [
292
+ 'parent' ,
293
+ ] ) ;
294
+ request . parent = defaultValue1 ;
295
+ const expectedHeaderRequestParams = `parent=${ defaultValue1 } ` ;
287
296
const expectedError = new Error ( 'expected' ) ;
288
297
client . innerApiCalls . reportPhishing = stubSimpleCall (
289
298
undefined ,
290
299
expectedError
291
300
) ;
292
301
await assert . rejects ( client . reportPhishing ( request ) , expectedError ) ;
293
- assert (
294
- ( client . innerApiCalls . reportPhishing as SinonStub )
295
- . getCall ( 0 )
296
- . calledWith ( request , expectedOptions , undefined )
297
- ) ;
302
+ const actualRequest = (
303
+ client . innerApiCalls . reportPhishing as SinonStub
304
+ ) . getCall ( 0 ) . args [ 0 ] ;
305
+ assert . deepStrictEqual ( actualRequest , request ) ;
306
+ const actualHeaderRequestParams = (
307
+ client . innerApiCalls . reportPhishing as SinonStub
308
+ ) . getCall ( 0 ) . args [ 1 ] . otherArgs . headers [ 'x-goog-request-params' ] ;
309
+ assert ( actualHeaderRequestParams . includes ( expectedHeaderRequestParams ) ) ;
298
310
} ) ;
299
311
300
312
it ( 'invokes reportPhishing with closed client' , async ( ) => {
@@ -309,7 +321,10 @@ describe('v1beta1.PhishingProtectionServiceV1Beta1Client', () => {
309
321
const request = generateSampleMessage (
310
322
new protos . google . cloud . phishingprotection . v1beta1 . ReportPhishingRequest ( )
311
323
) ;
312
- request . parent = '' ;
324
+ const defaultValue1 = getTypeDefaultValue ( 'ReportPhishingRequest' , [
325
+ 'parent' ,
326
+ ] ) ;
327
+ request . parent = defaultValue1 ;
313
328
const expectedError = new Error ( 'The client has already been closed.' ) ;
314
329
client . close ( ) ;
315
330
await assert . rejects ( client . reportPhishing ( request ) , expectedError ) ;
0 commit comments