Skip to content
This repository was archived by the owner on May 15, 2023. It is now read-only.
Open
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
25 changes: 23 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,16 @@ function isEmpty(val) {
return val == null || val.length <= 0;
}

/**
* To get xml2js's config as a string
* @private
* @param {object} xml2jsConfig a xml2js's config
* @returns {string} return xml2js's config as a string
*/
function getStringXml2jsConfig(xml2jsConfig) {
return JSON.stringify(xml2jsConfig || {}).replace(/"(.+)":/, '$1: ').replace('"', '\'');
}

/**
* Populate property of the swagger project
* @private
Expand Down Expand Up @@ -89,6 +99,8 @@ function getData(swagger, apiPath, operation, response, config, info) {
data.pathParams = config.pathParams;
}

data.xml2jsConfig = getStringXml2jsConfig(config.xml2js);

// used for checking requestData table
var requestPath = (swagger.basePath) ? path.posix.join(swagger.basePath, apiPath) : apiPath;

Expand Down Expand Up @@ -244,10 +256,14 @@ function testGenResponse(swagger, apiPath, operation, response, config, consume,

// get the data
data = getData(swagger, apiPath, operation, response, config, info);
if (helpers.mediaTypeContainsJson(produce) && !data.noSchema) {
if ((helpers.mediaTypeContainsJson(produce) || helpers.mediaTypeContainsXml(produce)) && !data.noSchema) {
info.importValidator = true;
}

if (helpers.mediaTypeContainsXml(produce) && !data.noSchema) {
info.importXml2js = true;
}

if (info.security && info.security.length !== 0) {
info.importEnv = true;
}
Expand Down Expand Up @@ -438,6 +454,7 @@ function testGenPath(swagger, apiPath, config) {
host: (swagger.host !== undefined ? swagger.host : 'localhost:10010'),
tests: result,
importValidator: info.importValidator,
importXml2js: info.importXml2js,
importEnv: info.importEnv,
importArete: info.importArete
};
Expand Down Expand Up @@ -559,7 +576,11 @@ handlebars.registerHelper('printJSON', helpers.printJSON);
handlebars.registerHelper('requestDataParamFormatter', helpers.requestDataParamFormatter);
handlebars.registerHelper('isJsonRepresentation', helpers.isJsonRepresentation);
handlebars.registerHelper('isJsonMediaType', helpers.isJsonMediaType);

handlebars.registerHelper('isXmlMediaType', helpers.isXmlMediaType);
handlebars.registerHelper('isHtmlMediaType', helpers.isHtmlMediaType);
handlebars.registerHelper('isPdfMediaType', helpers.isPdfMediaType);
handlebars.registerHelper('isNecessaryBody', helpers.isNecessaryBody);
handlebars.registerHelper('hasWriteParameter', helpers.hasWriteParameter);

module.exports = {
testGen: testGen
Expand Down
79 changes: 74 additions & 5 deletions lib/helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,14 @@ module.exports = {
requestDataParamFormatter : requestDataParamFormatter,
isJsonRepresentation : isJsonRepresentation,
isJsonMediaType : isJsonMediaType,
mediaTypeContainsJson : mediaTypeContainsJson
isXmlMediaType : isXmlMediaType,
isPdfMediaType : isPdfMediaType,
isHtmlMediaType : isHtmlMediaType,
isNecessaryBody : isNecessaryBody,
mediaTypeContainsJson : mediaTypeContainsJson,
mediaTypeContainsPdf : mediaTypeContainsPdf,
mediaTypeContainsXml : mediaTypeContainsXml,
hasWriteParameter : hasWriteParameter
};

function setLen(descriptionLength) {
Expand Down Expand Up @@ -62,7 +69,7 @@ function validateResponse(type, noSchema,
'needs 2 parameters');
}

if (!noSchema && mediaTypeContainsJson(type)) {
if (!noSchema && (mediaTypeContainsJson(type) || mediaTypeContainsXml(type))) {
return options.fn(this);
} else {
return options.inverse(this);
Expand All @@ -77,6 +84,40 @@ function isJsonMediaType(type, options) {
return mediaTypeContainsJson(type) ? options.fn(this) : options.inverse(this);
}

/**
* mustache helper method to determine if a mediaType is XML
* @param {string} type content type to be evaluated
*/
function isXmlMediaType(type, options) {
return mediaTypeContainsXml(type) ? options.fn(this) : options.inverse(this);
}

/**
* mustache helper method to determine if a mediaType is PDF
* @param {string} type content type to be evaluated
*/
function isPdfMediaType(type, options) {
return mediaTypeContainsPdf(type) ? options.fn(this) : options.inverse(this);
}

/**
* mustache helper method to determine if a mediaType is PDF
* @param {string} type content type to be evaluated
*/
function isHtmlMediaType(type, options) {
return mediaTypeContainsHtml(type) ? options.fn(this) : options.inverse(this);
}

/**
* mustache helper method to determine if is necessary that function test generated
* have body parameter
* @param {string} type content type to be evaluated
* @param {boolean} noSchema whether or not there is a defined schema
*/
function isNecessaryBody(type, noSchema, options) {
return noSchema || !mediaTypeContainsXml(type) ? options.fn(this) : options.inverse(this);
}

/**
* decides if this request/response has a JSON representation
* @param {string} contentType the media type of the request
Expand All @@ -94,6 +135,30 @@ function mediaTypeContainsJson(type) {
return /\bjson\b/i.test(type);
}

/**
* determines if the mediatype is xml
* @param {string} type content type to be evaluated
*/
function mediaTypeContainsXml(type) {
return /\bxml\b/i.test(type);
}

/**
* determines if the mediatype is pdf
* @param {string} type content type to be evaluated
*/
function mediaTypeContainsPdf(type) {
return /\bpdf\b/i.test(type);
}

/**
* determines if the mediatype is pdf
* @param {string} type content type to be evaluated
*/
function mediaTypeContainsHtml(type) {
return /\bhtml\b/i.test(type);
}

/**
* replaces path params with obvious indicator for filling values
* (i.e. if any part of the path is surrounded in curly braces {})
Expand Down Expand Up @@ -224,11 +289,15 @@ function requestDataParamFormatter(paramName, paramType, requestParameters){
var delimiter = "'";
if (['integer', 'number', 'boolean', 'null'].indexOf(paramType.toLowerCase()) > -1) {
delimiter = '';
}
}

if (typeof requestParameters[paramName] != 'undefined') {
return delimiter + requestParameters[paramName] + delimiter;
}

return "'DATA GOES HERE'";
}
}

function hasWriteParameter(param, requestParameters, options){
return !requestParameters[param.name] && !param.required ? options.inverse(this) : options.fn(this)
}
Loading