@contract-case/case-core-plugin-http
Version:
ContractCase core HTTP plugin, providing HTTP matchers and mocks
102 lines • 5.39 kB
JavaScript
;
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.combineResultPromises)(...[
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) => {
if (request.uniqueName) {
return (0, case_plugin_base_1.describeMessage)(request.uniqueName);
}
const segments = [
(0, case_plugin_base_1.describeMessage)('an http '),
context.descendAndDescribe(request.method, (0, case_plugin_base_1.addLocation)('method', context)),
(0, case_plugin_base_1.describeMessage)(' request to '),
context.descendAndDescribe(request.path, (0, case_plugin_base_1.addLocation)('path', context)),
];
if (request.query !== undefined) {
segments.push((0, case_plugin_base_1.describeMessage)(`?${qs_1.default.stringify(Object.entries(request.query).reduce((acc, [key, value]) => ({
...acc,
[key]: (0, case_plugin_base_1.renderToString)(context.descendAndDescribe(value, (0, case_plugin_base_1.addLocation)(`query[${key}]`, context))),
}), {}), { encode: false })}`));
}
if (request.headers) {
segments.push((0, case_plugin_base_1.describeMessage)(' with the following headers '), context.descendAndDescribe(request.headers, (0, case_plugin_base_1.addLocation)('headers', context)));
}
if (request.body) {
segments.push((0, case_plugin_base_1.describeMessage)(' and body '), context.descendAndDescribe(request.body, (0, case_plugin_base_1.addLocation)('body', context)));
}
else {
segments.push((0, case_plugin_base_1.describeMessage)(' without a body'));
}
return (0, case_plugin_base_1.concatenateDescribe)(...segments);
};
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