diff --git a/samples/webmasters/query.js b/samples/webmasters/query.js new file mode 100644 index 00000000000..dc97bed9052 --- /dev/null +++ b/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 +}; diff --git a/src/lib/apirequest.ts b/src/lib/apirequest.ts index 4ed21e87c63..1322ec6ef85 100644 --- a/src/lib/apirequest.ts +++ b/src/lib/apirequest.ts @@ -119,7 +119,11 @@ export function createAPIRequest( // 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); diff --git a/test/samples/test.samples.sheets.ts b/test/samples/test.samples.sheets.ts index bd1b2e2662e..44fd98635d8 100644 --- a/test/samples/test.samples.sheets.ts +++ b/test/samples/test.samples.sheets.ts @@ -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(); diff --git a/test/samples/test.samples.webmasters.ts b/test/samples/test.samples.webmasters.ts new file mode 100644 index 00000000000..0faed14e238 --- /dev/null +++ b/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(); + }); + }); +}); diff --git a/test/test.path.ts b/test/test.path.ts index d3eda0f44a4..66f730417cd 100644 --- a/test/test.path.ts +++ b/test/test.path.ts @@ -153,8 +153,8 @@ 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) => { @@ -162,7 +162,7 @@ describe('Path params', () => { 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) => { @@ -170,7 +170,7 @@ describe('Path params', () => { return done(err2); } const parm = Utils.getPath(res).split('/').pop(); - assert.equal(parm, 'p@ram'); + assert.equal(decodeURIComponent(parm!), 'p@ram'); done(); }); });