Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
fix: preserve default values in x-goog-request-params header (#411)
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] committed Sep 14, 2022
1 parent 06b70f7 commit 61c49e2
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 44 deletions.
2 changes: 1 addition & 1 deletion src/v1beta1/phishing_protection_service_v1_beta1_client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,7 @@ export class PhishingProtectionServiceV1Beta1Client {
options.otherArgs.headers = options.otherArgs.headers || {};
options.otherArgs.headers['x-goog-request-params'] =
this._gaxModule.routingHeader.fromParams({
parent: request.parent || '',
parent: request.parent ?? '',
});
this.initialize();
return this.innerApiCalls.reportPhishing(request, options, callback);
Expand Down
101 changes: 58 additions & 43 deletions test/gapic_phishing_protection_service_v1_beta1_v1beta1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@ import * as phishingprotectionservicev1beta1Module 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 @@ -191,26 +206,25 @@ describe('v1beta1.PhishingProtectionServiceV1Beta1Client', () => {
const request = generateSampleMessage(
new protos.google.cloud.phishingprotection.v1beta1.ReportPhishingRequest()
);
request.parent = '';
const expectedHeaderRequestParams = 'parent=';
const expectedOptions = {
otherArgs: {
headers: {
'x-goog-request-params': expectedHeaderRequestParams,
},
},
};
const defaultValue1 = getTypeDefaultValue('ReportPhishingRequest', [
'parent',
]);
request.parent = defaultValue1;
const expectedHeaderRequestParams = `parent=${defaultValue1}`;
const expectedResponse = generateSampleMessage(
new protos.google.cloud.phishingprotection.v1beta1.ReportPhishingResponse()
);
client.innerApiCalls.reportPhishing = stubSimpleCall(expectedResponse);
const [response] = await client.reportPhishing(request);
assert.deepStrictEqual(response, expectedResponse);
assert(
(client.innerApiCalls.reportPhishing as SinonStub)
.getCall(0)
.calledWith(request, expectedOptions, undefined)
);
const actualRequest = (
client.innerApiCalls.reportPhishing as SinonStub
).getCall(0).args[0];
assert.deepStrictEqual(actualRequest, request);
const actualHeaderRequestParams = (
client.innerApiCalls.reportPhishing as SinonStub
).getCall(0).args[1].otherArgs.headers['x-goog-request-params'];
assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams));
});

it('invokes reportPhishing without error using callback', async () => {
Expand All @@ -225,15 +239,11 @@ describe('v1beta1.PhishingProtectionServiceV1Beta1Client', () => {
const request = generateSampleMessage(
new protos.google.cloud.phishingprotection.v1beta1.ReportPhishingRequest()
);
request.parent = '';
const expectedHeaderRequestParams = 'parent=';
const expectedOptions = {
otherArgs: {
headers: {
'x-goog-request-params': expectedHeaderRequestParams,
},
},
};
const defaultValue1 = getTypeDefaultValue('ReportPhishingRequest', [
'parent',
]);
request.parent = defaultValue1;
const expectedHeaderRequestParams = `parent=${defaultValue1}`;
const expectedResponse = generateSampleMessage(
new protos.google.cloud.phishingprotection.v1beta1.ReportPhishingResponse()
);
Expand All @@ -256,11 +266,14 @@ describe('v1beta1.PhishingProtectionServiceV1Beta1Client', () => {
});
const response = await promise;
assert.deepStrictEqual(response, expectedResponse);
assert(
(client.innerApiCalls.reportPhishing as SinonStub)
.getCall(0)
.calledWith(request, expectedOptions /*, callback defined above */)
);
const actualRequest = (
client.innerApiCalls.reportPhishing as SinonStub
).getCall(0).args[0];
assert.deepStrictEqual(actualRequest, request);
const actualHeaderRequestParams = (
client.innerApiCalls.reportPhishing as SinonStub
).getCall(0).args[1].otherArgs.headers['x-goog-request-params'];
assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams));
});

it('invokes reportPhishing with error', async () => {
Expand All @@ -275,26 +288,25 @@ describe('v1beta1.PhishingProtectionServiceV1Beta1Client', () => {
const request = generateSampleMessage(
new protos.google.cloud.phishingprotection.v1beta1.ReportPhishingRequest()
);
request.parent = '';
const expectedHeaderRequestParams = 'parent=';
const expectedOptions = {
otherArgs: {
headers: {
'x-goog-request-params': expectedHeaderRequestParams,
},
},
};
const defaultValue1 = getTypeDefaultValue('ReportPhishingRequest', [
'parent',
]);
request.parent = defaultValue1;
const expectedHeaderRequestParams = `parent=${defaultValue1}`;
const expectedError = new Error('expected');
client.innerApiCalls.reportPhishing = stubSimpleCall(
undefined,
expectedError
);
await assert.rejects(client.reportPhishing(request), expectedError);
assert(
(client.innerApiCalls.reportPhishing as SinonStub)
.getCall(0)
.calledWith(request, expectedOptions, undefined)
);
const actualRequest = (
client.innerApiCalls.reportPhishing as SinonStub
).getCall(0).args[0];
assert.deepStrictEqual(actualRequest, request);
const actualHeaderRequestParams = (
client.innerApiCalls.reportPhishing as SinonStub
).getCall(0).args[1].otherArgs.headers['x-goog-request-params'];
assert(actualHeaderRequestParams.includes(expectedHeaderRequestParams));
});

it('invokes reportPhishing with closed client', async () => {
Expand All @@ -309,7 +321,10 @@ describe('v1beta1.PhishingProtectionServiceV1Beta1Client', () => {
const request = generateSampleMessage(
new protos.google.cloud.phishingprotection.v1beta1.ReportPhishingRequest()
);
request.parent = '';
const defaultValue1 = getTypeDefaultValue('ReportPhishingRequest', [
'parent',
]);
request.parent = defaultValue1;
const expectedError = new Error('The client has already been closed.');
client.close();
await assert.rejects(client.reportPhishing(request), expectedError);
Expand Down

0 comments on commit 61c49e2

Please sign in to comment.