Skip to content

Commit 6f04739

Browse files
Mike Kistlerdpopp07
Mike Kistler
authored andcommitted
fix: Move check for serviceUrl to createRequest (#47)
1 parent c5556de commit 6f04739

File tree

4 files changed

+45
-37
lines changed

4 files changed

+45
-37
lines changed

lib/base_service.ts

+10-2
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ export class BaseService {
129129
*
130130
* @param {Object} parameters - service request options passed in by user.
131131
* @param {string} parameters.options.method - the http method.
132-
* @param {string} parameters.options.url - the URL of the service.
132+
* @param {string} parameters.options.url - the path portion of the URL to be appended to the serviceUrl
133133
* @param {string} parameters.options.path - the path to be appended to the service URL.
134134
* @param {string} parameters.options.qs - the querystring to be included in the URL.
135135
* @param {string} parameters.options.body - the data to be sent as the request body.
@@ -140,11 +140,19 @@ export class BaseService {
140140
* - object: the value is converted to a JSON string before insertion into the form body
141141
* - NodeJS.ReadableStream|Buffer|FileWithMetadata: sent as a file, with any associated metadata
142142
* - array: each element of the array is sent as a separate form part using any special processing as described above
143-
* @param {HeaderOptions} parameters.options.headers - additional headers to be passed on the request.
143+
* @param {Object} parameters.defaultOptions
144+
* @param {string} parameters.defaultOptions.serviceUrl - the base URL of the service
145+
* @param {HeaderOptions} parameters.defaultOptions.headers - additional headers to be passed on the request.
144146
* @param {Function} callback - callback function to pass the response back to
145147
* @returns {ReadableStream|undefined}
146148
*/
147149
protected createRequest(parameters, callback) {
150+
// validate serviceUrl parameter has been set
151+
const serviceUrl = parameters.defaultOptions && parameters.defaultOptions.serviceUrl;
152+
if (!serviceUrl || typeof serviceUrl !== 'string') {
153+
return callback(new Error('The service URL is required'), null);
154+
}
155+
148156
this.authenticator.authenticate(parameters.defaultOptions, err => {
149157
err ? callback(err) : this.requestWrapperInstance.sendRequest(parameters, callback);
150158
});

lib/requestwrapper.ts

-8
Original file line numberDiff line numberDiff line change
@@ -125,14 +125,6 @@ export class RequestWrapper {
125125
const { path, body, form, formData, qs, method, serviceUrl } = options;
126126
let { headers, url } = options;
127127

128-
// validate service url parameter has been set
129-
if (!serviceUrl || typeof serviceUrl !== 'string') {
130-
return _callback(new Error('The service URL is required'), null);
131-
}
132-
133-
// use default service url if `url` parameter is not specified in generated method
134-
url = url || serviceUrl;
135-
136128
const multipartForm = new FormData();
137129

138130
// Form params

test/unit/base-service.test.js

+34-1
Original file line numberDiff line numberDiff line change
@@ -262,6 +262,33 @@ describe('Base Service', () => {
262262
expect(testService.requestWrapperInstance.sendRequest).toBe(sendRequestMock); // verify it is calling the instance
263263
});
264264

265+
it('should call callback with an error if `serviceUrl` is not set', done => {
266+
const testService = new TestService({
267+
authenticator: AUTHENTICATOR,
268+
});
269+
testService.setServiceUrl(undefined);
270+
271+
const parameters = {
272+
defaultOptions: {
273+
body: 'post=body',
274+
formData: '',
275+
qs: {},
276+
method: 'POST',
277+
headers: {
278+
'test-header': 'test-header-value',
279+
},
280+
responseType: 'buffer',
281+
},
282+
};
283+
284+
testService.createRequest(parameters, (err, res) => {
285+
// assert results
286+
expect(err).toBeInstanceOf(Error);
287+
expect(res).toBeNull();
288+
done();
289+
});
290+
});
291+
265292
it('should send error back to user on authenticate() failure', done => {
266293
const testService = new TestService({
267294
authenticator: AUTHENTICATOR,
@@ -272,7 +299,13 @@ describe('Base Service', () => {
272299
const fakeError = new Error('token request failed');
273300
authenticateMock.mockImplementation((_, callback) => callback(fakeError));
274301

275-
testService.createRequest(EMPTY_OBJECT, err => {
302+
const parameters = {
303+
defaultOptions: {
304+
serviceUrl: 'https://foo.bar.baz/api',
305+
},
306+
};
307+
308+
testService.createRequest(parameters, err => {
276309
expect(err).toBe(fakeError);
277310
expect(authenticateMock).toHaveBeenCalled();
278311
done();

test/unit/requestWrapper.test.js

+1-26
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ describe('sendRequest', () => {
5959
formData: '',
6060
qs: {},
6161
method: 'POST',
62-
serviceUrl:
62+
url:
6363
'https://example.ibm.com/v1/environments/environment-id/configurations/configuration-id',
6464
headers: {
6565
'test-header': 'test-header-value',
@@ -326,31 +326,6 @@ describe('sendRequest', () => {
326326
});
327327
});
328328

329-
it('should call callback with an error if `serviceUrl` is not set', done => {
330-
const parameters = {
331-
defaultOptions: {
332-
body: 'post=body',
333-
formData: '',
334-
qs: {},
335-
method: 'POST',
336-
headers: {
337-
'test-header': 'test-header-value',
338-
},
339-
responseType: 'buffer',
340-
},
341-
};
342-
343-
mockAxiosInstance.mockResolvedValue(axiosResolveValue);
344-
345-
requestWrapperInstance.sendRequest(parameters, (err, res) => {
346-
// assert results
347-
expect(err).toBeInstanceOf(Error);
348-
expect(res).toBeNull();
349-
expect(mockAxiosInstance).not.toHaveBeenCalled();
350-
done();
351-
});
352-
});
353-
354329
// Need to rewrite this to test instantiation with userOptions
355330

356331
// it('should keep parameters in options that are not explicitly set in requestwrapper', done => {

0 commit comments

Comments
 (0)