From 3d8f568e3911d3d17811e37c5e701db0508a251e Mon Sep 17 00:00:00 2001 From: Matthew Keezer Date: Fri, 1 May 2020 17:16:02 -0500 Subject: [PATCH] fix: Strip trailing slashes in sendRequest (#92) --- lib/request-wrapper.ts | 4 +++- test/unit/request-wrapper.test.js | 24 ++++++++++++++++++++++++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/lib/request-wrapper.ts b/lib/request-wrapper.ts index 2f396a1de..476bf9416 100644 --- a/lib/request-wrapper.ts +++ b/lib/request-wrapper.ts @@ -21,7 +21,7 @@ import FormData = require('form-data'); import https = require('https'); import querystring = require('querystring'); import { PassThrough as readableStream } from 'stream'; -import { buildRequestFileObject, getMissingParams, isEmptyObject, isFileData, isFileWithMetadata } from './helper'; +import { buildRequestFileObject, getMissingParams, isEmptyObject, isFileData, isFileWithMetadata, stripTrailingSlash } from './helper'; import logger from './logger'; const isBrowser = typeof window === 'object'; @@ -180,6 +180,8 @@ export class RequestWrapper { url = serviceUrl + url; } + url = stripTrailingSlash(url); + let data = body; if (form) { diff --git a/test/unit/request-wrapper.test.js b/test/unit/request-wrapper.test.js index 3d3238411..1fcbee242 100644 --- a/test/unit/request-wrapper.test.js +++ b/test/unit/request-wrapper.test.js @@ -98,6 +98,30 @@ describe('sendRequest', () => { done(); }); + it('sendRequest should strip trailing slashes', async done => { + const parameters = { + defaultOptions: { + body: 'post=body', + formData: '', + qs: {}, + method: 'POST', + url: 'https://example.ibm.com/', + headers: { + 'test-header': 'test-header-value', + }, + responseType: 'buffer', + }, + }; + + mockAxiosInstance.mockResolvedValue(axiosResolveValue); + + const res = await requestWrapperInstance.sendRequest(parameters); + // assert results + expect(mockAxiosInstance.mock.calls[0][0].url).toEqual('https://example.ibm.com'); + expect(res).toEqual(expectedResult); + done(); + }); + it('should call formatError if request failed', async done => { const parameters = { defaultOptions: {