From 7c42fccf9486201fcb29d41e6472eab52c2fc953 Mon Sep 17 00:00:00 2001 From: childish-sambino Date: Thu, 25 Jun 2020 11:28:37 -0500 Subject: [PATCH] fix: encode URL path params (#94) When referencing a resource by a unique name with non-safe characters, it needs to be URI-encoded or the request will fail. --- src/services/open-api-client.js | 1 + test/services/twilio-api/twilio-client.test.js | 16 ++++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/src/services/open-api-client.js b/src/services/open-api-client.js index ca00e849..3f7023f3 100644 --- a/src/services/open-api-client.js +++ b/src/services/open-api-client.js @@ -83,6 +83,7 @@ class OpenApiClient { if (doesObjectHaveProperty(opts.pathParams, pathNode)) { value = opts.pathParams[pathNode]; + value = encodeURIComponent(value); } logger.debug(`pathNode=${pathNode}, value=${value}`); diff --git a/test/services/twilio-api/twilio-client.test.js b/test/services/twilio-api/twilio-client.test.js index a0e27ac4..f5d4d0b4 100644 --- a/test/services/twilio-api/twilio-client.test.js +++ b/test/services/twilio-api/twilio-client.test.js @@ -232,6 +232,22 @@ describe('services', () => { expect(httpClient.lastRequest.data).to.eql(qs.stringify({ EmergencyEnabled: 'true' })); }); + test + .nock('https://api.twilio.com', api => { + api.get(`/2010-04-01/Accounts/${accountSid}/Calls/hey%23there%3F.json`).reply(200, { + status: 'in-progress' + }); + }) + .it('encodes non-safe path param characters', async () => { + const response = await client.fetch({ + domain: 'api', + path: '/2010-04-01/Accounts/{AccountSid}/Calls/{Sid}.json', + pathParams: { Sid: 'hey#there?' } + }); + + expect(response).to.eql({ status: 'in-progress' }); + }); + /* eslint-disable max-nested-callbacks */ describe('getLimit', () => { test.it('gets the limit', () => {