Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: add test for x-goog-user-project header #1889

Merged
merged 4 commits into from
Dec 9, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"client_id": "764086051850-6qr4p6gpi6hn506pt8ejuq83di341hur.apps.googleusercontent.com",
"client_secret": "privatekey",
"refresh_token": "refreshtoken",
"type": "authorized_user",
"project_id": "my-project",
"quota_project_id": "my-quota-project"
}
45 changes: 42 additions & 3 deletions test/test.transporters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,69 @@
import * as assert from 'assert';
import {APIEndpoint} from 'googleapis-common';
import * as nock from 'nock';
import {GoogleApis} from '../src';
import {AuthPlus} from '../src/googleapis';
import {GoogleApis, google} from '../src';
import {Utils} from './utils';

async function testHeaders(drive: APIEndpoint) {
const req = nock(Utils.baseUrl)
.post('/drive/v2/files/a/comments')
.reply(200, function() {
const headers = this.req.headers;
// ensure that the x-goog-user-project is loaded from default credentials:
assert.strictEqual(headers['x-goog-user-project'][0], 'my-quota-project');

// ensure that the x-goog-api-client header is populated by
// googleapis-common:
const headers = this.req.headers['x-goog-api-client'];
assert.ok(
/gdcl\/[0-9]+\.[\w-.]+ gl-node\/[0-9]+\.[\w-.]+ auth\/[0-9]+\.[\w-.]+/.test(
headers
headers['x-goog-api-client'][0]
)
);
});
const auth = getAuthClientMock();
const res = await drive.comments.insert({
fileId: 'a',
headers: {'If-None-Match': '12345'},
auth: await auth.getClient(),
});
req.done();
auth.done();
assert.strictEqual(res.config.headers['If-None-Match'], '12345');
}

// Returns an auth client that fakes loading application default credentials
// from a fixtures directory:
function getAuthClientMock() {
// mock environment variables such that default credentials are loaded.
const projectOriginal = process.env.GCLOUD_PROJECT;
process.env.GCLOUD_PROJECT = 'my-fake-project';
const homeOriginal = process.env.HOME;
process.env.HOME = './test/fixtures/';
const appdataOriginal = process.env.APPDATA;
process.env.APPDATA = './test/fixtures/.config';

// an attempt will be made to fetch access token on first request.
const req = nock('https://oauth2.googleapis.com')
.post('/token')
.reply(200, {});

// An "AuthPlus" client with an added "done" method for resetting mocks.
class AuthMock extends AuthPlus {
done() {
process.env.GCLOUD_PROJECT = projectOriginal;
process.env.HOME = homeOriginal;
process.env.APPDATA = appdataOriginal;
req.done();
}
}
const auth = new AuthMock({
// Scopes can be specified either as an array or as a single, space-delimited string.
scopes: 'https://www.googleapis.com/auth/drive',
});
return auth;
}

async function testContentType(drive: APIEndpoint) {
nock(Utils.baseUrl)
.post('/drive/v2/files/a/comments')
Expand Down