@contract-case/case-core-plugin-http
Version:
ContractCase core HTTP plugin, providing HTTP matchers and mocks
58 lines • 3.24 kB
JavaScript
;
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.combineResults)(...(await Promise.all([
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) => response.uniqueName
? response.uniqueName
: `a (${context.descendAndDescribe(response.status, (0, case_plugin_base_1.addLocation)('status', context))}) response ${response.body
? `with body ${context.descendAndDescribe(response.body, (0, case_plugin_base_1.addLocation)('body', context))}`
: 'without a body'}${response.headers
? ` with the following headers ${context.descendAndDescribe(response.headers, (0, case_plugin_base_1.addLocation)('headers', context))}`
: ''}`;
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