Skip to content
This repository has been archived by the owner on May 15, 2023. It is now read-only.

Added input testing #107

Closed
wants to merge 14 commits into from
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
wrap-iife: 2
one-var: [2, "never"]
quote-props: [2, "as-needed"]
max-len: [2, 80, 2]
max-len: [2, 160, 2]
yoda: [2, "never"]
dot-notation: 2
new-cap: 2
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ stt.testGen(swagger, config);
* **`testModule`** *required*: One of `supertest` or `request`. Choose between direct API calls (`request`) vs. programatic access to your API (`supertest`).
* **`pathNames`** *required*: List of path names available in your Swagger API spec used to generate tests for. Empty array leads to **all paths**.
* **`loadTest`** *optional*: List of objects info in your Swagger API spec used to generate stress tests. If specify, pathName & operation are **required**. Optional fields requests defaults to `1000`, concurrent defaults to `100`.
* **`maxLen`** *optional*: Maximum line length. Defaults to `80`.
* **`maxLen`** *optional*: Maximum line length. If set to `-1`, descriptions will not be truncated. Defaults to `80`.
* **`pathParams`** *optional*: Object containing the values of a specific path parameters.

#### Return value
Expand Down
160 changes: 34 additions & 126 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ var handlebars = require('handlebars');
var sanitize = require('sanitize-filename');
var read = require('fs').readFileSync;
var _ = require('lodash');
var strObj = require('string');
var join = require('path').join;
var deref = require('json-schema-deref-sync');
var len;
var helpers = require('./lib/helpers.js');

/**
* To check if it is an empty array or undefined
Expand Down Expand Up @@ -88,6 +87,19 @@ function getData(swagger, path, operation, response, config, info) {
data.pathParams = config.pathParams;
}

// used for checking inputTesting table
var tempPath = (((swagger.basePath !== undefined) &&
(swagger.basePath !== '/'))
? swagger.basePath : '') + path;

// get inputTesting from config if defined for this path:operation:response
if (config.inputTesting &&
Copy link
Contributor

@Remco75 Remco75 Sep 14, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wouldn't it make more sense to users to call the config entry something like 'request-data' instead of inputTesting?

config.inputTesting[tempPath] &&
config.inputTesting[tempPath][operation] &&
config.inputTesting[tempPath][operation][response]) {
data.inputTesting = config.inputTesting[tempPath][operation][response];
}

// cope with loadTest info
if (info.loadTest != null) {
_.forEach(info.loadTest, function(loadTestParam) {
Expand Down Expand Up @@ -239,7 +251,17 @@ function testGenResponse(swagger, path, operation, response, config,

source = read(templatePath, 'utf8');
templateFn = handlebars.compile(source, {noEscape: true});
result = templateFn(data);

if (data.inputTesting && data.inputTesting.length > 0) {
result = '';
for (var i = 0; i < data.inputTesting.length; i++) {
data.inputs = data.inputTesting[i];
result += templateFn(data);
}
} else {
result = templateFn(data);
}

return result;
}

Expand Down Expand Up @@ -431,10 +453,10 @@ function testGen(swagger, config) {
handlebars.registerPartial('schema-partial', schemaTemp);
source = read(join(__dirname, '/templates/environment.handlebars'), 'utf8');
environment = handlebars.compile(source, {noEscape: true});
len = 80;
helpers.len = 80;

if (config.maxLen && !isNaN(config.maxLen)) {
len = config.maxLen;
helpers.len = config.maxLen;
}

if (!targets || targets.length === 0) {
Expand Down Expand Up @@ -505,127 +527,13 @@ function testGen(swagger, config) {
return output;
}

handlebars.registerHelper('is', helpers.is);
handlebars.registerHelper('ifCond', helpers.ifCond);
handlebars.registerHelper('validateResponse', helpers.validateResponse);
handlebars.registerHelper('length', helpers.length);
handlebars.registerHelper('pathify', helpers.pathify);
handlebars.registerHelper('printJSON', helpers.printJSON);

module.exports = {
testGen: testGen
};

// http://goo.gl/LFoiYG
handlebars.registerHelper('is', function(lvalue, rvalue, options) {
if (arguments.length < 3) {
throw new Error('Handlebars Helper \'is\' needs 2 parameters');
}

if (lvalue !== rvalue) {
return options.inverse(this);
} else {
return options.fn(this);
}
});

// http://goo.gl/LFoiYG
handlebars.registerHelper('ifCond', function(v1, v2, options) {
if (arguments.length < 3) {
throw new Error('Handlebars Helper \'ifCond\' needs 2 parameters');
}
if (v1.length > 0 || v2) {
return options.fn(this);
}
return options.inverse(this);
});

/**
* determines if content types are able to be validated
* @param {string} type content type to be evaluated
* @param {boolean} noSchema whether or not there is a defined schema
* @param {Object} options handlebars built-in options
* @returns {boolean} whether or not the content can be validated
*/
handlebars.registerHelper('validateResponse', function(type, noSchema,
options) {
if (arguments.length < 3) {
throw new Error('Handlebars Helper \'validateResponse\'' +
'needs 2 parameters');
}

if (!noSchema && type === TYPE_JSON) {
return options.fn(this);
} else {
return options.inverse(this);
}
});

/**
* replaces path params with obvious indicator for filling values
* (i.e. if any part of the path is surrounded in curly braces {})
* @param {string} path request path to be pathified
* @param {object} pathParams contains path parameters to replace with
* @returns {string} pathified string
*/
handlebars.registerHelper('pathify', function(path, pathParams) {
var r;

if (arguments.length < 3) {
throw new Error('Handlebars Helper \'pathify\'' +
' needs 2 parameters');
}

if ((typeof path) !== 'string') {
throw new TypeError('Handlebars Helper \'pathify\'' +
'requires path to be a string');
}

if ((typeof pathParams) !== 'object') {
throw new TypeError('Handlebars Helper \'pathify\'' +
'requires pathParams to be an object');
}

if (Object.keys(pathParams).length > 0) {
var re = new RegExp(/(?:\{+)(.*?(?=\}))(?:\}+)/g);
var re2;
var matches = [];
var m = re.exec(path);
var i;

while (m) {
matches.push(m[1]);
m = re.exec(path);
}

for (i = 0; i < matches.length; i++) {
var match = matches[i];

re2 = new RegExp('(\\{+)' + match + '(?=\\})(\\}+)');

if (typeof (pathParams[match]) !== 'undefined' &&
pathParams[match] !== null) {
// console.log("Match found for "+match+": "+pathParams[match]);
path = path.replace(re2, pathParams[match]);
} else {
// console.log("No match found for "+match+": "+pathParams[match]);
path = path.replace(re2, '{' + match + ' PARAM GOES HERE}');
}
}
return path;
}

r = new RegExp(/(?:\{+)(.*?(?=\}))(?:\}+)/g);
return path.replace(r, '{$1 PARAM GOES HERE}');
});

/**
* split the long description into multiple lines
* @param {string} description request description to be splitted
* @returns {string} multiple lines
*/
handlebars.registerHelper('length', function(description) {
if (arguments.length < 2) {
throw new Error('Handlebar Helper \'length\'' +
' needs 1 parameter');
}

if ((typeof description) !== 'string') {
throw new TypeError('Handlebars Helper \'length\'' +
'requires path to be a string');
}
return strObj(description).truncate(len - 50).s;
});
Loading