diff --git a/lib/index.js b/lib/index.js index ea040d3..891c447 100644 --- a/lib/index.js +++ b/lib/index.js @@ -1,26 +1,25 @@ -const pathToRegexp = require("path-to-regexp"); -const debug = require("debug")("mock-handler"); +const { pathToRegexp } = require("path-to-regexp"); +const debug = require("debug")("express-request-mock"); const cors = require("cors"); const path = require("path"); -const mockHandler = function(options) { - const { - root = "", - table = {}, - mockFolder = "", - corsOptions = { - origin: true, - credentials: true, - optionsSuccessStatus: 200 - } - } = options; +const cwd = process.cwd(); +const processConfiguration = require("./config"); +const { getArgs } = require("./tools"); + +const mockHandler = function(conf) { + const configuration = processConfiguration(conf); + + const { root, table, dir, delay, corsOptions } = configuration; const corsHandler = cors(corsOptions); const stack = []; Object.keys(table).forEach(url => { + const keys = []; stack.push({ - path: pathToRegexp(root + url), + path: pathToRegexp(root + url, keys), + keys, file: table[url] }); }); @@ -34,20 +33,16 @@ const mockHandler = function(options) { } function getResponse(match) { - const { file, args = [] } = match; - - const response = require(path.join(process.cwd(), mockFolder, file)); + const { file, args = {} } = match; + const response = require(path.join(cwd, dir, file)); if (!response) { - debug("Could not load the response file: " + file); return null; } if (typeof response === "function") { - debug("Responding with mock function: " + file); - return response.apply(this, args); + return response(args); } else { - debug("Responding with mock file: " + file); return response; } } @@ -58,7 +53,7 @@ const mockHandler = function(options) { let match; if (hasExactMatch(url)) { - debug(["Matched exact url:", method, url].join(" ")); + debug(`Matched exact url: ${method} ${url}`); match = { file: getExactMatch(url) }; @@ -73,10 +68,10 @@ const mockHandler = function(options) { } if (regexMatch) { - debug(["Matched url:", method, url].join(" ")); + debug(`Matched url: ${method} ${url}`); match = { file: layer.file, - args: regexMatch.slice(1) + args: getArgs(regexMatch, layer) }; } } @@ -84,18 +79,31 @@ const mockHandler = function(options) { return match; } + function sendResponse(match, res) { + const { file } = match; + const response = getResponse(match); + if (response) { + debug( + `${ + delay ? `${delay}ms Delayed response` : "Responding" + } with mock file ${file}` + ); + setTimeout(function() { + res.setHeader("X-Mocked-Response-File", match.file); + res.setHeader("X-Mocked-Response-Delay", delay); + res.send(response); + }, delay); + } else { + debug("Could not load the response file: " + file); + } + } + return function(req, res, next) { try { if (stack.length) { const match = getMatch(req); if (match) { - corsHandler(req, res, function() { - const response = getResponse(match); - if (response) { - res.setHeader("X-Mocked-Response", match.file); - res.send(response); - } - }); + corsHandler(req, res, () => sendResponse(match, res)); } else { next(); }