UNPKG

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

Version:

ContractCase core HTTP plugin, providing HTTP matchers and mocks

87 lines 4.83 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.HttpRequestMatcher = void 0; const case_plugin_base_1 = require("@contract-case/case-plugin-base"); const qs_1 = __importDefault(require("qs")); const isHttpRequestData = (data) => { const maybeRequestData = data; return (typeof maybeRequestData.method === 'string' && typeof maybeRequestData.path === 'string'); }; const strip = (matcher, matchContext) => ({ ...(matcher.body ? { body: matchContext.descendAndStrip(matcher.body, (0, case_plugin_base_1.addLocation)('body', matchContext)), } : {}), ...(matcher.query ? { query: matchContext.descendAndStrip(matcher.query, (0, case_plugin_base_1.addLocation)('query', matchContext)), } : {}), ...(matcher.headers ? { headers: matchContext.descendAndStrip(matcher.headers, (0, case_plugin_base_1.addLocation)('headers', matchContext)), } : {}), method: (0, case_plugin_base_1.mustResolveToString)(matcher.method, (0, case_plugin_base_1.addLocation)('method', matchContext)), path: (0, case_plugin_base_1.mustResolveToString)(matcher.path, (0, case_plugin_base_1.addLocation)('path', matchContext)), }); const check = async (matcher, matchContext, actual) => { if (!actual) { throw new case_plugin_base_1.CaseConfigurationError('The server was never called. Please confirm that you are calling the mock server, and not your real server', matchContext); } if (!isHttpRequestData(actual)) { throw new case_plugin_base_1.CaseCoreError("The HttpRequestMatcher was invoked with something that isn't http request data.", matchContext); } return (0, case_plugin_base_1.combineResults)(...(await Promise.all([ matchContext.descendAndCheck(matcher.method, (0, case_plugin_base_1.addLocation)('method', matchContext), actual.method), matchContext.descendAndCheck(matcher.path, (0, case_plugin_base_1.addLocation)('path', matchContext), actual.path), matcher.query !== undefined ? matchContext.descendAndCheck(matcher.query, (0, case_plugin_base_1.addLocation)('query', matchContext), actual.query) : Promise.resolve((0, case_plugin_base_1.makeResults)()), matcher.headers !== undefined ? matchContext.descendAndCheck(matcher.headers, (0, case_plugin_base_1.addLocation)('headers', matchContext), actual.headers) : Promise.resolve((0, case_plugin_base_1.makeResults)()), matcher.body !== undefined ? matchContext.descendAndCheck(matcher.body, (0, case_plugin_base_1.addLocation)('body', matchContext), actual.body) : Promise.resolve((0, case_plugin_base_1.makeResults)()), ]))); }; const name = (request, context) => request.uniqueName ? request.uniqueName : `an http ${context.descendAndDescribe(request.method, (0, case_plugin_base_1.addLocation)('method', context))} request to ${context.descendAndDescribe(request.path, (0, case_plugin_base_1.addLocation)('path', context))}${request.query !== undefined ? `?${qs_1.default.stringify(Object.entries(request.query).reduce((acc, [key, value]) => ({ ...acc, [key]: context.descendAndDescribe(value, (0, case_plugin_base_1.addLocation)(`query[${key}]`, context)), }), {}), { encode: false })}` : ''}${request.headers ? ` with the following headers ${context.descendAndDescribe(request.headers, (0, case_plugin_base_1.addLocation)('headers', context))}` : ''}${request.body ? ` and body ${context.descendAndDescribe(request.body, (0, case_plugin_base_1.addLocation)('body', context))}` : ' without a body'}`; const validate = (matcher, matchContext) => Promise.resolve() .then(() => { if (matcher.body != null) { return matchContext.descendAndValidate(matcher.body, (0, case_plugin_base_1.addLocation)('body', matchContext)); } return undefined; }) .then(() => { if (matcher.query != null) { return matchContext.descendAndValidate(matcher.query, (0, case_plugin_base_1.addLocation)('query', matchContext)); } return undefined; }) .then(() => { if (matcher.headers != null) { return matchContext.descendAndValidate(matcher.headers, (0, case_plugin_base_1.addLocation)('headers', matchContext)); } return undefined; }) .then(() => matchContext.descendAndValidate(matcher.method, (0, case_plugin_base_1.addLocation)('method', matchContext))); exports.HttpRequestMatcher = { describe: name, check, strip, validate }; //# sourceMappingURL=HttpRequestMatcher.js.map