UNPKG

@contract-case/case-core-plugin-http

Version:

ContractCase core HTTP plugin, providing HTTP matchers and mocks

28 lines 1.9 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.UrlEncodedStringMatcher = void 0; const case_plugin_base_1 = require("@contract-case/case-plugin-base"); const check = (matcher, matchContext, actual) => typeof actual === 'string' ? matchContext.descendAndCheck(matcher['_case:matcher:child'], (0, case_plugin_base_1.addLocation)(':urlEncoded', matchContext), decodeURIComponent(actual)) : (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 can't treat it as an encoded URL component`, actual, matchContext)); const strip = (matcher, matchContext) => { const result = matchContext.descendAndStrip(matcher['_case:matcher:child'], (0, case_plugin_base_1.addLocation)(':urlEncoded', matchContext)); if (typeof result === 'string') return encodeURIComponent(result); throw new case_plugin_base_1.CaseConfigurationError(`Unable to URL encode '${result}' during stripMatchers, as it's not a string and therefore can't be URL encoded`); }; exports.UrlEncodedStringMatcher = { describe: (matcher, matchContext) => `uriEncoded string '${matchContext.descendAndDescribe(matcher['_case:matcher:child'], (0, case_plugin_base_1.addLocation)(':urlEncoded', matchContext))}'`, check, strip, validate: (matcher, matchContext) => Promise.resolve().then(() => { const encodedString = (0, case_plugin_base_1.mustResolveToString)(matcher['_case:matcher:child'], (0, case_plugin_base_1.addLocation)(':urlEncoded', matchContext)); try { decodeURIComponent(encodedString); } catch (e) { throw new case_plugin_base_1.CaseConfigurationError(`The provided string example failed to decode as a URI: ${e.message}`); } }), }; //# sourceMappingURL=UrlEncodedStringMatcher.js.map