Skip to content

Commit

Permalink
Update main file
Browse files Browse the repository at this point in the history
  • Loading branch information
cenobitedk committed Jan 13, 2020
1 parent 383de1e commit 19c46db
Showing 1 changed file with 39 additions and 31 deletions.
70 changes: 39 additions & 31 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -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]
});
});
Expand All @@ -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;
}
}
Expand All @@ -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)
};
Expand All @@ -73,29 +68,42 @@ 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)
};
}
}

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();
}
Expand Down

0 comments on commit 19c46db

Please sign in to comment.