Fixed issue where content-type with wild characters resulted in mismacthes for valid collection headrs

This commit is contained in:
Vishal Shingala
2022-01-19 12:53:15 +05:30
parent ce49559ef2
commit 989cea9ce6
4 changed files with 150 additions and 19 deletions

View File

@@ -3689,15 +3689,19 @@ module.exports = {
_.forEach(_.keys(contentObj), (contentType) => {
let contentMediaType = this.parseMediaType(contentType);
mediaTypes.push(contentMediaType.type + '/' + contentMediaType.subtype);
mediaTypes.push({
type: contentMediaType.type,
subtype: contentMediaType.subtype,
contentType: contentMediaType.type + '/' + contentMediaType.subtype
});
});
// prefer JSON > XML > Other media types for suggested header.
_.forEach(mediaTypes, (mediaType) => {
let headerFamily = this.getHeaderFamily(mediaType);
let headerFamily = this.getHeaderFamily(mediaType.contentType);
if (headerFamily !== HEADER_TYPE.INVALID) {
suggestedContentHeader = mediaType;
suggestedContentHeader = mediaType.contentType;
hasComputedType = true;
if (headerFamily === HEADER_TYPE.JSON) {
return false;
@@ -3707,7 +3711,7 @@ module.exports = {
// if no JSON or XML, take whatever we have
if (!hasComputedType && mediaTypes.length > 0) {
suggestedContentHeader = mediaTypes[0];
suggestedContentHeader = mediaTypes[0].contentType;
hasComputedType = true;
}
@@ -3763,8 +3767,23 @@ module.exports = {
}
// Invalid type of header found
else if (!_.isEmpty(contentHeader) && !_.includes(mediaTypes, contentHeaderMediaType)) {
let mismatchObj = {
else if (!_.isEmpty(contentHeader)) {
let mismatchObj,
matched = false;
// wildcard header macthing
_.forEach(mediaTypes, (mediaType) => {
let transactionHeader = _.split(contentHeaderMediaType, '/'),
headerTypeMatched = (mediaType.type === '*' || mediaType.type === transactionHeader[0]),
headerSubtypeMatched = (mediaType.subtype === '*' || mediaType.subtype === transactionHeader[1]);
if (headerTypeMatched && headerSubtypeMatched) {
matched = true;
}
});
if (!matched) {
mismatchObj = {
property: mismatchProperty,
transactionJsonPath: transactionPathPrefix + `[${contentHeaderIndex}].value`,
schemaJsonPath: schemaPathPrefix,
@@ -3782,6 +3801,7 @@ module.exports = {
}
mismatches.push(mismatchObj);
}
}
return mismatches;
},

View File

@@ -77,6 +77,71 @@
}
],
"event": []
},
{
"id": "6067a189-11f4-4098-b87c-d1365d9ceb9b",
"name": "Update all pets",
"request": {
"name": "Update all pets",
"description": {},
"url": {
"path": [
"pets"
],
"host": [
"{{baseUrl}}"
],
"query": [],
"variable": []
},
"header": [
{
"key": "Content-Type",
"value": "application/json"
}
],
"method": "PUT",
"auth": null,
"body": {
"mode": "raw",
"raw": "{\n \"id\": 33231879,\n \"name\": \"d\",\n \"tag\": \"proident ullamco\"\n}"
}
},
"response": [
{
"id": "6bf06974-2393-43de-8a80-1d682b3cf4f7",
"name": "content with all allowed types",
"originalRequest": {
"url": {
"path": [
"pets"
],
"host": [
"{{baseUrl}}"
],
"query": [],
"variable": []
},
"method": "PUT",
"body": {
"mode": "raw",
"raw": "{\n \"id\": 33231879,\n \"name\": \"d\",\n \"tag\": \"proident ullamco\"\n}"
}
},
"status": "OK",
"code": 200,
"header": [
{
"key": "Content-Type",
"value": "text/xml"
}
],
"body": "",
"cookie": [],
"_postman_previewlanguage": "text"
}
],
"event": []
}
],
"event": []

View File

@@ -25,6 +25,20 @@ paths:
'application/json; charset=utf-8':
schema:
$ref: "#/components/schemas/Pet"
put:
summary: Update all pets
requestBody:
content:
application/*:
schema:
$ref: "#/components/schemas/Pet"
responses:
'200':
description: content with all allowed types
content:
'*/*':
schema:
$ref: "#/components/schemas/Pet"
components:
schemas:
Pet:

View File

@@ -556,6 +556,38 @@ describe('VALIDATE FUNCTION TESTS ', function () {
});
});
it('Should correctly match and validate content type headers having wildcard characters' +
' with collection req/res body', function (done) {
let differentContentTypesSpec = fs.readFileSync(path.join(__dirname, VALIDATION_DATA_FOLDER_PATH +
'/differentContentTypesSpec.yaml'), 'utf-8'),
differentContentTypesCollection = fs.readFileSync(path.join(__dirname, VALIDATION_DATA_FOLDER_PATH +
'/differentContentTypesCollection.json'), 'utf-8'),
resultObj,
historyRequest = [],
options = {
showMissingInSchemaErrors: true,
suggestAvailableFixes: true
},
schemaPack = new Converter.SchemaPack({ type: 'string', data: differentContentTypesSpec }, options);
getAllTransactions(JSON.parse(differentContentTypesCollection), historyRequest);
schemaPack.validateTransaction(historyRequest, (err, result) => {
expect(err).to.be.null;
expect(result).to.be.an('object');
resultObj = result.requests[historyRequest[1].id].endpoints[0];
/**
* Both req and res body should have matched content types
*/
expect(resultObj.matched).to.eql(true);
expect(resultObj.mismatches).to.have.lengthOf(0);
expect(resultObj.responses[_.keys(resultObj.responses)[0]].matched).to.eql(true);
expect(resultObj.responses[_.keys(resultObj.responses)[0]].mismatches).to.have.lengthOf(0);
done();
});
});
it('Should be able to validate and suggest correct value for body with primitive data type', function (done) {
let primitiveDataTypeBodySpec = fs.readFileSync(path.join(__dirname, VALIDATION_DATA_FOLDER_PATH +
'/primitiveDataTypeBodySpec.yaml'), 'utf-8'),