Skip to content

Commit

Permalink
Merge pull request #12 from smartive/feat/body-match
Browse files Browse the repository at this point in the history
feat: Body match
  • Loading branch information
mircostraessle authored Oct 14, 2020
2 parents 80e0dae + a9e6b2f commit 0ab4176
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 10 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ node_modules
# Typescript stuff
build
coverage

.env
22 changes: 12 additions & 10 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ const { SMTPServer } = require("smtp-server");

const app = express();

const PORT = process.env.PORT || "1080";
const SMTP_PORT = process.env.SMTP_PORT || "25";
const HOST = process.env.HOST || "0.0.0.0";

let routes = [];
let calls = [];
let nextCallListeners = [];
Expand Down Expand Up @@ -51,7 +55,7 @@ const smtpServer = new SMTPServer({
},
});

smtpServer.listen("25", "0.0.0.0");
smtpServer.listen(SMTP_PORT, HOST);

const route = express.Router();
app.use(process.env.MOCK_PATH || "/mock", route);
Expand All @@ -66,7 +70,7 @@ const streamToString = (readStream) =>
route.post("/mock", (req, res) => {
console.log(`Mocking route:`, req.body);
routes = routes.filter(
(route) => route.request.match !== req.body.request.match
(route) => !(route.request.match === req.body.request.match && route.request.bodyMatch === req.body.request.bodyMatch)
);
routes.push(req.body);
res.sendStatus(204);
Expand Down Expand Up @@ -137,11 +141,12 @@ app.all("*", (req, res) => {
calls.push(call);
}

const stringifiedBody = JSON.stringify(req.body, null, 2)
for (const route of routes) {
if (new RegExp(`^${route.request.match}$`).test(req.url)) {
console.log(`Call to ${req.url} matched ${route.request.match}`);
if (new RegExp(`^${route.request.match}$`).test(req.url) && (!route.request.bodyMatch || new RegExp(`^${route.request.bodyMatch}$`, 'm').test(stringifiedBody))) {
console.log(`Call to ${req.url} matched ${route.request.match} ${route.request.bodyMatch || ''}`);
const response = route.response;
res.status(response.status);
res.status(response.status || 200);
res.setHeader("Content-Type", response.contentType || "application/json");
const body = response.bodies ? response.bodies.shift() : response.body;
res.send(response.contentType ? body : JSON.stringify(body));
Expand All @@ -154,16 +159,13 @@ app.all("*", (req, res) => {
}

const errorMessage = `Request ${req.url} didn't match any registered route.`;
console.log(errorMessage, routes);

res.status(400).send({
error: errorMessage,
url: req.url,
});
});

const port = process.env.PORT || "1080";
const host = process.env.HOST || "0.0.0.0";
app.listen(port, host, () =>
console.log(`Smart mockserver running at ${host}:${port}`)
app.listen(PORT, HOST, () =>
console.log(`Smart mockserver running at ${HOST}:${PORT}`)
);
59 changes: 59 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"description": "",
"main": "index.js",
"scripts": {
"dev": "env-cmd npm start",
"start": "node ."
},
"repository": {
Expand All @@ -29,6 +30,7 @@
},
"devDependencies": {
"@semantic-release/exec": "^5.0.0",
"env-cmd": "^10.1.0",
"semantic-release": "^15.13.16"
}
}

0 comments on commit 0ab4176

Please sign in to comment.