Skip to content

Commit

Permalink
fix: preserve default values in x-goog-request-params header (#156)
Browse files Browse the repository at this point in the history
- [ ] Regenerate this pull request now.

PiperOrigin-RevId: 474338479

Source-Link: googleapis/googleapis@d5d35e0

Source-Link: googleapis/googleapis-gen@efcd3f9
Copy-Tag: eyJwIjoiLmdpdGh1Yi8uT3dsQm90LnlhbWwiLCJoIjoiZWZjZDNmOTM5NjJhMTAzZjY4ZjAwM2UyYTFlZWNkZTZmYTIxNmEyNyJ9
  • Loading branch information
gcf-owl-bot[bot] authored Sep 14, 2022
1 parent ac079a7 commit 4216c6d
Show file tree
Hide file tree
Showing 6 changed files with 259 additions and 220 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down
101 changes: 58 additions & 43 deletions packages/google-api-servicecontrol/test/gapic_quota_controller_v1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T extends object>(instance: T) {
const filledObject = (
instance.constructor as typeof protobuf.Message
Expand Down Expand Up @@ -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 () => {
Expand All @@ -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()
);
Expand All @@ -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 () => {
Expand All @@ -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 () => {
Expand All @@ -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);
Expand Down
Loading

0 comments on commit 4216c6d

Please sign in to comment.