UNPKG

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

Version:

ContractCase core HTTP plugin, providing HTTP matchers and mocks

71 lines 3.85 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.HttpResponseMatcher = void 0; const case_plugin_base_1 = require("@contract-case/case-plugin-base"); const codeValidator_1 = require("./codeValidator"); const isHttpResponseData = (data) => { const maybeResponseData = data; return typeof maybeResponseData.status === 'number'; }; const strip = (matcher, matchContext) => ({ ...(matcher.body ? { body: matchContext.descendAndStrip(matcher.body, (0, case_plugin_base_1.addLocation)('body', matchContext)), } : {}), ...(matcher.headers ? { headers: matchContext.descendAndStrip(matcher.headers, (0, case_plugin_base_1.addLocation)('headers', matchContext)), } : {}), status: (0, case_plugin_base_1.mustResolveToNumber)(matcher.status, (0, case_plugin_base_1.addLocation)('status', matchContext)), }); const check = async (matcher, matchContext, actual) => { if (!isHttpResponseData(actual)) { throw new case_plugin_base_1.CaseCoreError("The HttpResponseMatcher was called with an actual that wasn't an http response"); } return (0, case_plugin_base_1.combineResultPromises)(...[ matchContext.descendAndCheck(matcher.status, (0, case_plugin_base_1.addLocation)('status', matchContext), actual.status), matcher.headers !== undefined ? await matchContext.descendAndCheck(matcher.headers, (0, case_plugin_base_1.addLocation)('headers', matchContext), actual.headers) : (0, case_plugin_base_1.makeNoErrorResult)(), matcher.body !== undefined ? matchContext.descendAndCheck(matcher.body, (0, case_plugin_base_1.addLocation)('body', matchContext), actual.body) : (0, case_plugin_base_1.makeNoErrorResult)(), ]); }; const name = (response, context) => { if (response.uniqueName) { return (0, case_plugin_base_1.describeMessage)(`returns ${response.uniqueName}`); } const segments = [ (0, case_plugin_base_1.describeMessage)('returns a '), (0, case_plugin_base_1.concatenateDescribe)((0, case_plugin_base_1.describeMessage)('('), context.descendAndDescribe(response.status, (0, case_plugin_base_1.addLocation)('status', context)), (0, case_plugin_base_1.describeMessage)(')')), (0, case_plugin_base_1.describeMessage)(' response '), ]; if (response.body) { segments.push((0, case_plugin_base_1.describeMessage)('with body '), context.descendAndDescribe(response.body, (0, case_plugin_base_1.addLocation)('body', context))); } else { segments.push((0, case_plugin_base_1.describeMessage)('without a body')); } if (response.headers) { segments.push((0, case_plugin_base_1.describeMessage)(' with the following headers '), context.descendAndDescribe(response.headers, (0, case_plugin_base_1.addLocation)('headers', context))); } return (0, case_plugin_base_1.concatenateDescribe)(...segments); }; const validate = (matcher, matchContext) => Promise.resolve() .then(() => matcher.body != null ? matchContext.descendAndValidate(matcher.body, (0, case_plugin_base_1.addLocation)('body', matchContext)) : null) .then(() => matcher.headers != null ? matchContext.descendAndValidate(matcher.headers, (0, case_plugin_base_1.addLocation)('headers', matchContext)) : null) .then(() => { if (!(0, case_plugin_base_1.isCaseNode)(matcher.status)) { (0, codeValidator_1.validateCodes)(matcher.status, (0, case_plugin_base_1.addLocation)('status', matchContext)); } return matchContext.descendAndValidate(matcher.status, (0, case_plugin_base_1.addLocation)('status', matchContext)); }); exports.HttpResponseMatcher = { describe: name, check, strip, validate }; //# sourceMappingURL=HttpResponseMatcher.js.map