From 1e42c881a9f1876ba46b3de69f72e02bc2ae2b3c Mon Sep 17 00:00:00 2001 From: Dhroov Gupta Date: Wed, 17 Jul 2019 14:42:19 +0530 Subject: [PATCH] made some changes --- lib/util.js | 25 ++++++------------------- test/unit/base.test.js | 4 ++-- 2 files changed, 8 insertions(+), 21 deletions(-) diff --git a/lib/util.js b/lib/util.js index 2f8b720..4366deb 100644 --- a/lib/util.js +++ b/lib/util.js @@ -13,7 +13,6 @@ const sdk = require('postman-collection'), URLENCODED = 'application/x-www-form-urlencoded', APP_JSON = 'application/json', APP_JS = 'application/javascript', - APP_XML = 'application/xml', TEXT_XML = 'text/xml', TEXT_PLAIN = 'text/plain', TEXT_HTML = 'text/html', @@ -114,7 +113,7 @@ module.exports = { * @param {string} cTypeHeader - the content type header string * @returns {string} format of content type header */ - getContentTypeHeader: function(cTypeHeader) { + getContentTypeHeaderFormat: function(cTypeHeader) { if (cTypeHeader.startsWith('application') && cTypeHeader.endsWith('json')) { return 'json'; } @@ -594,7 +593,7 @@ module.exports = { } for (let cType in contentObj) { - if (this.getContentTypeHeader(cType)) { + if (this.getContentTypeHeaderFormat(cType)) { cTypeHeader = cType; hasComputedType = true; } @@ -617,7 +616,7 @@ module.exports = { } responseBody = this.convertToPmBodyData(contentObj[cTypeHeader], cTypeHeader, this.options.indentCharacter); - if (this.getContentTypeHeader(cTypeHeader) === 'json') { + if (this.getContentTypeHeaderFormat(cTypeHeader) === 'json') { responseBody = JSON.stringify(responseBody, null, this.options.indentCharacter); } else if (typeof responseBody !== 'string') { @@ -730,7 +729,7 @@ module.exports = { bodyObj.schema = this.getRefObject(bodyObj.schema.$ref); } if (this.options.schemaFaker) { - if (this.getContentTypeHeader(contentType) === 'xml') { + if (this.getContentTypeHeaderFormat(contentType) === 'xml') { schemaType = SCHEMA_FORMATS.XML; } bodyData = safeSchemaFaker(bodyObj.schema || {}, this.components, schemaType, indentCharacter); @@ -1169,23 +1168,11 @@ module.exports = { if (responseBodyWrapper.contentTypeHeader) { // we could infer the content-type header from the body responseHeaders.push({ key: 'Content-Type', value: responseBodyWrapper.contentTypeHeader }); - if (this.getContentTypeHeader(responseBodyWrapper.contentTypeHeader) === 'json' && - responseBodyWrapper.contentTypeHeader.endsWith('json')) { - previewLanguage = 'json'; - } - else if (this.getContentTypeHeader(responseBodyWrapper.contentTypeHeader) === 'xml' && - responseBodyWrapper.contentTypeHeader.endsWith('xml')) { - previewLanguage = 'xml'; - } + previewLanguage = this.getContentTypeHeaderFormat(responseBodyWrapper.contentTypeHeader); } else if (response.content && Object.keys(response.content).length > 0) { responseHeaders.push({ key: 'Content-Type', value: Object.keys(response.content)[0] }); - if (Object.keys(response.content)[0] === APP_JSON) { - previewLanguage = 'json'; - } - else if (Object.keys(response.content)[0] === APP_XML) { - previewLanguage = 'xml'; - } + previewLanguage = this.getContentTypeHeaderFormat(Object.keys(response.content)[0]); } else { responseHeaders.push({ key: 'Content-Type', value: TEXT_PLAIN }); diff --git a/test/unit/base.test.js b/test/unit/base.test.js index 4eecad6..9d6f718 100644 --- a/test/unit/base.test.js +++ b/test/unit/base.test.js @@ -166,12 +166,12 @@ describe('INTERFACE FUNCTION TESTS ', function () { describe('Validate content type header function', function() { it('should checks for custom type JSON header', function() { - let result = utils.getContentTypeHeader('application/vnd.retailer+json'); + let result = utils.getContentTypeHeaderFormat('application/vnd.retailer+json'); expect(result).to.equal('json'); }); it('should checks for custom type xml header', function() { - let result = utils.getContentTypeHeader('application/vnd.retailer+xml'); + let result = utils.getContentTypeHeaderFormat('application/vnd.retailer+xml'); expect(result).to.equal('xml'); }); });