request-mocking-protocol
Version:
A protocol for declarative mocking of HTTP requests
27 lines • 1.01 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.QueryMatcher = void 0;
class QueryMatcher {
schema;
expectedQuery;
constructor(schema) {
this.schema = schema;
this.expectedQuery = this.schema.query || {};
}
get hasQuery() {
return Object.keys(this.expectedQuery).length > 0;
}
match(ctx) {
return Object.keys(this.expectedQuery).every((key) => this.matchQueryParam(ctx, key));
}
matchQueryParam(ctx, name) {
// todo: handle multi-value params in expectedQuery
const expectedValue = this.expectedQuery[name]?.toString() ?? null;
const actualValues = ctx.searchParams.getAll(name);
const result = expectedValue === null ? actualValues.length === 0 : actualValues.includes(expectedValue);
ctx.log(result, `query param "${name}"`, expectedValue, actualValues.join(','));
return result;
}
}
exports.QueryMatcher = QueryMatcher;
//# sourceMappingURL=query.js.map