Skip to content

Commit

Permalink
fix: Strip trailing slashes in sendRequest (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
rmkeezer committed May 1, 2020
1 parent f3f7ad9 commit 3d8f568
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
4 changes: 3 additions & 1 deletion lib/request-wrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -180,6 +180,8 @@ export class RequestWrapper {
url = serviceUrl + url;
}

url = stripTrailingSlash(url);

let data = body;

if (form) {
Expand Down
24 changes: 24 additions & 0 deletions test/unit/request-wrapper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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: {
Expand Down

0 comments on commit 3d8f568

Please sign in to comment.