request-mocking-protocol
Version:
A protocol for declarative mocking of HTTP requests
36 lines • 1.14 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MatchingContext = void 0;
class MatchingContext {
req;
logs = [];
params = {};
#searchParams;
constructor(req) {
this.req = req;
this.logs.push(`Matching request: ${req.method} ${req.url}`);
}
get searchParams() {
if (!this.#searchParams) {
this.#searchParams = new URL(this.req.url).searchParams;
}
return this.#searchParams;
}
log(result, entity, expected, actual) {
const icon = result ? '✅' : '❌';
this.logs.push(`${icon} Expected ${entity}: ${expected}`);
this.logs.push(`${' '.repeat(4)} Actual ${entity}: ${actual}`);
}
logDone(matched) {
this.logs.push(`Request ${matched ? 'matched' : 'not matched'}.`);
}
appendParams(groups = {}) {
Object.keys(groups).forEach((key) => {
const isNamedGroup = !/^\d+$/.test(key);
if (isNamedGroup)
this.params[key] = groups[key];
});
}
}
exports.MatchingContext = MatchingContext;
//# sourceMappingURL=context.js.map