Skip to content

Commit

Permalink
fix: encodeURIComponent path params (#1087)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinBeckwith committed Mar 27, 2018
1 parent a269cab commit d89a735
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 10 deletions.
57 changes: 57 additions & 0 deletions samples/webmasters/query.js
@@ -0,0 +1,57 @@
// Copyright 2016, Google, Inc.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

'use strict';

const {google} = require('googleapis');
const sampleClient = require('../sampleclient');

const webmasters = google.webmasters({
version: 'v3',
auth: sampleClient.oAuth2Client
});

function runSample (callback) {
webmasters.searchanalytics.query({
siteUrl: 'http://jbeckwith.com',
resource: {
startDate: '2018-01-01',
endDate: '2018-04-01'
}
}, (err, res) => {
if (err) {
console.error(err);
throw err;
}
console.log(res.data);
callback(res.data);
});
}

if (module === require.main) {
const scopes = [
'https://www.googleapis.com/auth/webmasters',
'https://www.googleapis.com/auth/webmasters.readonly'
];
sampleClient.authenticate(scopes, err => {
if (err) {
throw err;
}
runSample(() => { /* complete */ });
});
}

module.exports = {
runSample,
client: sampleClient.oAuth2Client
};
6 changes: 5 additions & 1 deletion src/lib/apirequest.ts
Expand Up @@ -119,7 +119,11 @@ export function createAPIRequest<T>(

// Parse urls
if (options.url) {
options.url = parseString(options.url, params);
const encodedParams = Object.assign(
{},
...Object.keys(params).map(
x => ({[x]: encodeURIComponent(params[x])})));
options.url = parseString(options.url, encodedParams);
}
if (parameters.mediaUrl) {
parameters.mediaUrl = parseString(parameters.mediaUrl, params);
Expand Down
11 changes: 6 additions & 5 deletions test/samples/test.samples.sheets.ts
Expand Up @@ -38,11 +38,12 @@ describe('sheets samples', () => {
});

it('should append values', done => {
const scope =
nock(baseUrl)
.post(
'/v4/spreadsheets/aSheetId/values/A1:A10:append?valueInputOption=USER_ENTERED')
.reply(200, {});
const range = 'A1:A10';
const scope = nock(baseUrl)
.post(`/v4/spreadsheets/aSheetId/values/${
encodeURIComponent(
range)}:append?valueInputOption=USER_ENTERED`)
.reply(200, {});
samples.append.runSample('aSheetId', 'A1:A10', (data: {}) => {
assert(data);
scope.done();
Expand Down
47 changes: 47 additions & 0 deletions test/samples/test.samples.webmasters.ts
@@ -0,0 +1,47 @@
// Copyright 2018, Google, LLC.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

import * as assert from 'assert';
import * as nock from 'nock';
import {Utils} from './../utils';

nock.disableNetConnect();

// tslint:disable: no-any
const samples: any = {
query: require('../../../samples/webmasters/query')
};

for (const p in samples) {
if (samples[p]) {
samples[p].client.credentials = {access_token: 'not-a-token'};
}
}

describe('webmaster samples', () => {
afterEach(() => {
nock.cleanAll();
});

it('should query analytics', done => {
const siteUrl = 'http://jbeckwith.com';
const path = `/webmasters/v3/sites/${
encodeURIComponent(siteUrl)}/searchAnalytics/query`;
const scope = nock(Utils.baseUrl).post(path).reply(200, {});
samples.query.runSample((data: {}) => {
assert(data);
scope.done();
done();
});
});
});
8 changes: 4 additions & 4 deletions test/test.path.ts
Expand Up @@ -153,24 +153,24 @@ describe('Path params', () => {
});
});

it('should not be urlencoded', (done) => {
const p = '/drive/v2/files/p@ram';
it('should be urlencoded', done => {
const p = `/drive/v2/files/${encodeURIComponent('p@ram')}`;
nock(Utils.baseUrl).get(p).reply(200);
localDrive.files.get(
{fileId: 'p@ram'}, (err: Error, res: AxiosResponse) => {
if (err) {
return done(err);
}
const parm = Utils.getPath(res).split('/').pop();
assert.equal(parm, 'p@ram');
assert.equal(decodeURIComponent(parm!), 'p@ram');
nock(Utils.baseUrl).get(p).reply(200);
remoteDrive.files.get(
{fileId: 'p@ram'}, (err2: Error, res2: AxiosResponse) => {
if (err2) {
return done(err2);
}
const parm = Utils.getPath(res).split('/').pop();
assert.equal(parm, 'p@ram');
assert.equal(decodeURIComponent(parm!), 'p@ram');
done();
});
});
Expand Down

1 comment on commit d89a735

@jamie-pate
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This breaks several api endpoints who's params include '/' characters.
See #1099

Please sign in to comment.