From 4216c6df4596783fca6d100a69ff6acf0b8d436f Mon Sep 17 00:00:00 2001 From: "gcf-owl-bot[bot]" <78513119+gcf-owl-bot[bot]@users.noreply.github.com> Date: Wed, 14 Sep 2022 22:34:20 +0000 Subject: [PATCH] fix: preserve default values in x-goog-request-params header (#156) - [ ] Regenerate this pull request now. PiperOrigin-RevId: 474338479 Source-Link: https://github.com/googleapis/googleapis/commit/d5d35e0353b59719e8917103b1bc7df2782bf6ba Source-Link: https://github.com/googleapis/googleapis-gen/commit/efcd3f93962a103f68f003e2a1eecde6fa216a27 Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiZWZjZDNmOTM5NjJhMTAzZjY4ZjAwM2UyYTFlZWNkZTZmYTIxNmEyNyJ9 --- .../src/v1/quota_controller_client.ts | 2 +- .../src/v1/service_controller_client.ts | 4 +- .../src/v2/service_controller_client.ts | 4 +- .../test/gapic_quota_controller_v1.ts | 101 ++++++---- .../test/gapic_service_controller_v1.ts | 184 ++++++++++-------- .../test/gapic_service_controller_v2.ts | 184 ++++++++++-------- 6 files changed, 259 insertions(+), 220 deletions(-) diff --git a/packages/google-api-servicecontrol/src/v1/quota_controller_client.ts b/packages/google-api-servicecontrol/src/v1/quota_controller_client.ts index 7a26c4d3ba3..045e856e6eb 100644 --- a/packages/google-api-servicecontrol/src/v1/quota_controller_client.ts +++ b/packages/google-api-servicecontrol/src/v1/quota_controller_client.ts @@ -411,7 +411,7 @@ export class QuotaControllerClient { options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = this._gaxModule.routingHeader.fromParams({ - service_name: request.serviceName || '', + service_name: request.serviceName ?? '', }); this.initialize(); return this.innerApiCalls.allocateQuota(request, options, callback); diff --git a/packages/google-api-servicecontrol/src/v1/service_controller_client.ts b/packages/google-api-servicecontrol/src/v1/service_controller_client.ts index 2f786e7d590..4f1a119584e 100644 --- a/packages/google-api-servicecontrol/src/v1/service_controller_client.ts +++ b/packages/google-api-servicecontrol/src/v1/service_controller_client.ts @@ -413,7 +413,7 @@ export class ServiceControllerClient { options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = this._gaxModule.routingHeader.fromParams({ - service_name: request.serviceName || '', + service_name: request.serviceName ?? '', }); this.initialize(); return this.innerApiCalls.check(request, options, callback); @@ -533,7 +533,7 @@ export class ServiceControllerClient { options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = this._gaxModule.routingHeader.fromParams({ - service_name: request.serviceName || '', + service_name: request.serviceName ?? '', }); this.initialize(); return this.innerApiCalls.report(request, options, callback); diff --git a/packages/google-api-servicecontrol/src/v2/service_controller_client.ts b/packages/google-api-servicecontrol/src/v2/service_controller_client.ts index f556caaaf6a..1be3fe2f883 100644 --- a/packages/google-api-servicecontrol/src/v2/service_controller_client.ts +++ b/packages/google-api-servicecontrol/src/v2/service_controller_client.ts @@ -424,7 +424,7 @@ export class ServiceControllerClient { options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = this._gaxModule.routingHeader.fromParams({ - service_name: request.serviceName || '', + service_name: request.serviceName ?? '', }); this.initialize(); return this.innerApiCalls.check(request, options, callback); @@ -537,7 +537,7 @@ export class ServiceControllerClient { options.otherArgs.headers = options.otherArgs.headers || {}; options.otherArgs.headers['x-goog-request-params'] = this._gaxModule.routingHeader.fromParams({ - service_name: request.serviceName || '', + service_name: request.serviceName ?? '', }); this.initialize(); return this.innerApiCalls.report(request, options, callback); diff --git a/packages/google-api-servicecontrol/test/gapic_quota_controller_v1.ts b/packages/google-api-servicecontrol/test/gapic_quota_controller_v1.ts index 8331b51b945..ff19682e854 100644 --- a/packages/google-api-servicecontrol/test/gapic_quota_controller_v1.ts +++ b/packages/google-api-servicecontrol/test/gapic_quota_controller_v1.ts @@ -25,6 +25,21 @@ import * as quotacontrollerModule from '../src'; import {protobuf} 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 @@ -159,26 +174,25 @@ describe('v1.QuotaControllerClient', () => { const request = generateSampleMessage( new protos.google.api.servicecontrol.v1.AllocateQuotaRequest() ); - request.serviceName = ''; - const expectedHeaderRequestParams = 'service_name='; - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const defaultValue1 = getTypeDefaultValue('AllocateQuotaRequest', [ + 'serviceName', + ]); + request.serviceName = defaultValue1; + const expectedHeaderRequestParams = `service_name=${defaultValue1}`; const expectedResponse = generateSampleMessage( new protos.google.api.servicecontrol.v1.AllocateQuotaResponse() ); client.innerApiCalls.allocateQuota = stubSimpleCall(expectedResponse); const [response] = await client.allocateQuota(request); assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.allocateQuota as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = ( + client.innerApiCalls.allocateQuota as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.allocateQuota as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes allocateQuota without error using callback', async () => { @@ -190,15 +204,11 @@ describe('v1.QuotaControllerClient', () => { const request = generateSampleMessage( new protos.google.api.servicecontrol.v1.AllocateQuotaRequest() ); - request.serviceName = ''; - const expectedHeaderRequestParams = 'service_name='; - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const defaultValue1 = getTypeDefaultValue('AllocateQuotaRequest', [ + 'serviceName', + ]); + request.serviceName = defaultValue1; + const expectedHeaderRequestParams = `service_name=${defaultValue1}`; const expectedResponse = generateSampleMessage( new protos.google.api.servicecontrol.v1.AllocateQuotaResponse() ); @@ -221,11 +231,14 @@ describe('v1.QuotaControllerClient', () => { }); const response = await promise; assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.allocateQuota as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions /*, callback defined above */) - ); + const actualRequest = ( + client.innerApiCalls.allocateQuota as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.allocateQuota as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes allocateQuota with error', async () => { @@ -237,26 +250,25 @@ describe('v1.QuotaControllerClient', () => { const request = generateSampleMessage( new protos.google.api.servicecontrol.v1.AllocateQuotaRequest() ); - request.serviceName = ''; - const expectedHeaderRequestParams = 'service_name='; - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const defaultValue1 = getTypeDefaultValue('AllocateQuotaRequest', [ + 'serviceName', + ]); + request.serviceName = defaultValue1; + const expectedHeaderRequestParams = `service_name=${defaultValue1}`; const expectedError = new Error('expected'); client.innerApiCalls.allocateQuota = stubSimpleCall( undefined, expectedError ); await assert.rejects(client.allocateQuota(request), expectedError); - assert( - (client.innerApiCalls.allocateQuota as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = ( + client.innerApiCalls.allocateQuota as SinonStub + ).getCall(0).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.allocateQuota as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes allocateQuota with closed client', async () => { @@ -268,7 +280,10 @@ describe('v1.QuotaControllerClient', () => { const request = generateSampleMessage( new protos.google.api.servicecontrol.v1.AllocateQuotaRequest() ); - request.serviceName = ''; + const defaultValue1 = getTypeDefaultValue('AllocateQuotaRequest', [ + 'serviceName', + ]); + request.serviceName = defaultValue1; const expectedError = new Error('The client has already been closed.'); client.close(); await assert.rejects(client.allocateQuota(request), expectedError); diff --git a/packages/google-api-servicecontrol/test/gapic_service_controller_v1.ts b/packages/google-api-servicecontrol/test/gapic_service_controller_v1.ts index 32cfb72003e..253509c1fb8 100644 --- a/packages/google-api-servicecontrol/test/gapic_service_controller_v1.ts +++ b/packages/google-api-servicecontrol/test/gapic_service_controller_v1.ts @@ -25,6 +25,21 @@ import * as servicecontrollerModule from '../src'; import {protobuf} 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 @@ -159,26 +174,24 @@ describe('v1.ServiceControllerClient', () => { const request = generateSampleMessage( new protos.google.api.servicecontrol.v1.CheckRequest() ); - request.serviceName = ''; - const expectedHeaderRequestParams = 'service_name='; - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const defaultValue1 = getTypeDefaultValue('CheckRequest', [ + 'serviceName', + ]); + request.serviceName = defaultValue1; + const expectedHeaderRequestParams = `service_name=${defaultValue1}`; const expectedResponse = generateSampleMessage( new protos.google.api.servicecontrol.v1.CheckResponse() ); client.innerApiCalls.check = stubSimpleCall(expectedResponse); const [response] = await client.check(request); assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.check as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = (client.innerApiCalls.check as SinonStub).getCall(0) + .args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.check as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes check without error using callback', async () => { @@ -190,15 +203,11 @@ describe('v1.ServiceControllerClient', () => { const request = generateSampleMessage( new protos.google.api.servicecontrol.v1.CheckRequest() ); - request.serviceName = ''; - const expectedHeaderRequestParams = 'service_name='; - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const defaultValue1 = getTypeDefaultValue('CheckRequest', [ + 'serviceName', + ]); + request.serviceName = defaultValue1; + const expectedHeaderRequestParams = `service_name=${defaultValue1}`; const expectedResponse = generateSampleMessage( new protos.google.api.servicecontrol.v1.CheckResponse() ); @@ -220,11 +229,13 @@ describe('v1.ServiceControllerClient', () => { }); const response = await promise; assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.check as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions /*, callback defined above */) - ); + const actualRequest = (client.innerApiCalls.check as SinonStub).getCall(0) + .args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.check as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes check with error', async () => { @@ -236,23 +247,21 @@ describe('v1.ServiceControllerClient', () => { const request = generateSampleMessage( new protos.google.api.servicecontrol.v1.CheckRequest() ); - request.serviceName = ''; - const expectedHeaderRequestParams = 'service_name='; - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const defaultValue1 = getTypeDefaultValue('CheckRequest', [ + 'serviceName', + ]); + request.serviceName = defaultValue1; + const expectedHeaderRequestParams = `service_name=${defaultValue1}`; const expectedError = new Error('expected'); client.innerApiCalls.check = stubSimpleCall(undefined, expectedError); await assert.rejects(client.check(request), expectedError); - assert( - (client.innerApiCalls.check as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = (client.innerApiCalls.check as SinonStub).getCall(0) + .args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.check as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes check with closed client', async () => { @@ -264,7 +273,10 @@ describe('v1.ServiceControllerClient', () => { const request = generateSampleMessage( new protos.google.api.servicecontrol.v1.CheckRequest() ); - request.serviceName = ''; + const defaultValue1 = getTypeDefaultValue('CheckRequest', [ + 'serviceName', + ]); + request.serviceName = defaultValue1; const expectedError = new Error('The client has already been closed.'); client.close(); await assert.rejects(client.check(request), expectedError); @@ -281,26 +293,25 @@ describe('v1.ServiceControllerClient', () => { const request = generateSampleMessage( new protos.google.api.servicecontrol.v1.ReportRequest() ); - request.serviceName = ''; - const expectedHeaderRequestParams = 'service_name='; - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const defaultValue1 = getTypeDefaultValue('ReportRequest', [ + 'serviceName', + ]); + request.serviceName = defaultValue1; + const expectedHeaderRequestParams = `service_name=${defaultValue1}`; const expectedResponse = generateSampleMessage( new protos.google.api.servicecontrol.v1.ReportResponse() ); client.innerApiCalls.report = stubSimpleCall(expectedResponse); const [response] = await client.report(request); assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.report as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = (client.innerApiCalls.report as SinonStub).getCall( + 0 + ).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.report as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes report without error using callback', async () => { @@ -312,15 +323,11 @@ describe('v1.ServiceControllerClient', () => { const request = generateSampleMessage( new protos.google.api.servicecontrol.v1.ReportRequest() ); - request.serviceName = ''; - const expectedHeaderRequestParams = 'service_name='; - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const defaultValue1 = getTypeDefaultValue('ReportRequest', [ + 'serviceName', + ]); + request.serviceName = defaultValue1; + const expectedHeaderRequestParams = `service_name=${defaultValue1}`; const expectedResponse = generateSampleMessage( new protos.google.api.servicecontrol.v1.ReportResponse() ); @@ -343,11 +350,14 @@ describe('v1.ServiceControllerClient', () => { }); const response = await promise; assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.report as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions /*, callback defined above */) - ); + const actualRequest = (client.innerApiCalls.report as SinonStub).getCall( + 0 + ).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.report as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes report with error', async () => { @@ -359,23 +369,22 @@ describe('v1.ServiceControllerClient', () => { const request = generateSampleMessage( new protos.google.api.servicecontrol.v1.ReportRequest() ); - request.serviceName = ''; - const expectedHeaderRequestParams = 'service_name='; - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const defaultValue1 = getTypeDefaultValue('ReportRequest', [ + 'serviceName', + ]); + request.serviceName = defaultValue1; + const expectedHeaderRequestParams = `service_name=${defaultValue1}`; const expectedError = new Error('expected'); client.innerApiCalls.report = stubSimpleCall(undefined, expectedError); await assert.rejects(client.report(request), expectedError); - assert( - (client.innerApiCalls.report as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = (client.innerApiCalls.report as SinonStub).getCall( + 0 + ).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.report as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes report with closed client', async () => { @@ -387,7 +396,10 @@ describe('v1.ServiceControllerClient', () => { const request = generateSampleMessage( new protos.google.api.servicecontrol.v1.ReportRequest() ); - request.serviceName = ''; + const defaultValue1 = getTypeDefaultValue('ReportRequest', [ + 'serviceName', + ]); + request.serviceName = defaultValue1; const expectedError = new Error('The client has already been closed.'); client.close(); await assert.rejects(client.report(request), expectedError); diff --git a/packages/google-api-servicecontrol/test/gapic_service_controller_v2.ts b/packages/google-api-servicecontrol/test/gapic_service_controller_v2.ts index 3c212e0cea6..f3abd813dbf 100644 --- a/packages/google-api-servicecontrol/test/gapic_service_controller_v2.ts +++ b/packages/google-api-servicecontrol/test/gapic_service_controller_v2.ts @@ -25,6 +25,21 @@ import * as servicecontrollerModule from '../src'; import {protobuf} 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 @@ -159,26 +174,24 @@ describe('v2.ServiceControllerClient', () => { const request = generateSampleMessage( new protos.google.api.servicecontrol.v2.CheckRequest() ); - request.serviceName = ''; - const expectedHeaderRequestParams = 'service_name='; - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const defaultValue1 = getTypeDefaultValue('CheckRequest', [ + 'serviceName', + ]); + request.serviceName = defaultValue1; + const expectedHeaderRequestParams = `service_name=${defaultValue1}`; const expectedResponse = generateSampleMessage( new protos.google.api.servicecontrol.v2.CheckResponse() ); client.innerApiCalls.check = stubSimpleCall(expectedResponse); const [response] = await client.check(request); assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.check as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = (client.innerApiCalls.check as SinonStub).getCall(0) + .args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.check as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes check without error using callback', async () => { @@ -190,15 +203,11 @@ describe('v2.ServiceControllerClient', () => { const request = generateSampleMessage( new protos.google.api.servicecontrol.v2.CheckRequest() ); - request.serviceName = ''; - const expectedHeaderRequestParams = 'service_name='; - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const defaultValue1 = getTypeDefaultValue('CheckRequest', [ + 'serviceName', + ]); + request.serviceName = defaultValue1; + const expectedHeaderRequestParams = `service_name=${defaultValue1}`; const expectedResponse = generateSampleMessage( new protos.google.api.servicecontrol.v2.CheckResponse() ); @@ -220,11 +229,13 @@ describe('v2.ServiceControllerClient', () => { }); const response = await promise; assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.check as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions /*, callback defined above */) - ); + const actualRequest = (client.innerApiCalls.check as SinonStub).getCall(0) + .args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.check as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes check with error', async () => { @@ -236,23 +247,21 @@ describe('v2.ServiceControllerClient', () => { const request = generateSampleMessage( new protos.google.api.servicecontrol.v2.CheckRequest() ); - request.serviceName = ''; - const expectedHeaderRequestParams = 'service_name='; - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const defaultValue1 = getTypeDefaultValue('CheckRequest', [ + 'serviceName', + ]); + request.serviceName = defaultValue1; + const expectedHeaderRequestParams = `service_name=${defaultValue1}`; const expectedError = new Error('expected'); client.innerApiCalls.check = stubSimpleCall(undefined, expectedError); await assert.rejects(client.check(request), expectedError); - assert( - (client.innerApiCalls.check as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = (client.innerApiCalls.check as SinonStub).getCall(0) + .args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.check as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes check with closed client', async () => { @@ -264,7 +273,10 @@ describe('v2.ServiceControllerClient', () => { const request = generateSampleMessage( new protos.google.api.servicecontrol.v2.CheckRequest() ); - request.serviceName = ''; + const defaultValue1 = getTypeDefaultValue('CheckRequest', [ + 'serviceName', + ]); + request.serviceName = defaultValue1; const expectedError = new Error('The client has already been closed.'); client.close(); await assert.rejects(client.check(request), expectedError); @@ -281,26 +293,25 @@ describe('v2.ServiceControllerClient', () => { const request = generateSampleMessage( new protos.google.api.servicecontrol.v2.ReportRequest() ); - request.serviceName = ''; - const expectedHeaderRequestParams = 'service_name='; - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const defaultValue1 = getTypeDefaultValue('ReportRequest', [ + 'serviceName', + ]); + request.serviceName = defaultValue1; + const expectedHeaderRequestParams = `service_name=${defaultValue1}`; const expectedResponse = generateSampleMessage( new protos.google.api.servicecontrol.v2.ReportResponse() ); client.innerApiCalls.report = stubSimpleCall(expectedResponse); const [response] = await client.report(request); assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.report as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = (client.innerApiCalls.report as SinonStub).getCall( + 0 + ).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.report as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes report without error using callback', async () => { @@ -312,15 +323,11 @@ describe('v2.ServiceControllerClient', () => { const request = generateSampleMessage( new protos.google.api.servicecontrol.v2.ReportRequest() ); - request.serviceName = ''; - const expectedHeaderRequestParams = 'service_name='; - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const defaultValue1 = getTypeDefaultValue('ReportRequest', [ + 'serviceName', + ]); + request.serviceName = defaultValue1; + const expectedHeaderRequestParams = `service_name=${defaultValue1}`; const expectedResponse = generateSampleMessage( new protos.google.api.servicecontrol.v2.ReportResponse() ); @@ -343,11 +350,14 @@ describe('v2.ServiceControllerClient', () => { }); const response = await promise; assert.deepStrictEqual(response, expectedResponse); - assert( - (client.innerApiCalls.report as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions /*, callback defined above */) - ); + const actualRequest = (client.innerApiCalls.report as SinonStub).getCall( + 0 + ).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.report as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes report with error', async () => { @@ -359,23 +369,22 @@ describe('v2.ServiceControllerClient', () => { const request = generateSampleMessage( new protos.google.api.servicecontrol.v2.ReportRequest() ); - request.serviceName = ''; - const expectedHeaderRequestParams = 'service_name='; - const expectedOptions = { - otherArgs: { - headers: { - 'x-goog-request-params': expectedHeaderRequestParams, - }, - }, - }; + const defaultValue1 = getTypeDefaultValue('ReportRequest', [ + 'serviceName', + ]); + request.serviceName = defaultValue1; + const expectedHeaderRequestParams = `service_name=${defaultValue1}`; const expectedError = new Error('expected'); client.innerApiCalls.report = stubSimpleCall(undefined, expectedError); await assert.rejects(client.report(request), expectedError); - assert( - (client.innerApiCalls.report as SinonStub) - .getCall(0) - .calledWith(request, expectedOptions, undefined) - ); + const actualRequest = (client.innerApiCalls.report as SinonStub).getCall( + 0 + ).args[0]; + assert.deepStrictEqual(actualRequest, request); + const actualHeaderRequestParams = ( + client.innerApiCalls.report as SinonStub + ).getCall(0).args[1].otherArgs.headers['x-goog-request-params']; + assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams)); }); it('invokes report with closed client', async () => { @@ -387,7 +396,10 @@ describe('v2.ServiceControllerClient', () => { const request = generateSampleMessage( new protos.google.api.servicecontrol.v2.ReportRequest() ); - request.serviceName = ''; + const defaultValue1 = getTypeDefaultValue('ReportRequest', [ + 'serviceName', + ]); + request.serviceName = defaultValue1; const expectedError = new Error('The client has already been closed.'); client.close(); await assert.rejects(client.report(request), expectedError);