@apideck/portman
Version:
Port OpenAPI Spec to Postman Collection, with contract & variation tests included
164 lines • 9.95 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.testResponseHeaderContent = void 0;
var application_1 = require("../../application");
var testResponseHeaderContent = function (ResponseHeaderTests, pmOperation, config) {
ResponseHeaderTests.map(function (check) {
var _a;
var pmTestKey = '';
var pmTestValue = '';
var pmTestContains = '';
var pmTestOneOf = '';
var pmTestLength = '';
var pmTestMinLength = '';
var pmTestMaxLength = '';
var pmTestAssert = '';
var split = (_a = config === null || config === void 0 ? void 0 : config.separatorSymbol) !== null && _a !== void 0 ? _a : '::';
if (check.hasOwnProperty('key')) {
var negate = check.notExist === true ? 'not.have' : 'have';
var negateLabel = check.notExist === true ? 'does not exists' : 'is present';
pmTestKey = [
"// Validate if response header ".concat(negateLabel, " \n"),
"pm.test(\"[".concat(pmOperation.method.toUpperCase(), "]").concat(split).concat(pmOperation.path),
" - Response header ".concat(check.key, " ").concat(negateLabel, "\", function () {\n"),
" pm.response.to.".concat(negate, ".header(\"").concat(check.key, "\");\n"),
"});\n"
].join('');
}
if (check.hasOwnProperty('value')) {
var checkValue = check.value;
if (typeof check.value === 'string') {
checkValue = "\"".concat(check.value, "\"");
if (check.value.includes('{{') && check.value.includes('}}')) {
checkValue = "pm.collectionVariables.get(\"".concat(check.value.replace(/{{|}}/g, ''), "\")");
}
}
pmTestValue = [
"// Response header should have value \"".concat(check.value, "\" for \"").concat(check.key, "\"\n"),
"pm.test(\"[".concat(pmOperation.method.toUpperCase(), "]").concat(split).concat(pmOperation.path),
" - Content check if header value for '".concat(check.key, "' matches '").concat(check.value, "'\", function() {\n"),
" pm.expect(pm.response.headers.get(\"".concat(check.key, "\")).to.eql(").concat(checkValue, ");\n"),
"});\n"
].join('');
}
if (check.hasOwnProperty('contains')) {
var checkContains = check.contains;
if (typeof check.contains === 'string') {
checkContains = "\"".concat(check.contains, "\"");
if (check.contains.includes('{{') && check.contains.includes('}}')) {
checkContains = "pm.collectionVariables.get(\"".concat(check.contains.replace(/{{|}}/g, ''), "\")");
}
}
pmTestContains = [
"// Response header should contain value \"".concat(check.contains, "\" for \"").concat(check.key, "\"\n"),
"pm.test(\"[".concat(pmOperation.method.toUpperCase(), "]").concat(split).concat(pmOperation.path),
" - Content check if header value for '".concat(check.key, "' contains '").concat(check.contains, "'\", function() {\n"),
" pm.expect(pm.response.headers.get(\"".concat(check.key, "\")).to.include(").concat(checkContains, ");\n"),
"});\n"
].join('');
}
if (check.hasOwnProperty('oneOf')) {
if (Array.isArray(check.oneOf)) {
var safeOneOf = check.oneOf.map(function (item) {
if (typeof item === 'string') {
var checkOneOfItem = item;
if (checkOneOfItem.includes('{{') && checkOneOfItem.includes('}}')) {
checkOneOfItem = "pm.collectionVariables.get(\"".concat(checkOneOfItem.replace(/{{|}}/g, ''), "\")");
return checkOneOfItem;
}
return "\"".concat(checkOneOfItem, "\"");
}
return item;
});
pmTestOneOf = [
"// Response header should be one of the values \"".concat(check.oneOf, "\" for \"").concat(check.key, "\"\n"),
"pm.test(\"[".concat(pmOperation.method.toUpperCase(), "]").concat(split).concat(pmOperation.path),
" - Content check if header value for '".concat(check.key, "' is matching one of: '").concat(check.oneOf, "'\", function() {\n"),
" pm.expect(pm.response.headers.get(\"".concat(check.key, "\")).to.be.oneOf([").concat(safeOneOf, "]);\n"),
"});\n"
].join('');
}
}
if (check.hasOwnProperty('length')) {
var checkLength = check.length;
if (typeof check.length === 'string') {
checkLength = "\"".concat(check.length, "\"");
if (check.length.includes('{{') && check.length.includes('}}')) {
checkLength = "pm.collectionVariables.get(\"".concat(check.length.replace(/{{|}}/g, ''), "\")");
}
}
pmTestLength = [
"// Response header should have a length of \"".concat(check.length, "\" for \"").concat(check.key, "\"\n"),
"pm.test(\"[".concat(pmOperation.method.toUpperCase(), "]").concat(split).concat(pmOperation.path),
" - Content check if header value of '".concat(check.key, "' has a length of '").concat(check.length, "'\", function() {\n"),
" pm.expect(pm.response.headers.get(\"".concat(check.key, "\")).to.have.lengthOf(").concat(checkLength, ");\n"),
"});\n"
].join('');
}
if (check.hasOwnProperty('minLength')) {
var checkMinLength = check.minLength;
if (typeof check.minLength === 'string') {
checkMinLength = "\"".concat(check.minLength, "\"");
if (check.minLength.includes('{{') && check.minLength.includes('}}')) {
checkMinLength = "pm.collectionVariables.get(\"".concat(check.minLength.replace(/{{|}}/g, ''), "\")");
}
}
pmTestMinLength = [
"// Response header should have a minimum length of \"".concat(check.minLength, "\" for \"").concat(check.key, "\"\n"),
"pm.test(\"[".concat(pmOperation.method.toUpperCase(), "]").concat(split).concat(pmOperation.path),
" - Content check if header value of '".concat(check.key, "' has a minimum length of '").concat(check.minLength, "'\", function() {\n"),
" pm.expect(pm.response.headers.get(\"".concat(check.key, "\").length).is.at.least(").concat(checkMinLength, ");\n"),
"});\n"
].join('');
}
if (check.hasOwnProperty('maxLength')) {
var checkMaxLength = check.maxLength;
if (typeof check.maxLength === 'string') {
checkMaxLength = "\"".concat(check.maxLength, "\"");
if (check.maxLength.includes('{{') && check.maxLength.includes('}}')) {
checkMaxLength = "pm.collectionVariables.get(\"".concat(check.maxLength.replace(/{{|}}/g, ''), "\")");
}
}
pmTestMaxLength = [
"// Response header should have a maximum length of \"".concat(check.maxLength, "\" for \"").concat(check.key, "\"\n"),
"pm.test(\"[".concat(pmOperation.method.toUpperCase(), "]").concat(split).concat(pmOperation.path),
" - Content check if header value of '".concat(check.key, "' has a maximum length of '").concat(check.maxLength, "'\", function() {\n"),
" pm.expect(pm.response.headers.get(\"".concat(check.key, "\").length).is.at.most(").concat(checkMaxLength, ");\n"),
"});\n"
].join('');
}
if (check.hasOwnProperty('assert') && check.assert) {
var cleanAssert = check.assert
.replace(/\.\./g, '.')
.replace(/^\.|\.$/g, '')
.replace(/'/g, '"');
var cleanAssertLabel = cleanAssert.replace(/"/g, "'");
pmTestAssert = [
"// Response header value for \"".concat(check.key, "}\" \"").concat(cleanAssert, "\"\n"),
"pm.test(\"[".concat(pmOperation.method.toUpperCase(), "]").concat(split).concat(pmOperation.path),
" - Content check if header value for '".concat(check.key, "' '").concat(cleanAssertLabel, "'\", function() {\n"),
" pm.expect(pm.response.headers.get(\"".concat(check.key, "\")).").concat(cleanAssert, ";\n"),
"});\n"
].join('');
}
if (pmTestKey !== '')
(0, application_1.writeOperationTestScript)(pmOperation, pmTestKey);
if (pmTestValue !== '')
(0, application_1.writeOperationTestScript)(pmOperation, pmTestValue);
if (pmTestContains !== '')
(0, application_1.writeOperationTestScript)(pmOperation, pmTestContains);
if (pmTestOneOf !== '')
(0, application_1.writeOperationTestScript)(pmOperation, pmTestOneOf);
if (pmTestLength !== '')
(0, application_1.writeOperationTestScript)(pmOperation, pmTestLength);
if (pmTestMinLength !== '')
(0, application_1.writeOperationTestScript)(pmOperation, pmTestMinLength);
if (pmTestMaxLength !== '')
(0, application_1.writeOperationTestScript)(pmOperation, pmTestMaxLength);
if (pmTestAssert !== '')
(0, application_1.writeOperationTestScript)(pmOperation, pmTestAssert);
});
return pmOperation;
};
exports.testResponseHeaderContent = testResponseHeaderContent;
//# sourceMappingURL=testResponseHeaderContent.js.map