@case-contract-testing/case
Version:
Next-generation contract testing suite
70 lines (69 loc) • 3.91 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 qs_1 = __importDefault(require("qs"));
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 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, context_1.addLocation)('body', matchContext)),
}
: {}),
...(matcher.query
? {
query: matchContext.descendAndStrip(matcher.query, (0, context_1.addLocation)('query', matchContext)),
}
: {}),
...(matcher.headers
? {
headers: matchContext.descendAndStrip(matcher.headers, (0, context_1.addLocation)('headers', matchContext)),
}
: {}),
method: (0, resolve_1.mustResolveToString)(matcher.method, (0, context_1.addLocation)('method', matchContext)),
path: (0, resolve_1.mustResolveToString)(matcher.path, (0, context_1.addLocation)('path', matchContext)),
});
const check = async (matcher, matchContext, actual) => {
if (!actual) {
throw new entities_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 entities_1.CaseCoreError("The HttpRequestMatcher was invoked with something that isn't http request data.", matchContext);
}
return (0, results_1.combineResults)(...(await Promise.all([
matchContext.descendAndCheck(matcher.method, (0, context_1.addLocation)('method', matchContext), actual.method),
matchContext.descendAndCheck(matcher.path, (0, context_1.addLocation)('path', matchContext), actual.path),
matcher.query !== undefined
? matchContext.descendAndCheck(matcher.query, (0, context_1.addLocation)('query', matchContext), actual.query)
: Promise.resolve((0, results_1.makeResults)()),
matcher.headers !== undefined
? matchContext.descendAndCheck(matcher.headers, (0, context_1.addLocation)('headers', matchContext), actual.headers)
: Promise.resolve((0, results_1.makeResults)()),
matcher.body !== undefined
? matchContext.descendAndCheck(matcher.body, (0, context_1.addLocation)('body', matchContext), actual.body)
: Promise.resolve((0, results_1.makeResults)()),
])));
};
const name = (request, context) => request.uniqueName
? request.uniqueName
: `an http ${context.descendAndDescribe(request.method, (0, context_1.addLocation)('method', context))} request to ${context.descendAndDescribe(request.path, (0, context_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, context_1.addLocation)(`query[${key}]`, context)),
}), {}), { encode: false })}`
: ''}${request.headers
? ` with the following headers ${context.descendAndDescribe(request.headers, (0, context_1.addLocation)('headers', context))}`
: ''}${request.body
? ` and body ${context.descendAndDescribe(request.body, (0, context_1.addLocation)('body', context))}`
: ' without a body'}`;
exports.HttpRequestMatcher = { describe: name, check, strip };