@pact-foundation/pact
Version:
Pact for all things Javascript
106 lines • 4.17 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.displayQuery = displayQuery;
exports.displayRequest = displayRequest;
exports.filterMissingFeatureFlag = filterMissingFeatureFlag;
exports.printMismatch = printMismatch;
exports.printMismatches = printMismatches;
exports.generateMockServerError = generateMockServerError;
const ramda_1 = require("ramda");
// TODO: update Matching in the rust core to have a `type` property
// to avoid having to do this check!
const isMismatchingResultPlugin = (obj) => {
if (obj.error !== undefined &&
obj.mismatches)
return true;
return false;
};
const isPluginContentMismatch = (obj) => {
const cast = obj;
if (cast.diff !== undefined ||
(cast.expected !== undefined &&
cast.actual !== undefined &&
cast.mismatch !== undefined &&
cast.path !== undefined))
return true;
return false;
};
function displayQuery(query) {
const pairs = (0, ramda_1.toPairs)(query);
const mapped = (0, ramda_1.flatten)((0, ramda_1.map)(([key, values]) => (0, ramda_1.map)((val) => `${key}=${val}`, values), pairs));
return (0, ramda_1.join)('&', mapped);
}
function displayHeaders(headers, indent) {
return (0, ramda_1.join)(`\n${indent}`, (0, ramda_1.map)(([k, v]) => `${k}: ${v}`, (0, ramda_1.toPairs)(headers)));
}
function displayRequest(request, indent = '') {
const output = [''];
output.push(`${indent}Method: ${request.method}\n${indent}Path: ${request.path}`);
if (request.query) {
output.push(`${indent}Query String: ${displayQuery(request.query)}`);
}
if (request.headers) {
output.push(`${indent}Headers:\n${indent} ${displayHeaders(request.headers, `${indent} `)}`);
}
if (request.body) {
const body = JSON.stringify(request.body);
output.push(`${indent}Body: ${body.substr(0, 20)}... (${body.length} length)`);
}
return output.join('\n');
}
function filterMissingFeatureFlag(mismatches) {
if (process.env.PACT_EXPERIMENTAL_FEATURE_ALLOW_MISSING_REQUESTS) {
return mismatches.filter((m) => !isMismatchingResultPlugin(m) && m.type !== 'request-mismatch');
}
return mismatches;
}
function printMismatch(m) {
if (isPluginContentMismatch(m)) {
const s = [
`\t${m.path}: ${m.mismatch}\n`,
m.mismatch
? ''
: `\t\tExpected '${m.expected}', got: '${m.actual}${m.diff}'`,
];
if (m.diff) {
s.push(`\t\tDiff:`);
s.push(`\t\t\t${m.diff}`);
}
return s.join('\n\n');
}
switch (m.type) {
case 'MethodMismatch':
return `Expected ${m.expected}, got: ${m.actual}`;
default:
return m.mismatch;
}
}
function printMismatches(mismatches) {
const errors = mismatches.map((m) => printMismatch(m));
return errors.join('\n');
}
function generateMockServerError(mismatches, indent) {
return [
'Mock server failed with the following mismatches:',
...mismatches.map((mismatch, i) => {
if (isMismatchingResultPlugin(mismatch)) {
return printMismatches(mismatch.mismatches);
}
if (mismatch.type === 'request-mismatch') {
return `\n${indent}${i}) The following request was incorrect: \n
${indent}${mismatch.method} ${mismatch.path}
${mismatch.mismatches
?.map((d, j) => `\n${indent}${indent}${indent} 1.${j} ${printMismatch(d)}`)
.join('')}`;
}
if (mismatch.type === 'request-not-found') {
return `\n${indent}${i}) The following request was not expected: ${displayRequest(mismatch.request, `${indent} `)}`;
}
if (mismatch.type === 'missing-request') {
return `\n${indent}${i}) The following request was expected but not received: ${displayRequest(mismatch.request, `${indent} `)}`;
}
return `Unknown mismatch: ${mismatch}`;
}),
].join('\n');
}
//# sourceMappingURL=display.js.map