Press n or j to go to the next uncovered block, b, p or k for the previous block.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 | 3x 3x 3x 5x 5x 5x 5x 5x 12x 12x 17x 10x 10x 12x 8x 8x 5x 3x 7x 4x 3x 3x 3x 2x 1x 3x 3x 3x 21x 21x 3x 7x 7x 7x 7x 21x 7x 1x 6x 6x 5x 5x 5x 12x 5x 5x 5x 3x 3x 2x 5x 2x 2x 2x 3x 2x 2x 5x 2x 3x 3x 3x 3x 3x | const path = require('path'); const store = require('./store'); const getHeaderMatchedStubs = (reqHeaders, stubs) => { const incomingHeaders = Object.keys(reqHeaders); const files = Object.keys(stubs); const allMatchedMappings = []; let max_weight = 0; files.forEach((file) => { const headers = stubs[file]; const matchedHeaders = headers .filter((header) => incomingHeaders.includes(header.header)) .filter((header) => header.match(reqHeaders[header.header])) .map((header) => header.header); if (matchedHeaders.length === headers.length) { allMatchedMappings.push({ file, size: headers.length }); if (headers.length > max_weight) max_weight = headers.length; } }); return { allMatchedMappings, max_weight }; }; const processData = (file, req) => { if (typeof "String" !== typeof file) { return { data: file, status: 200 }; } const logicFile = require(path.join(store.functionDirectory, file)); const res = logicFile.response; if (res.inlineData) { res.data = res.inlineData; } else { res.data = require(path.join(store.dataDirectory, res.bodyFileName)); } logicFile.logic && logicFile.logic(req, res); return res; } const urlMatcher = (URLpattern, incomingURL) => { const regex = RegExp(URLpattern); return regex.test(incomingURL) } const getMatchedStub = ({ url, headers, method }) => { const routes = store.routes; const scenarioMap = store.scenarioMap; const allstubs = store.stubs; let stubs = {}, urlsWithMethods; const matchedURLs = Object.keys(routes).filter(route => urlMatcher(route, url)).map(route => route); if (matchedURLs.length === 0) { return { error: "No matched URL" }; } else { urlsWithMethods = matchedURLs.filter(url => routes[url][method]).map(url => url); if (urlsWithMethods.length === 0) return { error: "No Matched Request method" }; } urlsWithMethods.forEach(route => { const multipleStubs = routes[route][method]; Object.keys(multipleStubs).forEach(stubName => { stubs[stubName] = multipleStubs[stubName]; }) }) const { allMatchedMappings, max_weight } = getHeaderMatchedStubs(headers, stubs); Iif (allMatchedMappings.length === 0) { return { warning: "No perfect headers match" } } if (allMatchedMappings.length === 1) { //perfect match of headers const matchedFile = allMatchedMappings[0].file; return processScenario(allstubs, scenarioMap, matchedFile); } else { //have multiple matches const processedScenarios = allMatchedMappings .filter(singleFile => allstubs[singleFile.file].request.scenario) .map(singleFile => processScenario(allstubs, scenarioMap, singleFile.file)); Iif (processedScenarios.length !== 0) { const fileNames = processedScenarios .filter(processedScenario => typeof processedScenario === typeof "String") .map(files => files); if (fileNames.length === 0) { return { warning: "there was a match, but ignored as it was not fulfilling the state", tip: "if you didn't intend that to happen please remove scenario in request", possibleMatches: allMatchedMappings } } else if (fileNames.length === 1) { return fileNames[0]; } else { return { warning: "There multiple matches for this request after scenarios are applied", values: fileNames } } } const weighted_stubs = assignWeights(allMatchedMappings, max_weight); return { warning: "There multiple matches for this request", values: weighted_stubs } } }; const assignWeights = (files, max_weight) => { const ret=[]; files.forEach(file => { ret.push({file: file.file, weight:Math.ceil((file.size / max_weight) * 100) + '%'}); }) return ret; } const processScenario = (allstubs, scenarioMap, matchedFile) => { const presentScenario = allstubs[matchedFile].request.scenario; Eif (!presentScenario) return matchedFile; const presentStage = scenarioMap[presentScenario].presentState; const targetState = allstubs[matchedFile].request.targetState; if (!targetState) { //get stub match with the stage const stubState = allstubs[matchedFile].request.state; if (stubState === presentStage) { return matchedFile; } else { return { warning: "there was a match, but ignored as it was not fulfilling the state", tip: "if you didn't intend that to happen please remove scenario in request" } } } else { //increment the state of files as present state and return the response scenarioMap[presentScenario].presentState = targetState; return matchedFile; } } exports.processData = processData; exports.getMatchedStub = getMatchedStub; |