Skip to content

Commit

Permalink
fix: add logic to handle non json response from smapi (#171)
Browse files Browse the repository at this point in the history
  • Loading branch information
kakhaUrigashvili authored May 27, 2020
1 parent 11ccb61 commit aa55fc2
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 4 deletions.
22 changes: 20 additions & 2 deletions lib/commands/smapi/smapi-command-handler.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,24 @@ const _mapToParams = (optionsValues, flatParamsMap, commanderToApiCustomizationM

const _sdkFunctionName = (swaggerApiOperationName) => `call${swaggerApiOperationName.charAt(0).toUpperCase() + swaggerApiOperationName.slice(1)}`;

/**
* Parses response from smapi
* @param {Object} response object
*/
const parseSmapiResponse = (response) => {
let result = '';
const { body, headers } = response;
const contentType = headers.find((h) => h.key === 'content-type');
// json if no content type or content type is application/json
const isJson = !contentType || contentType.value.includes('application/json');
if (body && Object.keys(body).length) {
result = isJson ? jsonView.toString(body) : body;
} else {
result = 'Command executed successfully!';
}
return result;
};

/**
* Handles smapi command request
* @param {string} swaggerApiOperationName Swagger operation name.
Expand Down Expand Up @@ -137,10 +155,10 @@ const smapiCommandHandler = (swaggerApiOperationName, flatParamsMap, commanderTo
} else if (inputOpts.fullResponse) {
result = jsonView.toString({ body, headers, statusCode });
} else {
result = body && Object.keys(body).length ? jsonView.toString(body) : 'Command executed successfully!';
result = parseSmapiResponse(response);
}
return result;
});
};

module.exports = smapiCommandHandler;
module.exports = { smapiCommandHandler, parseSmapiResponse };
2 changes: 1 addition & 1 deletion lib/commands/smapi/smapi-commander.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const { ModelIntrospector } = require('ask-smapi-sdk');
const { kebabCase } = require('@src/utils/string-utils');
const { CliCustomizationProcessor } = require('@src/commands/smapi/cli-customization-processor');
const optionModel = require('@src/commands/option-model.json');
const smapiCommandHandler = require('@src/commands/smapi/smapi-command-handler');
const { smapiCommandHandler } = require('@src/commands/smapi/smapi-command-handler');
const aliases = require('@src/commands/smapi/customizations/aliases.json');
const { apiToCommanderMap } = require('@src/commands/smapi/customizations/parameters-map');

Expand Down
30 changes: 29 additions & 1 deletion test/unit/commands/smapi/smapi-command-handler-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const jsonView = require('@src/view/json-view');
const BeforeSendProcessor = require('@src/commands/smapi/before-send-processor');
const AuthorizationController = require('@src/controllers/authorization-controller');
const profileHelper = require('@src/utils/profile-helper');
const smapiCommandHandler = require('@src/commands/smapi/smapi-command-handler');
const { smapiCommandHandler, parseSmapiResponse } = require('@src/commands/smapi/smapi-command-handler');


describe('Smapi test - smapiCommandHandler function', () => {
Expand Down Expand Up @@ -182,3 +182,31 @@ describe('Smapi test - smapiCommandHandler function', () => {
sinon.restore();
});
});

describe('Smapi test - parseSmapiResponse function', () => {
it('| should parse text/csv response', () => {
const content = 'foo bar\n foo';
const response = { headers: [{ key: 'content-type', value: 'text/csv' }], body: content };

const result = parseSmapiResponse(response);

expect(result).eql(content);
});

it('| should parse application/json response', () => {
const content = { foo: 'bar' };
const response = { headers: [{ key: 'content-type', value: 'application/json' }], body: content };

const result = parseSmapiResponse(response);

expect(result).eql(jsonView.toString(content));
});

it('| should return command executed successfully if not response body', () => {
const response = { headers: [] };

const result = parseSmapiResponse(response);

expect(result).eql('Command executed successfully!');
});
});

0 comments on commit aa55fc2

Please sign in to comment.