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 | 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 3x 12x 12x 3x 3x 3x 12x 12x 12x 12x 12x 12x 3x 12x 12x 12x 6x 6x 6x 6x 3x 6x 6x 6x 3x 12x 3x 6x 6x 6x 3x 12x 12x 14x 14x 14x 12x 3x | const fs = require("fs"); const path = require("path"); const routes = new Map(); const Header = require("./utils/header"); const scenarioMap = {}; const store = require('./utils/store'); const logger = require('./utils/log'); const init = () => { logger.info({message: "Loading routes..."}); const files = fs.readdirSync(store.functionDirectory); files.length === 0 && logger.warn({message: "No routes found"}); files.forEach((file) => { loadRoutes(file); logger.info({message: "loaded", stub: file}); }); store.routes = routes; store.scenarioMap = scenarioMap; }; const loadRoutes = (file) => { const filePath = path.join(store.functionDirectory, file); const route = require(filePath); route.name = file; try { registerRoute(route); store.addStubs(route.name, route); } catch (err) { logger.error({message: 'Failed to load', filePath, err}); } }; const registerRoute = (routeFile) => { //won't create a route if path ot method isn't defined Iif (!(routeFile.request.method && routeFile.request.urlPath)) { logger.warn({message:'urlPath or method is not defined', stub: routeFile.name}); return; }; constructScenarioMap(routeFile.request, routeFile.name); if (routes[routeFile.request.urlPath]) { //if route is already defined, add it to the map const requestTypeToStubs = routes[routeFile.request.urlPath]; Eif (requestTypeToStubs[routeFile.request.method]) { //if stubs for requested method is defined requestTypeToStubs[routeFile.request.method][ routeFile.name ] = constructHeadersObj(routeFile.request.headers); } else { requestTypeToStubs[routeFile.request.method] = getMethodToHeader( routeFile.request.headers, routeFile.name ); } } else { routes[routeFile.request.urlPath] = getRequestTypeToStub( routeFile.request, routeFile.name ); } }; const getMethodToHeader = (headers, name) => { const stubToHeaders = {}; stubToHeaders[name] = constructHeadersObj(headers); return stubToHeaders; }; const constructScenarioMap = ({ scenario, state }, name) => { Iif (scenario) { if (!state) throw name + " has defined scenario, but no state was found!"; if (!scenarioMap[scenario]) { scenarioMap[scenario] = {}; } if (!scenarioMap[scenario]['presentState']) { scenarioMap[scenario]['presentState'] = 'init'; } if (!scenarioMap[scenario]['stages']) { scenarioMap[scenario]['stages'] = {}; } if (!scenarioMap[scenario]['stages'][state]) { scenarioMap[scenario]['stages'][state] = []; } scenarioMap[scenario]['stages'][state].push(name); } } const getRequestTypeToStub = ({ headers, method }, name) => { const requestTypeToStubs = {}; requestTypeToStubs[method] = getMethodToHeader(headers, name); return requestTypeToStubs; }; const constructHeadersObj = (headers) => { const ret = []; headers && Object.keys(headers).forEach((header) => { const logicToValue = headers[header]; const logicFunc = Object.keys(logicToValue)[0]; ret.push(new Header(header, logicFunc, logicToValue[logicFunc])); }); return ret; }; module.exports = init; |