UNPKG

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

Version:

ContractCase core HTTP plugin, providing HTTP matchers and mocks

57 lines 3.8 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HttpBasicAuthMatcher = void 0; const case_plugin_base_1 = require("@contract-case/case-plugin-base"); const encode = (username, password) => { if (username.includes(':')) { throw new case_plugin_base_1.CaseConfigurationError(`The username for basic auth was: '${username}', but it must not contain a ':' (See RFC 7617)`); } return Buffer.from(`${username}:${password}`).toString('base64'); }; const check = async (matcher, matchContext, actual) => { if (typeof actual !== 'string') { 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 can't use it as basic auth`, actual, matchContext)); } let decoded; try { decoded = Buffer.from(actual, 'base64').toString('utf8'); } catch { return (0, case_plugin_base_1.makeResults)((0, case_plugin_base_1.matchingError)(matcher, `${(0, case_plugin_base_1.actualToString)(actual)} failed to decode from base64 to utf8`, actual, matchContext)); } if (!decoded.includes(':')) { return (0, case_plugin_base_1.makeResults)((0, case_plugin_base_1.matchingError)(matcher, `Basic auth value decoded to ${decoded}, which doesn't contain a ':'`, actual, matchContext)); } const pivot = decoded.indexOf(':'); const actualUsername = decoded.substring(0, pivot); const actualPassword = decoded.substring(pivot + 1); return (0, case_plugin_base_1.combineResultPromises)(matchContext.descendAndCheck(matcher['_case:matcher:username'], (0, case_plugin_base_1.addLocation)('username', matchContext), actualUsername), matchContext.descendAndCheck(matcher['_case:matcher:password'], (0, case_plugin_base_1.addLocation)('password', matchContext), actualPassword)); }; const strip = (matcher, matchContext) => { const username = matchContext.descendAndStrip(matcher['_case:matcher:username'], matchContext); const password = matchContext.descendAndStrip(matcher['_case:matcher:password'], matchContext); if (typeof username !== 'string') { throw new case_plugin_base_1.CaseConfigurationError("The username for basic auth didn't resolve to a string, please check the definition"); } if (typeof password !== 'string') { throw new case_plugin_base_1.CaseConfigurationError("The password for basic auth didn't resolve to a string, please check the definition"); } return encode(username, password); }; exports.HttpBasicAuthMatcher = { describe: (matcher, matchContext) => `http basic auth with username='${matchContext.descendAndDescribe(matcher['_case:matcher:username'], (0, case_plugin_base_1.addLocation)('username', matchContext))}' and password=${matchContext.descendAndDescribe(matcher['_case:matcher:password'], (0, case_plugin_base_1.addLocation)('password', matchContext))}`, check, strip, validate: (matcher, matchContext) => Promise.resolve() .then(() => { if (!('_case:matcher:password' in matcher)) { throw new case_plugin_base_1.CaseConfigurationError('The HttpBasicAuthMatcher must be given a password', matchContext); } if (!('_case:matcher:username' in matcher)) { throw new case_plugin_base_1.CaseConfigurationError('The HttpBasicAuthMatcher must be given a username', matchContext); } }) .then(() => matchContext.descendAndValidate(matcher['_case:matcher:username'], (0, case_plugin_base_1.addLocation)('username', matchContext))) .then(() => matchContext.descendAndValidate(matcher['_case:matcher:password'], (0, case_plugin_base_1.addLocation)('password', matchContext))), }; //# sourceMappingURL=HttpBasicAuthMatcher.js.map