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

Commit 132966c

Browse files
committed
test generation for xml mediatype converting body to json to check schema
1 parent 3f30165 commit 132966c

File tree

30 files changed

+3757
-24
lines changed

30 files changed

+3757
-24
lines changed

index.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -244,10 +244,14 @@ function testGenResponse(swagger, apiPath, operation, response, config, consume,
244244

245245
// get the data
246246
data = getData(swagger, apiPath, operation, response, config, info);
247-
if (helpers.mediaTypeContainsJson(produce) && !data.noSchema) {
247+
if ((helpers.mediaTypeContainsJson(produce) || helpers.mediaTypeContainsXml(produce)) && !data.noSchema) {
248248
info.importValidator = true;
249249
}
250250

251+
if (helpers.mediaTypeContainsXml(produce) && !data.noSchema) {
252+
info.importXml2js = true;
253+
}
254+
251255
if (info.security && info.security.length !== 0) {
252256
info.importEnv = true;
253257
}
@@ -438,6 +442,7 @@ function testGenPath(swagger, apiPath, config) {
438442
host: (swagger.host !== undefined ? swagger.host : 'localhost:10010'),
439443
tests: result,
440444
importValidator: info.importValidator,
445+
importXml2js: info.importXml2js,
441446
importEnv: info.importEnv,
442447
importArete: info.importArete
443448
};
@@ -559,6 +564,8 @@ handlebars.registerHelper('printJSON', helpers.printJSON);
559564
handlebars.registerHelper('requestDataParamFormatter', helpers.requestDataParamFormatter);
560565
handlebars.registerHelper('isJsonRepresentation', helpers.isJsonRepresentation);
561566
handlebars.registerHelper('isJsonMediaType', helpers.isJsonMediaType);
567+
handlebars.registerHelper('isXmlMediaType', helpers.isXmlMediaType);
568+
handlebars.registerHelper('isNecessaryBody', helpers.isNecessaryBody);
562569

563570

564571
module.exports = {

lib/helpers.js

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,10 @@ module.exports = {
1717
requestDataParamFormatter : requestDataParamFormatter,
1818
isJsonRepresentation : isJsonRepresentation,
1919
isJsonMediaType : isJsonMediaType,
20-
mediaTypeContainsJson : mediaTypeContainsJson
20+
isXmlMediaType : isXmlMediaType,
21+
isNecessaryBody : isNecessaryBody,
22+
mediaTypeContainsJson : mediaTypeContainsJson,
23+
mediaTypeContainsXml : mediaTypeContainsXml
2124
};
2225

2326
function setLen(descriptionLength) {
@@ -62,7 +65,7 @@ function validateResponse(type, noSchema,
6265
'needs 2 parameters');
6366
}
6467

65-
if (!noSchema && mediaTypeContainsJson(type)) {
68+
if (!noSchema && (mediaTypeContainsJson(type) || mediaTypeContainsXml(type))) {
6669
return options.fn(this);
6770
} else {
6871
return options.inverse(this);
@@ -77,6 +80,23 @@ function isJsonMediaType(type, options) {
7780
return mediaTypeContainsJson(type) ? options.fn(this) : options.inverse(this);
7881
}
7982

83+
/**
84+
* mustache helper method to determine if a mediaType is JSON
85+
* @param {string} type content type to be evaluated
86+
*/
87+
function isXmlMediaType(type, options) {
88+
return mediaTypeContainsXml(type) ? options.fn(this) : options.inverse(this);
89+
}
90+
91+
/**
92+
* mustache helper method to determine if a mediaType is JSON
93+
* @param {string} type content type to be evaluated
94+
* @param {boolean} noSchema whether or not there is a defined schema
95+
*/
96+
function isNecessaryBody(type, noSchema, options) {
97+
return noSchema || !mediaTypeContainsXml(type) ? options.fn(this) : options.inverse(this);
98+
}
99+
80100
/**
81101
* decides if this request/response has a JSON representation
82102
* @param {string} contentType the media type of the request
@@ -94,6 +114,14 @@ function mediaTypeContainsJson(type) {
94114
return /\bjson\b/i.test(type);
95115
}
96116

117+
/**
118+
* determines if the mediatype is xml
119+
* @param {string} type content type to be evaluated
120+
*/
121+
function mediaTypeContainsXml(type) {
122+
return /\bxml\b/i.test(type);
123+
}
124+
97125
/**
98126
* replaces path params with obvious indicator for filling values
99127
* (i.e. if any part of the path is surrounded in curly braces {})
@@ -224,11 +252,11 @@ function requestDataParamFormatter(paramName, paramType, requestParameters){
224252
var delimiter = "'";
225253
if (['integer', 'number', 'boolean', 'null'].indexOf(paramType.toLowerCase()) > -1) {
226254
delimiter = '';
227-
}
228-
255+
}
256+
229257
if (typeof requestParameters[paramName] != 'undefined') {
230258
return delimiter + requestParameters[paramName] + delimiter;
231259
}
232260

233261
return "'DATA GOES HERE'";
234-
}
262+
}

0 commit comments

Comments
 (0)