UNPKG

@contract-case/case-core

Version:

Core functionality for the ContractCase contract testing suite

34 lines 2.35 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.Base64EncodedStringMatcher = void 0; const case_plugin_base_1 = require("@contract-case/case-plugin-base"); 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, case_plugin_base_1.addLocation)(':base64', matchContext), Buffer.from(actual, 'base64').toString()); } return (0, case_plugin_base_1.makeResults)((0, case_plugin_base_1.matchingError)(matcher, `${(0, case_plugin_base_1.actualToString)(actual)} is not a base64 encoded string`, actual, matchContext)); } return (0, case_plugin_base_1.makeResults)((0, case_plugin_base_1.matchingError)(matcher, `${(0, case_plugin_base_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, case_plugin_base_1.addLocation)(':base64', matchContext)); if (typeof result === 'string') return Buffer.from(result).toString('base64'); throw new case_plugin_base_1.CaseConfigurationError(`Unable to base64 encode '${result}' during stripMatchers, as it's not a string and therefore can't be base64 encoded`, matchContext, 'BAD_INTERACTION_DEFINITION'); }; exports.Base64EncodedStringMatcher = { describe: (matcher, matchContext) => `base64 encoded string '${matchContext.descendAndDescribe(matcher['_case:matcher:child'], (0, case_plugin_base_1.addLocation)(':base64', matchContext))}'`, check, strip, validate: (matcher, matchContext) => Promise.resolve().then(() => { const encodedString = (0, case_plugin_base_1.mustResolveToString)(matcher, (0, case_plugin_base_1.addLocation)(':base64', matchContext)); try { Buffer.from(encodedString, 'base64').toString(); } catch (e) { throw new case_plugin_base_1.CaseConfigurationError(`The provided string example failed to decode as base64: ${e.message}`, matchContext, 'BAD_INTERACTION_DEFINITION'); } }), }; //# sourceMappingURL=Base64EncodedString.js.map