@case-contract-testing/case
Version:
Next-generation contract testing suite
27 lines (26 loc) • 1.73 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Base64EncodedStringMatcher = void 0;
const entities_1 = require("../../entities");
const context_1 = require("../../entities/context");
const results_1 = require("../../entities/results");
const check = (matcher, matchContext, actual) => {
if (typeof actual === 'string') {
if (actual.match(/^([A-Za-z0-9+/]{4})*([A-Za-z0-9+/]{3}=|[A-Za-z0-9+/]{2}==)?$/) !== null) {
return matchContext.descendAndCheck(matcher['case:matcher:child'], (0, context_1.addLocation)(':base64', matchContext), Buffer.from(actual, 'base64').toString());
}
return (0, results_1.makeResults)((0, results_1.matchingError)(matcher, `${(0, results_1.actualToString)(actual)} is not a base64 encoded string`, actual, matchContext));
}
return (0, results_1.makeResults)((0, results_1.matchingError)(matcher, `${(0, results_1.actualToString)(actual)} is not a string; so it can't match a base64 encoded string`, actual, matchContext));
};
const strip = (matcher, matchContext) => {
const result = matchContext.descendAndStrip(matcher['case:matcher:child'], (0, context_1.addLocation)(':base64', matchContext));
if (typeof result === 'string')
return Buffer.from(result).toString('base64');
throw new entities_1.CaseConfigurationError(`Unable to base64 encode '${result}' during stripMatchers, as it's not a string and therefore can't be base64 encoded`);
};
exports.Base64EncodedStringMatcher = {
describe: (matcher, matchContext) => `base64 encoded string '${matchContext.descendAndDescribe(matcher['case:matcher:child'], (0, context_1.addLocation)(':base64', matchContext))}'`,
check,
strip,
};