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

feat: request gzip responses #1065

Merged
merged 1 commit into from
Mar 14, 2018
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
6 changes: 6 additions & 0 deletions src/lib/apirequest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ import {SchemaParameters} from './schema';

const maxContentLength = Math.pow(2, 31);

// tslint:disable-next-line no-var-requires
const pkg = require('../../../package.json');
const USER_AGENT = `google-api-nodejs-client/${pkg.version} (gzip)`;

function isReadableStream(obj: stream.Readable|string) {
return obj instanceof stream.Readable && typeof obj._read === 'function';
}
Expand Down Expand Up @@ -205,6 +209,8 @@ export function createAPIRequest<T>(
// to 10MB. Setting to 2GB by default.
// https://github.com/google/google-api-nodejs-client/issues/991
options.maxContentLength = options.maxContentLength || maxContentLength;
options.headers['Accept-Encoding'] = 'gzip';

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

options.headers['User-Agent'] = USER_AGENT;

// By default Axios treats any 2xx as valid, and all non 2xx status
// codes as errors. This is a problem for HTTP 304s when used along
Expand Down
1 change: 0 additions & 1 deletion test/test.auth.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,6 @@ describe('OAuth2 client', () => {

nock(Utils.baseUrl).get('/drive/v2/files/wat').reply(200);
await pify(localDrive.files.get)({fileId: 'wat', auth: oauth2client});
console.log(`Creds: ${JSON.stringify(oauth2client.credentials)}`);
assert.equal(JSON.stringify(oauth2client.credentials), JSON.stringify({
access_token: 'abc123',
refresh_token: 'abc',
Expand Down
17 changes: 17 additions & 0 deletions test/test.transporters.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,18 @@ async function testContentType(drive: APIEndpoint) {
assert(res.request.headers['content-type'].indexOf('application/json') === 0);
}

async function testGzip(drive: APIEndpoint) {
nock(Utils.baseUrl)
.get(
'/drive/v2/files', undefined,
{reqheaders: {'Accept-Encoding': 'gzip'}})
.reply(200, {});
const res = await pify(drive.files.list)();
assert.deepEqual(res.data, {});
// note: axios strips the `content-encoding` header from the response,
// so that cannot be checked here.
}

async function testBody(drive: APIEndpoint) {
nock(Utils.baseUrl).get('/drive/v2/files').reply(200);
const res = await pify(drive.files.list)();
Expand Down Expand Up @@ -117,6 +129,11 @@ describe('Transporters', () => {
await testContentType(remoteDrive);
});

it('should add the proper gzip headers', async () => {
await testGzip(localDrive);
await testGzip(remoteDrive);
});

it('should not add body for GET requests', async () => {
await testBody(localDrive);
await testBody(remoteDrive);
Expand Down