diff --git a/lib/specValidator.js b/lib/specValidator.js index 5fbe4945..a090c87b 100644 --- a/lib/specValidator.js +++ b/lib/specValidator.js @@ -555,6 +555,10 @@ class SpecValidator { } } + if (!(options.headers['content-type'] || options.headers['Content-Type'])) { + options.headers['Content-Type'] = utils.getJsonContentType(operation.consumes); + } + let request = null; let validationResult = {}; if (!foundIssues) { @@ -687,7 +691,7 @@ class SpecValidator { } //ensure content-type header is present if (!(exampleResponseHeaders['content-type'] || exampleResponseHeaders['Content-Type'])) { - exampleResponseHeaders['content-type'] = operation.produces[0]; + exampleResponseHeaders['content-type'] = utils.getJsonContentType(operation.produces); } let exampleResponse = new ResponseWrapper(exampleResponseStatusCode, exampleResponseBody, exampleResponseHeaders); let validationResult = self.validateResponse(operation, exampleResponse); diff --git a/lib/util/utils.js b/lib/util/utils.js index b98460b3..8910e8b6 100644 --- a/lib/util/utils.js +++ b/lib/util/utils.js @@ -360,4 +360,21 @@ exports.removeObject = function removeObject(doc, ptr) { log.error(err); } return result; -}; \ No newline at end of file +}; + +/* + * Finds the first content-type that contains "/json". Only supported Content-Types are + * "text/json" & "application/json" so we perform first best match that contains '/json' + * @param {Array} Array of content-types. + * + * @param {string} content-type that contains "/json". + */ +exports.getJsonContentType = function getJsonContentType(consumesOrProduces) { + var firstMatchedJson = null; + if (consumesOrProduces) { + firstMatchedJson = consumesOrProduces.find((contentType) => { + return (contentType.match(/.*\/json.*/ig) !== null); + }); + } + return firstMatchedJson; +};