Skip to content

Commit

Permalink
test: adds integration test for x-goog-user-project header (#404)
Browse files Browse the repository at this point in the history
  • Loading branch information
bcoe authored Dec 12, 2019
1 parent dbe941c commit b74914e
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/google-cloud-translate/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,9 @@
"eslint-config-prettier": "^6.0.0",
"eslint-plugin-node": "^10.0.0",
"eslint-plugin-prettier": "^3.0.0",
"google-auth-library": "^5.7.0",
"gts": "^1.0.0",
"http2spy": "^1.1.0",
"intelli-espower-loader": "^1.0.1",
"jsdoc": "^3.6.2",
"jsdoc-fresh": "^1.0.1",
Expand Down
80 changes: 80 additions & 0 deletions packages/google-cloud-translate/system-test/translate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import * as assert from 'assert';
import {TranslationServiceClient} from '../src';

const http2spy = require('http2spy');
const API_KEY = process.env.TRANSLATE_API_KEY;

describe('translate', () => {
Expand Down Expand Up @@ -135,5 +136,84 @@ describe('translate', () => {
supportTarget: true,
});
});

it('should populate x-goog-user-project header, and succeed if valid project', async () => {
const {GoogleAuth} = require('google-auth-library');
const auth = new GoogleAuth({
credentials: Object.assign(
require(process.env.GOOGLE_APPLICATION_CREDENTIALS || ''),
{
quota_project_id: process.env.GCLOUD_PROJECT,
}
),
});
const {TranslationServiceClient} = http2spy.require(
require.resolve('../src')
);
const translate = new TranslationServiceClient({
auth,
});

// We run the same test as "list of supported languages", but with an
// alternate "quota_project_id" set; Given that GCLOUD_PROJECT
// references a valid project, we expect success:
const projectId = await translate.getProjectId();
// This should not hrow an exception:
await translate.getSupportedLanguages({
parent: `projects/${projectId}`,
});
// Ensure we actually populated the header:
assert.strictEqual(
process.env.GCLOUD_PROJECT,
http2spy.requests[http2spy.requests.length - 1][
'x-goog-user-project'
][0]
);
});

it('should populate x-goog-user-project header, and fail if invalid project', async () => {
const {GoogleAuth} = require('google-auth-library');
const auth = new GoogleAuth({
credentials: Object.assign(
require(process.env.GOOGLE_APPLICATION_CREDENTIALS || ''),
{
quota_project_id: 'my-fake-billing-project',
}
),
});
const {TranslationServiceClient} = http2spy.require(
require.resolve('../src')
);
const translate = new TranslationServiceClient({
auth,
});

// We set a quota project "my-fake-billing-project" that does not exist,
// this should result in an error.
let err: Error | null = null;
try {
const projectId = await translate.getProjectId();
const [result] = await translate.getSupportedLanguages({
parent: `projects/${projectId}`,
});
} catch (_err) {
err = _err;
}
assert(err);
assert(
err!.message.includes(
// make sure the error included our fake project name, we shouldn't
// be too specific about the error incase it changes upstream.
'my-fake-billing-project'
),
err!.message
);
assert.strictEqual(
'my-fake-billing-project',
http2spy.requests[http2spy.requests.length - 1][
'x-goog-user-project'
][0]
);
});
});
});

0 comments on commit b74914e

Please sign in to comment.