Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Move check for serviceUrl to createRequest #47

Merged
merged 1 commit into from
Sep 19, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/base_service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -129,7 +129,7 @@ export class BaseService {
*
* @param {Object} parameters - service request options passed in by user.
* @param {string} parameters.options.method - the http method.
* @param {string} parameters.options.url - the URL of the service.
* @param {string} parameters.options.url - the path portion of the URL to be appended to the serviceUrl
* @param {string} parameters.options.path - the path to be appended to the service URL.
* @param {string} parameters.options.qs - the querystring to be included in the URL.
* @param {string} parameters.options.body - the data to be sent as the request body.
Expand All @@ -140,11 +140,19 @@ export class BaseService {
* - object: the value is converted to a JSON string before insertion into the form body
* - NodeJS.ReadableStream|FileObject|Buffer|FileParamAttributes: sent as a file, with any associated metadata
* - array: each element of the array is sent as a separate form part using any special processing as described above
* @param {HeaderOptions} parameters.options.headers - additional headers to be passed on the request.
* @param {Object} parameters.defaultOptions
* @param {string} parameters.defaultOptions.serviceUrl - the base URL of the service
* @param {HeaderOptions} parameters.defaultOptions.headers - additional headers to be passed on the request.
* @param {Function} callback - callback function to pass the response back to
* @returns {ReadableStream|undefined}
*/
protected createRequest(parameters, callback) {
// validate serviceUrl parameter has been set
const serviceUrl = parameters.defaultOptions && parameters.defaultOptions.serviceUrl;
if (!serviceUrl || typeof serviceUrl !== 'string') {
return callback(new Error('The service URL is required'), null);
}

this.authenticator.authenticate(parameters.defaultOptions, err => {
err ? callback(err) : this.requestWrapperInstance.sendRequest(parameters, callback);
});
Expand Down
8 changes: 0 additions & 8 deletions lib/requestwrapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,14 +125,6 @@ export class RequestWrapper {
const { path, body, form, formData, qs, method, serviceUrl } = options;
let { headers, url } = options;

// validate service url parameter has been set
if (!serviceUrl || typeof serviceUrl !== 'string') {
return _callback(new Error('The service URL is required'), null);
}

// use default service url if `url` parameter is not specified in generated method
url = url || serviceUrl;

const multipartForm = new FormData();

// Form params
Expand Down
35 changes: 34 additions & 1 deletion test/unit/base-service.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,33 @@ describe('Base Service', () => {
expect(testService.requestWrapperInstance.sendRequest).toBe(sendRequestMock); // verify it is calling the instance
});

it('should call callback with an error if `serviceUrl` is not set', done => {
const testService = new TestService({
authenticator: AUTHENTICATOR,
});
testService.setServiceUrl(undefined);

const parameters = {
defaultOptions: {
body: 'post=body',
formData: '',
qs: {},
method: 'POST',
headers: {
'test-header': 'test-header-value',
},
responseType: 'buffer',
},
};

testService.createRequest(parameters, (err, res) => {
// assert results
expect(err).toBeInstanceOf(Error);
expect(res).toBeNull();
done();
});
});

it('should send error back to user on authenticate() failure', done => {
const testService = new TestService({
authenticator: AUTHENTICATOR,
Expand All @@ -272,7 +299,13 @@ describe('Base Service', () => {
const fakeError = new Error('token request failed');
authenticateMock.mockImplementation((_, callback) => callback(fakeError));

testService.createRequest(EMPTY_OBJECT, err => {
const parameters = {
defaultOptions: {
serviceUrl: 'https://foo.bar.baz/api',
},
};

testService.createRequest(parameters, err => {
expect(err).toBe(fakeError);
expect(authenticateMock).toHaveBeenCalled();
done();
Expand Down
27 changes: 1 addition & 26 deletions test/unit/requestWrapper.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('sendRequest', () => {
formData: '',
qs: {},
method: 'POST',
serviceUrl:
url:
'https://example.ibm.com/v1/environments/environment-id/configurations/configuration-id',
headers: {
'test-header': 'test-header-value',
Expand Down Expand Up @@ -326,31 +326,6 @@ describe('sendRequest', () => {
});
});

it('should call callback with an error if `serviceUrl` is not set', done => {
const parameters = {
defaultOptions: {
body: 'post=body',
formData: '',
qs: {},
method: 'POST',
headers: {
'test-header': 'test-header-value',
},
responseType: 'buffer',
},
};

mockAxiosInstance.mockResolvedValue(axiosResolveValue);

requestWrapperInstance.sendRequest(parameters, (err, res) => {
// assert results
expect(err).toBeInstanceOf(Error);
expect(res).toBeNull();
expect(mockAxiosInstance).not.toHaveBeenCalled();
done();
});
});

// Need to rewrite this to test instantiation with userOptions

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