UNPKG

cypress-contract-stubs

Version:

Cypress plugin for Spring Contract Stubs

108 lines 4.09 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.interceptStubs = void 0; function interceptStubs(options) { cy.task('contract:stubs', null, { log: false }).then((stubs) => { stubs .filter((stub) => filterByStubNames(stub.name, options === null || options === void 0 ? void 0 : options.names)) .filter((stub) => filterByStubOptions(stub, options)) .sort(sortByPriority) .forEach(({ id, name, request, response }) => { try { const matcher = { query: {}, headers: {} }; matcher.method = request.method; matchRequestUrl(request, matcher); matchRequestQueryParams(request, matcher); matchRequestHeaders(request, matcher); cy.intercept(matcher, (req) => { req.reply(response.status || 200, response.body, response.headers); }).as(id); } catch (e) { console.error(`Error when generating matcher for stub "${name}"`, e.message); } }); }); } exports.interceptStubs = interceptStubs; function filterByStubNames(name, stubNames) { if (stubNames === null || stubNames === void 0 ? void 0 : stubNames.length) { return stubNames.includes(name); } else { return true; } } function filterByStubOptions({ request }, options) { var _a, _b, _c; let shouldBeIntercepted = true; for (const header in options === null || options === void 0 ? void 0 : options.headers) { const optionsValue = options === null || options === void 0 ? void 0 : options.headers[header]; const stubValue = (_a = request.headers) === null || _a === void 0 ? void 0 : _a[header]; if (stubValue === null || stubValue === void 0 ? void 0 : stubValue.equalTo) { if (stubValue.equalTo === optionsValue) { (_b = request.headers) === null || _b === void 0 ? true : delete _b[header]; shouldBeIntercepted = true; } else { shouldBeIntercepted = false; } } else if (stubValue === null || stubValue === void 0 ? void 0 : stubValue.matches) { if (matchPattern(stubValue.matches).test(optionsValue)) { (_c = request.headers) === null || _c === void 0 ? true : delete _c[header]; shouldBeIntercepted = true; } else { shouldBeIntercepted = false; } } } return shouldBeIntercepted; } function sortByPriority(stubA, stubB) { const priorityA = stubA.priority || 0; const priorityB = stubB.priority || 0; return priorityA - priorityB; } function matchRequestUrl(request, matcher) { if (request.url) { matcher.url = request.url; } else if (request.urlPath) { matcher.pathname = request.urlPath; } else if (request.urlPattern) { matcher.url = matchPattern(request.urlPattern); } else if (request.urlPathPattern) { matcher.pathname = matchPattern(request.urlPathPattern); } } function matchRequestQueryParams(request, matcher) { for (const param in request.queryParameters) { const value = request.queryParameters[param]; if (value.equalTo) { matcher.query[param] = value.equalTo; } else if (value.matches) { matcher.query[param] = matchPattern(value.matches); } } } function matchRequestHeaders(request, matcher) { for (const header in request.headers) { const value = request.headers[header]; if (value.equalTo) { matcher.headers[header] = value.equalTo; } else if (value.matches) { matcher.headers[header] = matchPattern(value.matches); } } } function matchPattern(pattern) { return new RegExp(pattern.replace('?+', '?.*'), 'g'); } Cypress.Commands.add('interceptStubs', interceptStubs); //# sourceMappingURL=intercept-stubs.js.map