UNPKG

unmock-core

Version:

[![npm](https://img.shields.io/npm/v/unmock-core.svg)][npmjs] [![CircleCI](https://circleci.com/gh/unmock/unmock-js.svg?style=svg)](https://circleci.com/gh/unmock/unmock-js) [![codecov](https://codecov.io/gh/unmock/unmock-js/branch/dev/graph/badge.svg)](h

121 lines 5.27 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); const debug_1 = __importDefault(require("debug")); const xregexp_1 = __importDefault(require("xregexp")); const util_1 = require("./util"); const debugLog = debug_1.default("unmock:matcher"); class OASMatcher { static normalizeRequestPathToServerPath(reqPath, serverPathPrefix) { const serverUrlWithoutTrailingSlash = serverPathPrefix.replace(/\/$/, ""); const regexToRemoveFromReqPath = new RegExp(`^${serverUrlWithoutTrailingSlash}`); return reqPath.replace(regexToRemoveFromReqPath, ""); } static buildRegexpForPaths(schema) { const mapping = {}; const paths = schema.paths; if (paths === undefined || paths.length === 0) { return mapping; } Object.keys(paths).forEach((path) => { const pathParameters = util_1.getPathParametersFromPath(path); let newPath = ""; if (pathParameters.length === 0) { newPath = path; } else { const schemaParameters = util_1.getPathParametersFromSchema(paths, path); newPath = util_1.buildPathRegexStringFromParameters(path, schemaParameters, pathParameters); } const newPathRegex = xregexp_1.default(`^${newPath}$`, "gi"); mapping[path] = newPathRegex; }); return mapping; } constructor({ schema }) { this.schema = schema; this.endpointToRegexMapping = OASMatcher.buildRegexpForPaths(this.schema); } matchToOperationObject(sreq) { const { matches, reqPathWithoutServerPrefix } = this.matchesServer(sreq); if (!matches) { debugLog(`Could not find a matching server.`); return undefined; } if (reqPathWithoutServerPrefix === undefined) { throw new Error("Expected to get a path without the server path prefix"); } debugLog(`Matched server, looking for match for ${reqPathWithoutServerPrefix}`); const matchingPathItemOrUndef = this.findMatchingPathItem(reqPathWithoutServerPrefix); if (matchingPathItemOrUndef === undefined) { return undefined; } const requestMethod = sreq.method.toLowerCase(); debugLog(`Matched path object, looking for match for ${requestMethod}`); const matchingPath = matchingPathItemOrUndef; return matchingPath[requestMethod]; } findEndpoint(reqPath) { const paths = this.schema.paths; if (paths === undefined || paths.length === 0) { return undefined; } const servers = this.schema.servers; if (servers !== undefined && servers.length > 0) { for (const server of servers) { const serverUrl = new URL(server.url); if (reqPath.startsWith(serverUrl.pathname)) { reqPath = OASMatcher.normalizeRequestPathToServerPath(reqPath, serverUrl.pathname); break; } } } const directMatch = paths[reqPath]; if (directMatch !== undefined) { debugLog(`Found direct path match for ${reqPath}`); return { schemaEndpoint: reqPath, normalizedEndpoint: reqPath }; } const definedPaths = Object.keys(paths); debugLog(`Searching for match for ${reqPath} for ${definedPaths}`); for (const pathItemKey of Object.keys(paths)) { const pathRegex = this.endpointToRegexMapping[pathItemKey]; if (xregexp_1.default.test(reqPath, pathRegex)) { return { schemaEndpoint: pathItemKey, normalizedEndpoint: reqPath }; } } return undefined; } findMatchingPathItem(reqPath) { const maybePathItemKey = this.findEndpoint(reqPath); return maybePathItemKey !== undefined ? this.schema.paths[maybePathItemKey.schemaEndpoint] : undefined; } matchesServer(sreq) { const servers = this.schema.servers; if (servers === undefined || servers.length === 0) { debugLog("No servers to match"); return { matches: false }; } for (const server of servers) { const serverUrl = new URL(server.url); const protocol = serverUrl.protocol.replace(":", ""); debugLog(`Testing: ${protocol} vs. ${sreq.protocol}, ${serverUrl.hostname} ` + `vs ${sreq.host}, ${sreq.path} vs ${serverUrl.pathname}`); if (protocol === sreq.protocol && serverUrl.hostname === sreq.host && sreq.path.startsWith(serverUrl.pathname)) { const reqPathWithoutServerPrefix = OASMatcher.normalizeRequestPathToServerPath(sreq.path, serverUrl.pathname); return { matches: true, reqPathWithoutServerPrefix, }; } } return { matches: false }; } } exports.OASMatcher = OASMatcher; //# sourceMappingURL=matcher.js.map