@case-contract-testing/case
Version:
Next-generation contract testing suite
47 lines (46 loc) • 2.43 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.HttpResponseMatcher = void 0;
const entities_1 = require("../../entities");
const context_1 = require("../../entities/context");
const resolve_1 = require("../../entities/nodes/matchers/resolve");
const results_1 = require("../../entities/results");
const isHttpResponseData = (data) => {
const maybeResponseData = data;
return typeof maybeResponseData.status === 'number';
};
const strip = (matcher, matchContext) => ({
...(matcher.body
? {
body: matchContext.descendAndStrip(matcher.body, (0, context_1.addLocation)('body', matchContext)),
}
: {}),
...(matcher.headers
? {
headers: matchContext.descendAndStrip(matcher.headers, (0, context_1.addLocation)('headers', matchContext)),
}
: {}),
status: (0, resolve_1.mustResolveToNumber)(matcher.status, (0, context_1.addLocation)('status', matchContext)),
});
const check = async (matcher, matchContext, actual) => {
if (!isHttpResponseData(actual)) {
throw new entities_1.CaseCoreError("The HttpResponseMatcher was called with an actual that wasn't an http response");
}
return (0, results_1.combineResults)(...(await Promise.all([
matchContext.descendAndCheck(matcher.status, (0, context_1.addLocation)('status', matchContext), actual.status),
matcher.headers !== undefined
? await matchContext.descendAndCheck(matcher.headers, (0, context_1.addLocation)('headers', matchContext), actual.headers)
: (0, results_1.makeNoErrorResult)(),
matcher.body !== undefined
? matchContext.descendAndCheck(matcher.body, (0, context_1.addLocation)('body', matchContext), actual.body)
: (0, results_1.makeNoErrorResult)(),
])));
};
const name = (response, context) => response.uniqueName
? response.uniqueName
: `a (${context.descendAndDescribe(response.status, (0, context_1.addLocation)('status', context))}) response ${response.body
? `with body ${context.descendAndDescribe(response.body, (0, context_1.addLocation)('body', context))}`
: 'without a body'}${response.headers
? ` with the following headers ${context.descendAndDescribe(response.headers, (0, context_1.addLocation)('headers', context))}`
: ''}`;
exports.HttpResponseMatcher = { describe: name, check, strip };