diff --git a/src/v2/services_client.ts b/src/v2/services_client.ts index 7613358..348cbb7 100644 --- a/src/v2/services_client.ts +++ b/src/v2/services_client.ts @@ -590,7 +590,7 @@ export class ServicesClient { options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = this._gaxModule.routingHeader.fromParams({ - resource: request.resource || '', + resource: request.resource ?? '', }); this.initialize(); return this.innerApiCalls.getIamPolicy(request, options, callback); @@ -686,7 +686,7 @@ export class ServicesClient { options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = this._gaxModule.routingHeader.fromParams({ - resource: request.resource || '', + resource: request.resource ?? '', }); this.initialize(); return this.innerApiCalls.setIamPolicy(request, options, callback); @@ -777,7 +777,7 @@ export class ServicesClient { options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = this._gaxModule.routingHeader.fromParams({ - resource: request.resource || '', + resource: request.resource ?? '', }); this.initialize(); return this.innerApiCalls.testIamPermissions(request, options, callback); diff --git a/test/gapic_revisions_v2.ts b/test/gapic_revisions_v2.ts index 91355c9..1199b86 100644 --- a/test/gapic_revisions_v2.ts +++ b/test/gapic_revisions_v2.ts @@ -27,6 +27,21 @@ import {PassThrough} from 'stream'; import {protobuf, LROperation, operationsProtos} from 'google-gax'; +// Dynamically loaded proto JSON is needed to get the type information +// to fill in default values for request objects +const root = protobuf.Root.fromJSON( + require('../protos/protos.json') +).resolveAll(); + +// eslint-disable-next-line @typescript-eslint/no-unused-vars +function getTypeDefaultValue(typeName: string, fields: string[]) { + let type = root.lookupType(typeName) as protobuf.Type; + for (const field of fields.slice(0, -1)) { + type = type.fields[field]?.resolvedType as protobuf.Type; + } + return type.fields[fields[fields.length - 1]]?.defaultValue; +} + function generateSampleMessage(instance: T) { const filledObject = ( instance.constructor as typeof protobuf.Message @@ -252,33 +267,23 @@ describe('v2.RevisionsClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.GetRevisionRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; // path template: projects/*/locations/{location=*}/** request.name = 'projects/value/locations/value/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const expectedHeaderRequestParams = 'location=value'; const expectedResponse = generateSampleMessage( new protos.google.cloud.run.v2.Revision() ); client.innerApiCalls.getRevision = stubSimpleCall(expectedResponse); const [response] = await client.getRevision(request); assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.getRevision as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = ( + client.innerApiCalls.getRevision as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.getRevision as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes getRevision without error using callback', async () => { @@ -290,22 +295,9 @@ describe('v2.RevisionsClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.GetRevisionRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; // path template: projects/*/locations/{location=*}/** request.name = 'projects/value/locations/value/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const expectedHeaderRequestParams = 'location=value'; const expectedResponse = generateSampleMessage( new protos.google.cloud.run.v2.Revision() ); @@ -328,11 +320,14 @@ describe('v2.RevisionsClient', () => { }); const response = await promise; assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.getRevision as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions /*, callback defined above */) - ); + const actualRequest = ( + client.innerApiCalls.getRevision as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.getRevision as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes getRevision with error', async () => { @@ -344,33 +339,23 @@ describe('v2.RevisionsClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.GetRevisionRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; // path template: projects/*/locations/{location=*}/** request.name = 'projects/value/locations/value/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const expectedHeaderRequestParams = 'location=value'; const expectedError = new Error('expected'); client.innerApiCalls.getRevision = stubSimpleCall( undefined, expectedError ); await assert.rejects(client.getRevision(request), expectedError); - assert( - (client.innerApiCalls.getRevision as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = ( + client.innerApiCalls.getRevision as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.getRevision as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes getRevision with closed client', async () => { @@ -400,22 +385,9 @@ describe('v2.RevisionsClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.DeleteRevisionRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; // path template: projects/*/locations/{location=*}/** request.name = 'projects/value/locations/value/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const expectedHeaderRequestParams = 'location=value'; const expectedResponse = generateSampleMessage( new protos.google.longrunning.Operation() ); @@ -424,11 +396,14 @@ describe('v2.RevisionsClient', () => { const [operation] = await client.deleteRevision(request); const [response] = await operation.promise(); assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.deleteRevision as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = ( + client.innerApiCalls.deleteRevision as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.deleteRevision as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes deleteRevision without error using callback', async () => { @@ -440,22 +415,9 @@ describe('v2.RevisionsClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.DeleteRevisionRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; // path template: projects/*/locations/{location=*}/** request.name = 'projects/value/locations/value/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const expectedHeaderRequestParams = 'location=value'; const expectedResponse = generateSampleMessage( new protos.google.longrunning.Operation() ); @@ -485,11 +447,14 @@ describe('v2.RevisionsClient', () => { >; const [response] = await operation.promise(); assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.deleteRevision as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions /*, callback defined above */) - ); + const actualRequest = ( + client.innerApiCalls.deleteRevision as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.deleteRevision as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes deleteRevision with call error', async () => { @@ -501,33 +466,23 @@ describe('v2.RevisionsClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.DeleteRevisionRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; // path template: projects/*/locations/{location=*}/** request.name = 'projects/value/locations/value/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const expectedHeaderRequestParams = 'location=value'; const expectedError = new Error('expected'); client.innerApiCalls.deleteRevision = stubLongRunningCall( undefined, expectedError ); await assert.rejects(client.deleteRevision(request), expectedError); - assert( - (client.innerApiCalls.deleteRevision as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = ( + client.innerApiCalls.deleteRevision as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.deleteRevision as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes deleteRevision with LRO error', async () => { @@ -539,22 +494,9 @@ describe('v2.RevisionsClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.DeleteRevisionRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; // path template: projects/*/locations/{location=*}/** request.name = 'projects/value/locations/value/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const expectedHeaderRequestParams = 'location=value'; const expectedError = new Error('expected'); client.innerApiCalls.deleteRevision = stubLongRunningCall( undefined, @@ -563,11 +505,14 @@ describe('v2.RevisionsClient', () => { ); const [operation] = await client.deleteRevision(request); await assert.rejects(operation.promise(), expectedError); - assert( - (client.innerApiCalls.deleteRevision as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = ( + client.innerApiCalls.deleteRevision as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.deleteRevision as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes checkDeleteRevisionProgress without error', async () => { @@ -622,22 +567,9 @@ describe('v2.RevisionsClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.ListRevisionsRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; // path template: projects/*/locations/{location=*}/** request.parent = 'projects/value/locations/value/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const expectedHeaderRequestParams = 'location=value'; const expectedResponse = [ generateSampleMessage(new protos.google.cloud.run.v2.Revision()), generateSampleMessage(new protos.google.cloud.run.v2.Revision()), @@ -646,11 +578,14 @@ describe('v2.RevisionsClient', () => { client.innerApiCalls.listRevisions = stubSimpleCall(expectedResponse); const [response] = await client.listRevisions(request); assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.listRevisions as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = ( + client.innerApiCalls.listRevisions as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.listRevisions as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes listRevisions without error using callback', async () => { @@ -662,22 +597,9 @@ describe('v2.RevisionsClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.ListRevisionsRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; // path template: projects/*/locations/{location=*}/** request.parent = 'projects/value/locations/value/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const expectedHeaderRequestParams = 'location=value'; const expectedResponse = [ generateSampleMessage(new protos.google.cloud.run.v2.Revision()), generateSampleMessage(new protos.google.cloud.run.v2.Revision()), @@ -702,11 +624,14 @@ describe('v2.RevisionsClient', () => { }); const response = await promise; assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.listRevisions as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions /*, callback defined above */) - ); + const actualRequest = ( + client.innerApiCalls.listRevisions as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.listRevisions as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes listRevisions with error', async () => { @@ -718,33 +643,23 @@ describe('v2.RevisionsClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.ListRevisionsRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; // path template: projects/*/locations/{location=*}/** request.parent = 'projects/value/locations/value/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const expectedHeaderRequestParams = 'location=value'; const expectedError = new Error('expected'); client.innerApiCalls.listRevisions = stubSimpleCall( undefined, expectedError ); await assert.rejects(client.listRevisions(request), expectedError); - assert( - (client.innerApiCalls.listRevisions as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = ( + client.innerApiCalls.listRevisions as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.listRevisions as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes listRevisionsStream without error', async () => { @@ -756,15 +671,9 @@ describe('v2.RevisionsClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.ListRevisionsRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; // path template: projects/*/locations/{location=*}/** request.parent = 'projects/value/locations/value/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); + const expectedHeaderRequestParams = 'location=value'; const expectedResponse = [ generateSampleMessage(new protos.google.cloud.run.v2.Revision()), generateSampleMessage(new protos.google.cloud.run.v2.Revision()), @@ -792,11 +701,12 @@ describe('v2.RevisionsClient', () => { .getCall(0) .calledWith(client.innerApiCalls.listRevisions, request) ); - assert.strictEqual( - ( - client.descriptors.page.listRevisions.createStream as SinonStub - ).getCall(0).args[2].otherArgs.headers['x-goog-request-params'], - expectedHeaderRequestParams + assert( + (client.descriptors.page.listRevisions.createStream as SinonStub) + .getCall(0) + .args[2].otherArgs.headers['x-goog-request-params'].includes( + expectedHeaderRequestParams + ) ); }); @@ -809,15 +719,9 @@ describe('v2.RevisionsClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.ListRevisionsRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; // path template: projects/*/locations/{location=*}/** request.parent = 'projects/value/locations/value/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); + const expectedHeaderRequestParams = 'location=value'; const expectedError = new Error('expected'); client.descriptors.page.listRevisions.createStream = stubPageStreamingCall(undefined, expectedError); @@ -840,11 +744,12 @@ describe('v2.RevisionsClient', () => { .getCall(0) .calledWith(client.innerApiCalls.listRevisions, request) ); - assert.strictEqual( - ( - client.descriptors.page.listRevisions.createStream as SinonStub - ).getCall(0).args[2].otherArgs.headers['x-goog-request-params'], - expectedHeaderRequestParams + assert( + (client.descriptors.page.listRevisions.createStream as SinonStub) + .getCall(0) + .args[2].otherArgs.headers['x-goog-request-params'].includes( + expectedHeaderRequestParams + ) ); }); @@ -857,15 +762,9 @@ describe('v2.RevisionsClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.ListRevisionsRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; // path template: projects/*/locations/{location=*}/** request.parent = 'projects/value/locations/value/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); + const expectedHeaderRequestParams = 'location=value'; const expectedResponse = [ generateSampleMessage(new protos.google.cloud.run.v2.Revision()), generateSampleMessage(new protos.google.cloud.run.v2.Revision()), @@ -885,11 +784,12 @@ describe('v2.RevisionsClient', () => { ).getCall(0).args[1], request ); - assert.strictEqual( - ( - client.descriptors.page.listRevisions.asyncIterate as SinonStub - ).getCall(0).args[2].otherArgs.headers['x-goog-request-params'], - expectedHeaderRequestParams + assert( + (client.descriptors.page.listRevisions.asyncIterate as SinonStub) + .getCall(0) + .args[2].otherArgs.headers['x-goog-request-params'].includes( + expectedHeaderRequestParams + ) ); }); @@ -902,15 +802,9 @@ describe('v2.RevisionsClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.ListRevisionsRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; // path template: projects/*/locations/{location=*}/** request.parent = 'projects/value/locations/value/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); + const expectedHeaderRequestParams = 'location=value'; const expectedError = new Error('expected'); client.descriptors.page.listRevisions.asyncIterate = stubAsyncIterationCall(undefined, expectedError); @@ -927,11 +821,12 @@ describe('v2.RevisionsClient', () => { ).getCall(0).args[1], request ); - assert.strictEqual( - ( - client.descriptors.page.listRevisions.asyncIterate as SinonStub - ).getCall(0).args[2].otherArgs.headers['x-goog-request-params'], - expectedHeaderRequestParams + assert( + (client.descriptors.page.listRevisions.asyncIterate as SinonStub) + .getCall(0) + .args[2].otherArgs.headers['x-goog-request-params'].includes( + expectedHeaderRequestParams + ) ); }); }); diff --git a/test/gapic_services_v2.ts b/test/gapic_services_v2.ts index 099f122..6f5e0c1 100644 --- a/test/gapic_services_v2.ts +++ b/test/gapic_services_v2.ts @@ -27,6 +27,21 @@ import {PassThrough} from 'stream'; import {protobuf, LROperation, operationsProtos} from 'google-gax'; +// Dynamically loaded proto JSON is needed to get the type information +// to fill in default values for request objects +const root = protobuf.Root.fromJSON( + require('../protos/protos.json') +).resolveAll(); + +// eslint-disable-next-line @typescript-eslint/no-unused-vars +function getTypeDefaultValue(typeName: string, fields: string[]) { + let type = root.lookupType(typeName) as protobuf.Type; + for (const field of fields.slice(0, -1)) { + type = type.fields[field]?.resolvedType as protobuf.Type; + } + return type.fields[fields[fields.length - 1]]?.defaultValue; +} + function generateSampleMessage(instance: T) { const filledObject = ( instance.constructor as typeof protobuf.Message @@ -252,33 +267,23 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.GetServiceRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; // path template: projects/*/locations/{location=*}/** request.name = 'projects/value/locations/value/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const expectedHeaderRequestParams = 'location=value'; const expectedResponse = generateSampleMessage( new protos.google.cloud.run.v2.Service() ); client.innerApiCalls.getService = stubSimpleCall(expectedResponse); const [response] = await client.getService(request); assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.getService as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = ( + client.innerApiCalls.getService as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.getService as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes getService without error using callback', async () => { @@ -290,22 +295,9 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.GetServiceRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; // path template: projects/*/locations/{location=*}/** request.name = 'projects/value/locations/value/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const expectedHeaderRequestParams = 'location=value'; const expectedResponse = generateSampleMessage( new protos.google.cloud.run.v2.Service() ); @@ -328,11 +320,14 @@ describe('v2.ServicesClient', () => { }); const response = await promise; assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.getService as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions /*, callback defined above */) - ); + const actualRequest = ( + client.innerApiCalls.getService as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.getService as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes getService with error', async () => { @@ -344,33 +339,23 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.GetServiceRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; // path template: projects/*/locations/{location=*}/** request.name = 'projects/value/locations/value/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const expectedHeaderRequestParams = 'location=value'; const expectedError = new Error('expected'); client.innerApiCalls.getService = stubSimpleCall( undefined, expectedError ); await assert.rejects(client.getService(request), expectedError); - assert( - (client.innerApiCalls.getService as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = ( + client.innerApiCalls.getService as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.getService as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes getService with closed client', async () => { @@ -400,26 +385,25 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.iam.v1.GetIamPolicyRequest() ); - request.resource = ''; - const expectedHeaderRequestParams = 'resource='; - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const defaultValue1 = getTypeDefaultValue('GetIamPolicyRequest', [ + 'resource', + ]); + request.resource = defaultValue1; + const expectedHeaderRequestParams = `resource=${defaultValue1}`; const expectedResponse = generateSampleMessage( new protos.google.iam.v1.Policy() ); client.innerApiCalls.getIamPolicy = stubSimpleCall(expectedResponse); const [response] = await client.getIamPolicy(request); assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.getIamPolicy as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = ( + client.innerApiCalls.getIamPolicy as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.getIamPolicy as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes getIamPolicy without error using callback', async () => { @@ -431,15 +415,11 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.iam.v1.GetIamPolicyRequest() ); - request.resource = ''; - const expectedHeaderRequestParams = 'resource='; - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const defaultValue1 = getTypeDefaultValue('GetIamPolicyRequest', [ + 'resource', + ]); + request.resource = defaultValue1; + const expectedHeaderRequestParams = `resource=${defaultValue1}`; const expectedResponse = generateSampleMessage( new protos.google.iam.v1.Policy() ); @@ -462,11 +442,14 @@ describe('v2.ServicesClient', () => { }); const response = await promise; assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.getIamPolicy as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions /*, callback defined above */) - ); + const actualRequest = ( + client.innerApiCalls.getIamPolicy as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.getIamPolicy as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes getIamPolicy with error', async () => { @@ -478,26 +461,25 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.iam.v1.GetIamPolicyRequest() ); - request.resource = ''; - const expectedHeaderRequestParams = 'resource='; - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const defaultValue1 = getTypeDefaultValue('GetIamPolicyRequest', [ + 'resource', + ]); + request.resource = defaultValue1; + const expectedHeaderRequestParams = `resource=${defaultValue1}`; const expectedError = new Error('expected'); client.innerApiCalls.getIamPolicy = stubSimpleCall( undefined, expectedError ); await assert.rejects(client.getIamPolicy(request), expectedError); - assert( - (client.innerApiCalls.getIamPolicy as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = ( + client.innerApiCalls.getIamPolicy as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.getIamPolicy as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes getIamPolicy with closed client', async () => { @@ -509,7 +491,10 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.iam.v1.GetIamPolicyRequest() ); - request.resource = ''; + const defaultValue1 = getTypeDefaultValue('GetIamPolicyRequest', [ + 'resource', + ]); + request.resource = defaultValue1; const expectedError = new Error('The client has already been closed.'); client.close(); await assert.rejects(client.getIamPolicy(request), expectedError); @@ -526,26 +511,25 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.iam.v1.SetIamPolicyRequest() ); - request.resource = ''; - const expectedHeaderRequestParams = 'resource='; - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const defaultValue1 = getTypeDefaultValue('SetIamPolicyRequest', [ + 'resource', + ]); + request.resource = defaultValue1; + const expectedHeaderRequestParams = `resource=${defaultValue1}`; const expectedResponse = generateSampleMessage( new protos.google.iam.v1.Policy() ); client.innerApiCalls.setIamPolicy = stubSimpleCall(expectedResponse); const [response] = await client.setIamPolicy(request); assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.setIamPolicy as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = ( + client.innerApiCalls.setIamPolicy as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.setIamPolicy as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes setIamPolicy without error using callback', async () => { @@ -557,15 +541,11 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.iam.v1.SetIamPolicyRequest() ); - request.resource = ''; - const expectedHeaderRequestParams = 'resource='; - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const defaultValue1 = getTypeDefaultValue('SetIamPolicyRequest', [ + 'resource', + ]); + request.resource = defaultValue1; + const expectedHeaderRequestParams = `resource=${defaultValue1}`; const expectedResponse = generateSampleMessage( new protos.google.iam.v1.Policy() ); @@ -588,11 +568,14 @@ describe('v2.ServicesClient', () => { }); const response = await promise; assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.setIamPolicy as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions /*, callback defined above */) - ); + const actualRequest = ( + client.innerApiCalls.setIamPolicy as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.setIamPolicy as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes setIamPolicy with error', async () => { @@ -604,26 +587,25 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.iam.v1.SetIamPolicyRequest() ); - request.resource = ''; - const expectedHeaderRequestParams = 'resource='; - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const defaultValue1 = getTypeDefaultValue('SetIamPolicyRequest', [ + 'resource', + ]); + request.resource = defaultValue1; + const expectedHeaderRequestParams = `resource=${defaultValue1}`; const expectedError = new Error('expected'); client.innerApiCalls.setIamPolicy = stubSimpleCall( undefined, expectedError ); await assert.rejects(client.setIamPolicy(request), expectedError); - assert( - (client.innerApiCalls.setIamPolicy as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = ( + client.innerApiCalls.setIamPolicy as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.setIamPolicy as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes setIamPolicy with closed client', async () => { @@ -635,7 +617,10 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.iam.v1.SetIamPolicyRequest() ); - request.resource = ''; + const defaultValue1 = getTypeDefaultValue('SetIamPolicyRequest', [ + 'resource', + ]); + request.resource = defaultValue1; const expectedError = new Error('The client has already been closed.'); client.close(); await assert.rejects(client.setIamPolicy(request), expectedError); @@ -652,15 +637,11 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.iam.v1.TestIamPermissionsRequest() ); - request.resource = ''; - const expectedHeaderRequestParams = 'resource='; - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const defaultValue1 = getTypeDefaultValue('TestIamPermissionsRequest', [ + 'resource', + ]); + request.resource = defaultValue1; + const expectedHeaderRequestParams = `resource=${defaultValue1}`; const expectedResponse = generateSampleMessage( new protos.google.iam.v1.TestIamPermissionsResponse() ); @@ -668,11 +649,14 @@ describe('v2.ServicesClient', () => { stubSimpleCall(expectedResponse); const [response] = await client.testIamPermissions(request); assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.testIamPermissions as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = ( + client.innerApiCalls.testIamPermissions as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.testIamPermissions as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes testIamPermissions without error using callback', async () => { @@ -684,15 +668,11 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.iam.v1.TestIamPermissionsRequest() ); - request.resource = ''; - const expectedHeaderRequestParams = 'resource='; - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const defaultValue1 = getTypeDefaultValue('TestIamPermissionsRequest', [ + 'resource', + ]); + request.resource = defaultValue1; + const expectedHeaderRequestParams = `resource=${defaultValue1}`; const expectedResponse = generateSampleMessage( new protos.google.iam.v1.TestIamPermissionsResponse() ); @@ -715,11 +695,14 @@ describe('v2.ServicesClient', () => { }); const response = await promise; assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.testIamPermissions as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions /*, callback defined above */) - ); + const actualRequest = ( + client.innerApiCalls.testIamPermissions as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.testIamPermissions as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes testIamPermissions with error', async () => { @@ -731,26 +714,25 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.iam.v1.TestIamPermissionsRequest() ); - request.resource = ''; - const expectedHeaderRequestParams = 'resource='; - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const defaultValue1 = getTypeDefaultValue('TestIamPermissionsRequest', [ + 'resource', + ]); + request.resource = defaultValue1; + const expectedHeaderRequestParams = `resource=${defaultValue1}`; const expectedError = new Error('expected'); client.innerApiCalls.testIamPermissions = stubSimpleCall( undefined, expectedError ); await assert.rejects(client.testIamPermissions(request), expectedError); - assert( - (client.innerApiCalls.testIamPermissions as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = ( + client.innerApiCalls.testIamPermissions as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.testIamPermissions as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes testIamPermissions with closed client', async () => { @@ -762,7 +744,10 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.iam.v1.TestIamPermissionsRequest() ); - request.resource = ''; + const defaultValue1 = getTypeDefaultValue('TestIamPermissionsRequest', [ + 'resource', + ]); + request.resource = defaultValue1; const expectedError = new Error('The client has already been closed.'); client.close(); await assert.rejects(client.testIamPermissions(request), expectedError); @@ -779,22 +764,9 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.CreateServiceRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; // path template: projects/*/locations/{location=*} request.parent = 'projects/value/locations/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const expectedHeaderRequestParams = 'location=value'; const expectedResponse = generateSampleMessage( new protos.google.longrunning.Operation() ); @@ -803,11 +775,14 @@ describe('v2.ServicesClient', () => { const [operation] = await client.createService(request); const [response] = await operation.promise(); assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.createService as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = ( + client.innerApiCalls.createService as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.createService as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes createService without error using callback', async () => { @@ -819,22 +794,9 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.CreateServiceRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; // path template: projects/*/locations/{location=*} request.parent = 'projects/value/locations/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const expectedHeaderRequestParams = 'location=value'; const expectedResponse = generateSampleMessage( new protos.google.longrunning.Operation() ); @@ -864,11 +826,14 @@ describe('v2.ServicesClient', () => { >; const [response] = await operation.promise(); assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.createService as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions /*, callback defined above */) - ); + const actualRequest = ( + client.innerApiCalls.createService as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.createService as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes createService with call error', async () => { @@ -880,33 +845,23 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.CreateServiceRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; // path template: projects/*/locations/{location=*} request.parent = 'projects/value/locations/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const expectedHeaderRequestParams = 'location=value'; const expectedError = new Error('expected'); client.innerApiCalls.createService = stubLongRunningCall( undefined, expectedError ); await assert.rejects(client.createService(request), expectedError); - assert( - (client.innerApiCalls.createService as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = ( + client.innerApiCalls.createService as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.createService as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes createService with LRO error', async () => { @@ -918,22 +873,9 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.CreateServiceRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; // path template: projects/*/locations/{location=*} request.parent = 'projects/value/locations/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const expectedHeaderRequestParams = 'location=value'; const expectedError = new Error('expected'); client.innerApiCalls.createService = stubLongRunningCall( undefined, @@ -942,11 +884,14 @@ describe('v2.ServicesClient', () => { ); const [operation] = await client.createService(request); await assert.rejects(operation.promise(), expectedError); - assert( - (client.innerApiCalls.createService as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = ( + client.innerApiCalls.createService as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.createService as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes checkCreateServiceProgress without error', async () => { @@ -1001,23 +946,10 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.UpdateServiceRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; request.service = {}; // path template: projects/*/locations/{location=*}/** request.service.name = 'projects/value/locations/value/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const expectedHeaderRequestParams = 'location=value'; const expectedResponse = generateSampleMessage( new protos.google.longrunning.Operation() ); @@ -1026,11 +958,14 @@ describe('v2.ServicesClient', () => { const [operation] = await client.updateService(request); const [response] = await operation.promise(); assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.updateService as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = ( + client.innerApiCalls.updateService as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.updateService as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes updateService without error using callback', async () => { @@ -1042,23 +977,10 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.UpdateServiceRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; request.service = {}; // path template: projects/*/locations/{location=*}/** request.service.name = 'projects/value/locations/value/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const expectedHeaderRequestParams = 'location=value'; const expectedResponse = generateSampleMessage( new protos.google.longrunning.Operation() ); @@ -1088,11 +1010,14 @@ describe('v2.ServicesClient', () => { >; const [response] = await operation.promise(); assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.updateService as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions /*, callback defined above */) - ); + const actualRequest = ( + client.innerApiCalls.updateService as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.updateService as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes updateService with call error', async () => { @@ -1104,34 +1029,24 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.UpdateServiceRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; request.service = {}; // path template: projects/*/locations/{location=*}/** request.service.name = 'projects/value/locations/value/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const expectedHeaderRequestParams = 'location=value'; const expectedError = new Error('expected'); client.innerApiCalls.updateService = stubLongRunningCall( undefined, expectedError ); await assert.rejects(client.updateService(request), expectedError); - assert( - (client.innerApiCalls.updateService as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = ( + client.innerApiCalls.updateService as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.updateService as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes updateService with LRO error', async () => { @@ -1143,23 +1058,10 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.UpdateServiceRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; request.service = {}; // path template: projects/*/locations/{location=*}/** request.service.name = 'projects/value/locations/value/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const expectedHeaderRequestParams = 'location=value'; const expectedError = new Error('expected'); client.innerApiCalls.updateService = stubLongRunningCall( undefined, @@ -1168,11 +1070,14 @@ describe('v2.ServicesClient', () => { ); const [operation] = await client.updateService(request); await assert.rejects(operation.promise(), expectedError); - assert( - (client.innerApiCalls.updateService as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = ( + client.innerApiCalls.updateService as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.updateService as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes checkUpdateServiceProgress without error', async () => { @@ -1227,22 +1132,9 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.DeleteServiceRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; // path template: projects/*/locations/{location=*}/** request.name = 'projects/value/locations/value/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const expectedHeaderRequestParams = 'location=value'; const expectedResponse = generateSampleMessage( new protos.google.longrunning.Operation() ); @@ -1251,11 +1143,14 @@ describe('v2.ServicesClient', () => { const [operation] = await client.deleteService(request); const [response] = await operation.promise(); assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.deleteService as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = ( + client.innerApiCalls.deleteService as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.deleteService as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes deleteService without error using callback', async () => { @@ -1267,22 +1162,9 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.DeleteServiceRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; // path template: projects/*/locations/{location=*}/** request.name = 'projects/value/locations/value/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const expectedHeaderRequestParams = 'location=value'; const expectedResponse = generateSampleMessage( new protos.google.longrunning.Operation() ); @@ -1312,11 +1194,14 @@ describe('v2.ServicesClient', () => { >; const [response] = await operation.promise(); assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.deleteService as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions /*, callback defined above */) - ); + const actualRequest = ( + client.innerApiCalls.deleteService as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.deleteService as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes deleteService with call error', async () => { @@ -1328,33 +1213,23 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.DeleteServiceRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; // path template: projects/*/locations/{location=*}/** request.name = 'projects/value/locations/value/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const expectedHeaderRequestParams = 'location=value'; const expectedError = new Error('expected'); client.innerApiCalls.deleteService = stubLongRunningCall( undefined, expectedError ); await assert.rejects(client.deleteService(request), expectedError); - assert( - (client.innerApiCalls.deleteService as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = ( + client.innerApiCalls.deleteService as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.deleteService as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes deleteService with LRO error', async () => { @@ -1366,22 +1241,9 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.DeleteServiceRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; // path template: projects/*/locations/{location=*}/** request.name = 'projects/value/locations/value/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const expectedHeaderRequestParams = 'location=value'; const expectedError = new Error('expected'); client.innerApiCalls.deleteService = stubLongRunningCall( undefined, @@ -1390,11 +1252,14 @@ describe('v2.ServicesClient', () => { ); const [operation] = await client.deleteService(request); await assert.rejects(operation.promise(), expectedError); - assert( - (client.innerApiCalls.deleteService as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = ( + client.innerApiCalls.deleteService as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.deleteService as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes checkDeleteServiceProgress without error', async () => { @@ -1449,22 +1314,9 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.ListServicesRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; // path template: projects/*/locations/{location=*} request.parent = 'projects/value/locations/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const expectedHeaderRequestParams = 'location=value'; const expectedResponse = [ generateSampleMessage(new protos.google.cloud.run.v2.Service()), generateSampleMessage(new protos.google.cloud.run.v2.Service()), @@ -1473,11 +1325,14 @@ describe('v2.ServicesClient', () => { client.innerApiCalls.listServices = stubSimpleCall(expectedResponse); const [response] = await client.listServices(request); assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.listServices as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = ( + client.innerApiCalls.listServices as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.listServices as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes listServices without error using callback', async () => { @@ -1489,22 +1344,9 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.ListServicesRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; // path template: projects/*/locations/{location=*} request.parent = 'projects/value/locations/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const expectedHeaderRequestParams = 'location=value'; const expectedResponse = [ generateSampleMessage(new protos.google.cloud.run.v2.Service()), generateSampleMessage(new protos.google.cloud.run.v2.Service()), @@ -1529,11 +1371,14 @@ describe('v2.ServicesClient', () => { }); const response = await promise; assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.listServices as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions /*, callback defined above */) - ); + const actualRequest = ( + client.innerApiCalls.listServices as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.listServices as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes listServices with error', async () => { @@ -1545,33 +1390,23 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.ListServicesRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; // path template: projects/*/locations/{location=*} request.parent = 'projects/value/locations/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const expectedHeaderRequestParams = 'location=value'; const expectedError = new Error('expected'); client.innerApiCalls.listServices = stubSimpleCall( undefined, expectedError ); await assert.rejects(client.listServices(request), expectedError); - assert( - (client.innerApiCalls.listServices as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = ( + client.innerApiCalls.listServices as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.listServices as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes listServicesStream without error', async () => { @@ -1583,15 +1418,9 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.ListServicesRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; // path template: projects/*/locations/{location=*} request.parent = 'projects/value/locations/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); + const expectedHeaderRequestParams = 'location=value'; const expectedResponse = [ generateSampleMessage(new protos.google.cloud.run.v2.Service()), generateSampleMessage(new protos.google.cloud.run.v2.Service()), @@ -1619,11 +1448,12 @@ describe('v2.ServicesClient', () => { .getCall(0) .calledWith(client.innerApiCalls.listServices, request) ); - assert.strictEqual( - ( - client.descriptors.page.listServices.createStream as SinonStub - ).getCall(0).args[2].otherArgs.headers['x-goog-request-params'], - expectedHeaderRequestParams + assert( + (client.descriptors.page.listServices.createStream as SinonStub) + .getCall(0) + .args[2].otherArgs.headers['x-goog-request-params'].includes( + expectedHeaderRequestParams + ) ); }); @@ -1636,15 +1466,9 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.ListServicesRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; // path template: projects/*/locations/{location=*} request.parent = 'projects/value/locations/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); + const expectedHeaderRequestParams = 'location=value'; const expectedError = new Error('expected'); client.descriptors.page.listServices.createStream = stubPageStreamingCall( undefined, @@ -1669,11 +1493,12 @@ describe('v2.ServicesClient', () => { .getCall(0) .calledWith(client.innerApiCalls.listServices, request) ); - assert.strictEqual( - ( - client.descriptors.page.listServices.createStream as SinonStub - ).getCall(0).args[2].otherArgs.headers['x-goog-request-params'], - expectedHeaderRequestParams + assert( + (client.descriptors.page.listServices.createStream as SinonStub) + .getCall(0) + .args[2].otherArgs.headers['x-goog-request-params'].includes( + expectedHeaderRequestParams + ) ); }); @@ -1686,15 +1511,9 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.ListServicesRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; // path template: projects/*/locations/{location=*} request.parent = 'projects/value/locations/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); + const expectedHeaderRequestParams = 'location=value'; const expectedResponse = [ generateSampleMessage(new protos.google.cloud.run.v2.Service()), generateSampleMessage(new protos.google.cloud.run.v2.Service()), @@ -1714,11 +1533,12 @@ describe('v2.ServicesClient', () => { ).getCall(0).args[1], request ); - assert.strictEqual( - ( - client.descriptors.page.listServices.asyncIterate as SinonStub - ).getCall(0).args[2].otherArgs.headers['x-goog-request-params'], - expectedHeaderRequestParams + assert( + (client.descriptors.page.listServices.asyncIterate as SinonStub) + .getCall(0) + .args[2].otherArgs.headers['x-goog-request-params'].includes( + expectedHeaderRequestParams + ) ); }); @@ -1731,15 +1551,9 @@ describe('v2.ServicesClient', () => { const request = generateSampleMessage( new protos.google.cloud.run.v2.ListServicesRequest() ); - const expectedHeaderRequestParamsObj: {[key: string]: string} = {}; // path template: projects/*/locations/{location=*} request.parent = 'projects/value/locations/value'; - expectedHeaderRequestParamsObj['location'] = 'value'; - const expectedHeaderRequestParams = Object.entries( - expectedHeaderRequestParamsObj - ) - .map(([key, value]) => `${key}=${value}`) - .join('&'); + const expectedHeaderRequestParams = 'location=value'; const expectedError = new Error('expected'); client.descriptors.page.listServices.asyncIterate = stubAsyncIterationCall(undefined, expectedError); @@ -1756,11 +1570,12 @@ describe('v2.ServicesClient', () => { ).getCall(0).args[1], request ); - assert.strictEqual( - ( - client.descriptors.page.listServices.asyncIterate as SinonStub - ).getCall(0).args[2].otherArgs.headers['x-goog-request-params'], - expectedHeaderRequestParams + assert( + (client.descriptors.page.listServices.asyncIterate as SinonStub) + .getCall(0) + .args[2].otherArgs.headers['x-goog-request-params'].includes( + expectedHeaderRequestParams + ) ); }); });