diff --git a/app.json b/app.json
index b025f3df..5c3a60be 100644
--- a/app.json
+++ b/app.json
@@ -1,21 +1,19 @@
{
- "name": "mockbin",
- "description": "Test, mock, and track HTTP requests & responses between libraries, sockets and APIs",
- "repository": "https://github.com/Kong/mockbin",
- "website": "http://mockbin.org/",
- "success_url": "/",
- "addons": [
- "openredis"
- ],
- "keywords": [
- "api",
- "bin",
- "har",
- "http",
- "json",
- "mock",
- "mocking",
- "test",
- "testing"
- ]
+ "name": "mockbin",
+ "description": "Test, mock, and track HTTP requests & responses between libraries, sockets and APIs",
+ "repository": "https://github.com/Kong/mockbin",
+ "website": "http://mockbin.org/",
+ "success_url": "/",
+ "addons": ["openredis"],
+ "keywords": [
+ "api",
+ "bin",
+ "har",
+ "http",
+ "json",
+ "mock",
+ "mocking",
+ "test",
+ "testing"
+ ]
}
diff --git a/lib/index.js b/lib/index.js
index 2e1aa81f..b3dc8835 100644
--- a/lib/index.js
+++ b/lib/index.js
@@ -1,52 +1,62 @@
-'use strict'
-
-var debug = require('debug')('mockbin')
-var express = require('express')
-var mw = require('./middleware')
-var routes = require('./routes')
-
-module.exports = function router (options) {
- var router = express.Router()
-
- var defaults = [mw.forwarded, mw.errorHandler, mw.bodyParser, null, mw.cors, mw.poweredBy, mw.negotiateContent]
-
- var endpoints = [
- { action: 'get', path: '/', route: routes.hello },
- { action: 'all', path: '/ip', route: routes.ips.one },
- { action: 'all', path: '/ips', route: routes.ips.all },
- { action: 'all', path: '/agent', route: routes.headers.agent },
- { action: 'all', path: '/status/:code/:reason?', route: routes.status },
- { action: 'all', path: '/headers', route: routes.headers.all },
- { action: 'all', path: '/header/:name', route: routes.headers.one },
- { action: 'all', path: '/header/:name/:value', route: routes.headers.set },
- { action: 'all', path: '/cookies', route: routes.cookies.all },
- { action: 'all', path: '/forwarded', route: routes.forwarded },
- { action: 'all', path: '/cookie/:name', route: routes.cookies.one },
- { action: 'all', path: '/cookie/:name/:value', route: routes.cookies.set },
- { action: 'all', path: '/redirect/:status_code/:count?', route: routes.redirect },
- { action: 'all', path: '/delay/:ms?', route: routes.delay },
- { action: 'all', path: '/stream/:chunks?', route: routes.stream },
- { action: 'all', path: '/har*', route: routes.har },
- { action: 'all', path: '/echo*', route: routes.echo },
- { action: 'all', path: '/request*', route: routes.request }
- ]
-
- endpoints.forEach(function (endpoint) {
- // add route to middleware
- defaults.splice(3, 1, endpoint.route)
-
- // assign router to action at path
- router[endpoint.action].apply(router, [endpoint.path].concat(defaults))
- })
-
- if (options && options.redis) {
- router.use('/bin', routes.bins(options.redis))
- } else {
- debug('no redis dsn provided, will not load bin routes')
- }
-
- // duplicate routes with gzip forced
- router.use('/gzip', routes.gzip, mw.compression, Object.assign(router))
-
- return router
-}
+const debug = require("debug")("mockbin");
+const express = require("express");
+const mw = require("./middleware");
+const routes = require("./routes");
+
+module.exports = function router(options) {
+ const router = express.Router();
+
+ const defaults = [
+ mw.forwarded,
+ mw.errorHandler,
+ mw.bodyParser,
+ null,
+ mw.cors,
+ mw.poweredBy,
+ mw.negotiateContent,
+ ];
+
+ const endpoints = [
+ { action: "get", path: "/", route: routes.hello },
+ { action: "all", path: "/ip", route: routes.ips.one },
+ { action: "all", path: "/ips", route: routes.ips.all },
+ { action: "all", path: "/agent", route: routes.headers.agent },
+ { action: "all", path: "/status/:code/:reason?", route: routes.status },
+ { action: "all", path: "/headers", route: routes.headers.all },
+ { action: "all", path: "/header/:name", route: routes.headers.one },
+ { action: "all", path: "/header/:name/:value", route: routes.headers.set },
+ { action: "all", path: "/cookies", route: routes.cookies.all },
+ { action: "all", path: "/forwarded", route: routes.forwarded },
+ { action: "all", path: "/cookie/:name", route: routes.cookies.one },
+ { action: "all", path: "/cookie/:name/:value", route: routes.cookies.set },
+ {
+ action: "all",
+ path: "/redirect/:status_code/:count?",
+ route: routes.redirect,
+ },
+ { action: "all", path: "/delay/:ms?", route: routes.delay },
+ { action: "all", path: "/stream/:chunks?", route: routes.stream },
+ { action: "all", path: "/har*", route: routes.har },
+ { action: "all", path: "/echo*", route: routes.echo },
+ { action: "all", path: "/request*", route: routes.request },
+ ];
+
+ endpoints.forEach((endpoint) => {
+ // add route to middleware
+ defaults.splice(3, 1, endpoint.route);
+
+ // assign router to action at path
+ router[endpoint.action].apply(router, [endpoint.path].concat(defaults));
+ });
+
+ if (options?.redis) {
+ router.use("/bin", routes.bins(options.redis));
+ } else {
+ debug("no redis dsn provided, will not load bin routes");
+ }
+
+ // duplicate routes with gzip forced
+ router.use("/gzip", routes.gzip, mw.compression, Object.assign(router));
+
+ return router;
+};
diff --git a/lib/middleware/body-parser.js b/lib/middleware/body-parser.js
index 29a7d818..8bbd0864 100644
--- a/lib/middleware/body-parser.js
+++ b/lib/middleware/body-parser.js
@@ -1,159 +1,160 @@
-var Dicer = require("dicer");
-var querystring = require("querystring");
-var contentTypeParser = require("content-type");
-var util = require("../utils");
+const Dicer = require("dicer");
+const querystring = require("querystring");
+const contentTypeParser = require("content-type");
+const util = require("../utils");
const parseContentType = (contentTypeHeader) => {
- if (!contentTypeHeader) {
- return { contentType: null };
- }
- var { type, parameters } = contentTypeParser.parse(contentTypeHeader);
- return {
- contentType: type.replace(/\+$/, ""),
- parameters,
- };
+ if (!contentTypeHeader) {
+ return { contentType: null };
+ }
+ const { type, parameters } = contentTypeParser.parse(contentTypeHeader);
+ return {
+ contentType: type.replace(/\+$/, ""),
+ parameters,
+ };
};
module.exports = (req, res, next) => {
- req.bodyChunks = [];
-
- req.on("data", (chunk) => {
- req.bodyChunks.push(chunk);
- });
-
- req.on("end", () => {
- req.rawBody = Buffer.concat(req.bodyChunks);
- req.body = req.rawBody.toString("utf8");
- req.bodySize = req.rawBody.length;
- req.jsonBody = null;
- req.formBody = null;
- req.contentType = null;
- req.multiPartSimple = {};
-
- // parse Content-Type
- var contentTypeHeader = req.headers["content-type"];
- var { contentType, parameters } = parseContentType(contentTypeHeader);
-
- if (contentType) {
- req.contentType = contentType;
- }
-
- // create HAR Object
- req.har = util.createHar(req);
- req.simple = util.createSimpleHar(req);
-
- switch (req.contentType) {
- case "application/json":
- try {
- req.jsonBody = JSON.parse(req.body);
- } catch (exception) { }
-
- next();
- break;
-
- case "application/x-www-form-urlencoded":
- req.formBody = querystring.parse(req.body);
-
- // update HAR objects
- req.simple.postData.params = req.formBody;
- req.har.log.entries[0].request.postData.params = util.objectToArray(
- req.formBody,
- );
-
- next();
- break;
-
- case "multipart/mixed":
- case "multipart/related":
- case "multipart/form-data":
- case "multipart/alternate":
- req.multiPartData = [];
- req.multiPartParams = [];
-
- // parse a file upload
- var dice = new Dicer({
- boundary: parameters.boundary,
- });
-
- dice.on("part", (part) => {
- part.on("data", (data) => {
- req.multiPartData.push(data.toString("utf8"));
- });
-
- part.on("header", (headers) => {
- var param = {};
-
- if (headers["content-disposition"]) {
- var disposition = contentTypeParser.parse(
- headers["content-disposition"][0].replace(
- "form-data",
- "form-data/text",
- ) || "form-data/text",
- );
-
- param.name = disposition.parameters.name;
-
- if (disposition.parameters.filename) {
- param.fileName = disposition.parameters.filename;
- }
- }
-
- if (headers["content-type"]) {
- var { contentType: partContentType } = parseContentType(
- headers["content-type"][0] || "application/octet-stream",
- );
-
- param.contentType = partContentType;
- }
-
- req.multiPartParams.push(param);
- });
- });
-
- dice.on("finish", () => {
- // createa a new simple object param
- req.multiPart = req.multiPartParams.map((param, index) => {
- // append value to pair
- param.value = req.multiPartData[index];
-
- switch (typeof req.multiPartSimple[param.name]) {
- case "undefined":
- req.multiPartSimple[param.name] = param.value;
- break;
-
- // array
- case "object":
- req.multiPartSimple[param.name].push(param.value);
- break;
-
- case "string":
- // this exists? must be an array, make it so
- req.multiPartSimple[param.name] = [
- req.multiPartSimple[param.name],
- ];
- req.multiPartSimple[param.name].push(param.value);
- break;
- }
-
- return param;
- });
-
- // update HAR objects
- req.simple.postData.params = req.multiPartSimple
- ? req.multiPartSimple
- : [];
- req.har.log.entries[0].request.postData.params = req.multiPart
- ? req.multiPart
- : [];
-
- next();
- });
-
- dice.write(req.body);
- break;
-
- default:
- next();
- }
- });
+ req.bodyChunks = [];
+
+ req.on("data", (chunk) => {
+ req.bodyChunks.push(chunk);
+ });
+
+ req.on("end", () => {
+ req.rawBody = Buffer.concat(req.bodyChunks);
+ req.body = req.rawBody.toString("utf8");
+ req.bodySize = req.rawBody.length;
+ req.jsonBody = null;
+ req.formBody = null;
+ req.contentType = null;
+ req.multiPartSimple = {};
+
+ // parse Content-Type
+ const contentTypeHeader = req.headers["content-type"];
+ const { contentType, parameters } = parseContentType(contentTypeHeader);
+
+ if (contentType) {
+ req.contentType = contentType;
+ }
+
+ // create HAR Object
+ req.har = util.createHar(req);
+ req.simple = util.createSimpleHar(req);
+
+ switch (req.contentType) {
+ case "application/json":
+ try {
+ req.jsonBody = JSON.parse(req.body);
+ } catch (exception) {}
+
+ next();
+ break;
+
+ case "application/x-www-form-urlencoded":
+ req.formBody = querystring.parse(req.body);
+
+ // update HAR objects
+ req.simple.postData.params = req.formBody;
+ req.har.log.entries[0].request.postData.params = util.objectToArray(
+ req.formBody,
+ );
+
+ next();
+ break;
+
+ case "multipart/mixed":
+ case "multipart/related":
+ case "multipart/form-data":
+ case "multipart/alternate": {
+ req.multiPartData = [];
+ req.multiPartParams = [];
+
+ // parse a file upload
+ const dice = new Dicer({
+ boundary: parameters.boundary,
+ });
+
+ dice.on("part", (part) => {
+ part.on("data", (data) => {
+ req.multiPartData.push(data.toString("utf8"));
+ });
+
+ part.on("header", (headers) => {
+ const param = {};
+
+ if (headers["content-disposition"]) {
+ const disposition = contentTypeParser.parse(
+ headers["content-disposition"][0].replace(
+ "form-data",
+ "form-data/text",
+ ) || "form-data/text",
+ );
+
+ param.name = disposition.parameters.name;
+
+ if (disposition.parameters.filename) {
+ param.fileName = disposition.parameters.filename;
+ }
+ }
+
+ if (headers["content-type"]) {
+ const { contentType: partContentType } = parseContentType(
+ headers["content-type"][0] || "application/octet-stream",
+ );
+
+ param.contentType = partContentType;
+ }
+
+ req.multiPartParams.push(param);
+ });
+ });
+
+ dice.on("finish", () => {
+ // createa a new simple object param
+ req.multiPart = req.multiPartParams.map((param, index) => {
+ // append value to pair
+ param.value = req.multiPartData[index];
+
+ switch (typeof req.multiPartSimple[param.name]) {
+ case "undefined":
+ req.multiPartSimple[param.name] = param.value;
+ break;
+
+ // array
+ case "object":
+ req.multiPartSimple[param.name].push(param.value);
+ break;
+
+ case "string":
+ // this exists? must be an array, make it so
+ req.multiPartSimple[param.name] = [
+ req.multiPartSimple[param.name],
+ ];
+ req.multiPartSimple[param.name].push(param.value);
+ break;
+ }
+
+ return param;
+ });
+
+ // update HAR objects
+ req.simple.postData.params = req.multiPartSimple
+ ? req.multiPartSimple
+ : [];
+ req.har.log.entries[0].request.postData.params = req.multiPart
+ ? req.multiPart
+ : [];
+
+ next();
+ });
+
+ dice.write(req.body);
+ break;
+ }
+
+ default:
+ next();
+ }
+ });
};
diff --git a/lib/middleware/compression.js b/lib/middleware/compression.js
index d9b86117..005495f4 100644
--- a/lib/middleware/compression.js
+++ b/lib/middleware/compression.js
@@ -1,10 +1,6 @@
-'use strict'
-
-var compression = require('compression')
+const compression = require("compression");
module.exports = compression({
- threshold: 1,
- filter: function () {
- return true
- }
-})
+ threshold: 1,
+ filter: () => true,
+});
diff --git a/lib/middleware/cors.js b/lib/middleware/cors.js
index 282257f8..ef35278c 100644
--- a/lib/middleware/cors.js
+++ b/lib/middleware/cors.js
@@ -1,31 +1,29 @@
-'use strict'
+module.exports = (req, res, next) => {
+ // default values
+ const cors = {
+ origin: "*",
+ headers: Object.keys(req.headers).join(),
+ methods: req.method,
+ };
-module.exports = function (req, res, next) {
- // default values
- var cors = {
- origin: '*',
- headers: Object.keys(req.headers).join(),
- methods: req.method
- }
+ if (req.headers.origin) {
+ cors.origin = req.headers.origin;
+ }
- if (req.headers.origin) {
- cors.origin = req.headers.origin
- }
+ if (req.headers["access-control-request-headers"]) {
+ cors.headers = req.headers["access-control-request-headers"];
+ }
- if (req.headers['access-control-request-headers']) {
- cors.headers = req.headers['access-control-request-headers']
- }
+ if (req.headers["access-control-request-method"]) {
+ cors.methods = req.headers["access-control-request-method"];
+ }
- if (req.headers['access-control-request-method']) {
- cors.methods = req.headers['access-control-request-method']
- }
+ res.set({
+ "Access-Control-Allow-Origin": cors.origin,
+ "Access-Control-Allow-Methods": cors.methods,
+ "Access-Control-Allow-Headers": cors.headers,
+ "Access-Control-Allow-Credentials": "true",
+ });
- res.set({
- 'Access-Control-Allow-Origin': cors.origin,
- 'Access-Control-Allow-Methods': cors.methods,
- 'Access-Control-Allow-Headers': cors.headers,
- 'Access-Control-Allow-Credentials': 'true'
- })
-
- next()
-}
+ next();
+};
diff --git a/lib/middleware/error-handler.js b/lib/middleware/error-handler.js
index 22df697c..ffa13b0f 100644
--- a/lib/middleware/error-handler.js
+++ b/lib/middleware/error-handler.js
@@ -1,11 +1,9 @@
-'use strict'
+const debug = require("debug")("mockbin");
-var debug = require('debug')('mockbin')
+module.exports = (err, req, res, next) => {
+ debug(err);
-module.exports = function (err, req, res, next) {
- debug(err)
+ res.status(err.status || 500).view = "error";
- res.status(err.status || 500).view = 'error'
-
- next()
-}
+ next();
+};
diff --git a/lib/middleware/forwarded.js b/lib/middleware/forwarded.js
index 84024354..27301d12 100644
--- a/lib/middleware/forwarded.js
+++ b/lib/middleware/forwarded.js
@@ -1,5 +1,3 @@
-'use strict'
+const forwarded = require("forwarded-http/lib/middleware");
-var forwarded = require('forwarded-http/lib/middleware')
-
-module.exports = forwarded({ allowPrivate: true })
+module.exports = forwarded({ allowPrivate: true });
diff --git a/lib/middleware/index.js b/lib/middleware/index.js
index d4afe496..e0a6c6e1 100644
--- a/lib/middleware/index.js
+++ b/lib/middleware/index.js
@@ -1,9 +1,5 @@
-'use strict'
+const changeCase = require("change-case");
-var changeCase = require('change-case')
-
-module.exports = require('require-directory')(module, {
- rename: function (name) {
- return changeCase.camelCase(name)
- }
-})
+module.exports = require("require-directory")(module, {
+ rename: (name) => changeCase.camelCase(name),
+});
diff --git a/lib/middleware/negotiate-content.js b/lib/middleware/negotiate-content.js
index 849b2344..d5adc08d 100644
--- a/lib/middleware/negotiate-content.js
+++ b/lib/middleware/negotiate-content.js
@@ -1,84 +1,90 @@
-'use strict'
-
-var moment = require('moment')
-var XML = require('xmlbuilder')
-var YAML = require('yamljs')
-
-module.exports = function (req, res, next) {
- res.bodyXmlObj = {
- response: res.body
- }
-
- // am I pretty?
- var spaces = req.headers['x-pretty-print'] ? parseInt(req.headers['x-pretty-print'], 10) : 2
-
- var xmlOpts = {
- pretty: (spaces > 0),
- indent: new Array(spaces).join(' '),
- newline: '\n'
- }
-
- function YAMLResponse () {
- if (typeof res.body === 'string') {
- return res.send(res.body)
- }
-
- res.send(YAML.stringify(res.body, 6, spaces))
- }
-
- function JSONResponse () {
- req.app.set('json spaces', spaces)
-
- res.jsonp(res.body)
- }
-
- function XMLResponse () {
- res.send(XML.create(res.bodyXmlObj || res.body, { allowSurrogateChars: true }).end(xmlOpts))
- }
-
- function HTMLResponse () {
- req.app.locals.moment = moment
- XML.create(res.bodyXmlObj, { allowSurrogateChars: true })
- res.render(res.view || 'default', {
- req: req,
- res: res,
- data: {
- raw: res.body,
- yaml: YAML.stringify(res.body, res.yamlInline || 3, 2),
-
- json: JSON.stringify(res.body, null, 2),
-
- xml: XML.create(res.bodyXmlObj || res.body, { allowSurrogateChars: true }).end(xmlOpts)
- }
- })
- }
-
- res.format({
- 'application/json': JSONResponse,
- 'text/json': JSONResponse,
- 'text/x-json': JSONResponse,
- 'application/x-json': JSONResponse,
-
- 'text/javascript': JSONResponse,
- 'application/javascript': JSONResponse,
- 'application/x-javascript': JSONResponse,
-
- 'text/xml': XMLResponse,
- 'application/xml': XMLResponse,
- 'application/x-xml': XMLResponse,
-
- 'text/html': HTMLResponse,
- 'application/xhtml+xml': HTMLResponse,
-
- 'text/yaml': YAMLResponse,
- 'text/x-yaml': YAMLResponse,
- 'application/yaml': YAMLResponse,
- 'application/x-yaml': YAMLResponse,
-
- 'text/plain': YAMLResponse,
-
- default: JSONResponse
- })
-
- next()
-}
+const moment = require("moment");
+const XML = require("xmlbuilder");
+const YAML = require("yamljs");
+
+module.exports = (req, res, next) => {
+ res.bodyXmlObj = {
+ response: res.body,
+ };
+
+ // am I pretty?
+ const spaces = req.headers["x-pretty-print"]
+ ? parseInt(req.headers["x-pretty-print"], 10)
+ : 2;
+
+ const xmlOpts = {
+ pretty: spaces > 0,
+ indent: new Array(spaces).join(" "),
+ newline: "\n",
+ };
+
+ function YAMLResponse() {
+ if (typeof res.body === "string") {
+ return res.send(res.body);
+ }
+
+ res.send(YAML.stringify(res.body, 6, spaces));
+ }
+
+ function JSONResponse() {
+ req.app.set("json spaces", spaces);
+
+ res.jsonp(res.body);
+ }
+
+ function XMLResponse() {
+ res.send(
+ XML.create(res.bodyXmlObj || res.body, { allowSurrogateChars: true }).end(
+ xmlOpts,
+ ),
+ );
+ }
+
+ function HTMLResponse() {
+ req.app.locals.moment = moment;
+ XML.create(res.bodyXmlObj, { allowSurrogateChars: true });
+ res.render(res.view || "default", {
+ req: req,
+ res: res,
+ data: {
+ raw: res.body,
+ yaml: YAML.stringify(res.body, res.yamlInline || 3, 2),
+
+ json: JSON.stringify(res.body, null, 2),
+
+ xml: XML.create(res.bodyXmlObj || res.body, {
+ allowSurrogateChars: true,
+ }).end(xmlOpts),
+ },
+ });
+ }
+
+ res.format({
+ "application/json": JSONResponse,
+ "text/json": JSONResponse,
+ "text/x-json": JSONResponse,
+ "application/x-json": JSONResponse,
+
+ "text/javascript": JSONResponse,
+ "application/javascript": JSONResponse,
+ "application/x-javascript": JSONResponse,
+
+ "text/xml": XMLResponse,
+ "application/xml": XMLResponse,
+ "application/x-xml": XMLResponse,
+
+ "text/html": HTMLResponse,
+ "application/xhtml+xml": HTMLResponse,
+
+ "text/yaml": YAMLResponse,
+ "text/x-yaml": YAMLResponse,
+ "application/yaml": YAMLResponse,
+ "application/x-yaml": YAMLResponse,
+
+ "text/plain": YAMLResponse,
+
+ default: JSONResponse,
+ });
+
+ next();
+};
diff --git a/lib/middleware/not-found-handler.js b/lib/middleware/not-found-handler.js
index 522aa36d..cf23b41f 100644
--- a/lib/middleware/not-found-handler.js
+++ b/lib/middleware/not-found-handler.js
@@ -1,14 +1,12 @@
-'use strict'
+module.exports = (req, res, next) => {
+ res.view = 404;
-module.exports = function (req, res, next) {
- res.view = 404
+ res.status(404).body = {
+ error: {
+ code: "404",
+ message: "Not Found",
+ },
+ };
- res.status(404).body = {
- error: {
- code: '404',
- message: 'Not Found'
- }
- }
-
- next()
-}
+ next();
+};
diff --git a/lib/middleware/powered-by.js b/lib/middleware/powered-by.js
index 57849dec..0c4b54ba 100644
--- a/lib/middleware/powered-by.js
+++ b/lib/middleware/powered-by.js
@@ -1,9 +1,7 @@
-'use strict'
+module.exports = (req, res, next) => {
+ req.app.disable("x-powered-by");
-module.exports = function (req, res, next) {
- req.app.disable('x-powered-by')
+ res.set("X-Powered-By", "mockbin");
- res.set('X-Powered-By', 'mockbin')
-
- next()
-}
+ next();
+};
diff --git a/lib/routes/bins.js b/lib/routes/bins.js
index dd1f2b71..3c359460 100644
--- a/lib/routes/bins.js
+++ b/lib/routes/bins.js
@@ -1,65 +1,64 @@
-var debug = require('debug')('mockbin')
-var express = require('express')
-var mw = require('../middleware')
-var redis = require('redis')
-var routes = require('./bins/')
-var URL = require('url').URL
+const debug = require("debug")("mockbin");
+const express = require("express");
+const mw = require("../middleware");
+const redis = require("redis");
+const routes = require("./bins/");
+const URL = require("url").URL;
-module.exports = function bins (dsnStr) {
- // parse redis dsn
- var dsn = new URL(dsnStr)
+module.exports = function bins(dsnStr) {
+ // parse redis dsn
+ const dsn = new URL(dsnStr);
- this.dsn = dsn
+ this.dsn = dsn;
- // connect to redis
- this.client = redis.createClient(
- {
- host: dsn.hostname,
- port: dsn.port,
- no_ready_check: true
- })
+ // connect to redis
+ this.client = redis.createClient({
+ host: dsn.hostname,
+ port: dsn.port,
+ no_ready_check: true,
+ });
- // Disable client's AUTH command.
- this.client.auth = null
- this.client.send_command('AUTH', [dsn.username, dsn.password])
+ // Disable client's AUTH command.
+ this.client.auth = null;
+ this.client.send_command("AUTH", [dsn.username, dsn.password]);
- this.client.on('error', (err) => {
- debug('redis error:', err)
- })
+ this.client.on("error", (err) => {
+ debug("redis error:", err);
+ });
- var router = express.Router()
+ const router = express.Router();
- var defaults = [
- mw.forwarded,
- mw.errorHandler,
- mw.bodyParser,
- null,
- mw.cors,
- mw.negotiateContent
- ]
+ const defaults = [
+ mw.forwarded,
+ mw.errorHandler,
+ mw.bodyParser,
+ null,
+ mw.cors,
+ mw.negotiateContent,
+ ];
- var endpoints = [
- { action: 'get', path: '/create', route: routes.form.bind(this) },
- { action: 'post', path: '/create', route: routes.create.bind(this) },
- { action: 'get', path: '/:uuid/view', route: routes.view.bind(this) },
- { action: 'get', path: '/:uuid/sample', route: routes.sample.bind(this) },
- { action: 'get', path: '/:uuid/log', route: routes.log.bind(this) },
- {
- action: 'delete',
- path: '/:uuid/delete',
- route: routes.delete.bind(this)
- },
- { action: 'put', path: '/:uuid', route: routes.update.bind(this) },
- { action: 'all', path: '/:uuid*', route: routes.run.bind(this) }
- ]
+ const endpoints = [
+ { action: "get", path: "/create", route: routes.form.bind(this) },
+ { action: "post", path: "/create", route: routes.create.bind(this) },
+ { action: "get", path: "/:uuid/view", route: routes.view.bind(this) },
+ { action: "get", path: "/:uuid/sample", route: routes.sample.bind(this) },
+ { action: "get", path: "/:uuid/log", route: routes.log.bind(this) },
+ {
+ action: "delete",
+ path: "/:uuid/delete",
+ route: routes.delete.bind(this),
+ },
+ { action: "put", path: "/:uuid", route: routes.update.bind(this) },
+ { action: "all", path: "/:uuid*", route: routes.run.bind(this) },
+ ];
- endpoints.forEach((endpoint) => {
- // add route to middleware
- defaults.splice(3, 1, endpoint.route)
+ endpoints.forEach((endpoint) => {
+ // add route to middleware
+ defaults.splice(3, 1, endpoint.route);
- // assign router to action at path
- router[endpoint.action].apply(router, [endpoint.path].concat(defaults))
- })
+ // assign router to action at path
+ router[endpoint.action].apply(router, [endpoint.path].concat(defaults));
+ });
- return router
-}
+ return router;
+};
diff --git a/lib/routes/bins/create.js b/lib/routes/bins/create.js
index 34d7c5c7..44cee6b8 100644
--- a/lib/routes/bins/create.js
+++ b/lib/routes/bins/create.js
@@ -1,63 +1,64 @@
-'use strict'
-
-var debug = require('debug')('mockbin')
-var util = require('util')
-var uuid = require('uuid')
-var validate = require('har-validator')
+const debug = require("debug")("mockbin");
+const util = require("util");
+const uuid = require("uuid");
+const validate = require("har-validator");
module.exports = async function (req, res, next) {
- var mock = req.jsonBody
-
- // check for full HAR
- if (req.jsonBody && req.jsonBody.response) {
- mock = req.jsonBody.response
- }
-
- // exception for the web Form
- // TODO eliminate this and rely on req.simple.postData.text
- if (req.simple.postData.params && req.simple.postData.params.response) {
- try {
- mock = JSON.parse(req.simple.postData.params.response)
- } catch (e) {
- debug(e)
- }
- }
-
- // overritten by application/x-www-form-urlencoded or multipart/form-data
- if (req.simple.postData.text) {
- try {
- mock = JSON.parse(req.simple.postData.text)
- } catch (e) {
- debug(e)
- }
- }
-
- // provide optional values before validation
- mock.redirectURL = ''
- mock.bodySize = 0
- mock.headersSize = 0
-
- if (!mock.content) {
- mock.content = {}
- }
-
- mock.content.size = 0
-
- await validate.response(mock)
- .then(function () {
- var id = uuid.v4()
-
- this.client.set('bin:' + id, JSON.stringify(mock))
-
- res.view = 'redirect'
- res.status(201).location(util.format('/bin/%s', id)).body = id
- }.bind(this))
-
- .catch(function (err) {
- res.body = {
- errors: err.errors
- }
- })
-
- .then(next)
-}
+ let mock = req.jsonBody;
+
+ // check for full HAR
+ if (req.jsonBody?.response) {
+ mock = req.jsonBody.response;
+ }
+
+ // exception for the web Form
+ // TODO eliminate this and rely on req.simple.postData.text
+ if (req.simple.postData.params?.response) {
+ try {
+ mock = JSON.parse(req.simple.postData.params.response);
+ } catch (e) {
+ debug(e);
+ }
+ }
+
+ // overritten by application/x-www-form-urlencoded or multipart/form-data
+ if (req.simple.postData.text) {
+ try {
+ mock = JSON.parse(req.simple.postData.text);
+ } catch (e) {
+ debug(e);
+ }
+ }
+
+ // provide optional values before validation
+ mock.redirectURL = "";
+ mock.bodySize = 0;
+ mock.headersSize = 0;
+
+ if (!mock.content) {
+ mock.content = {};
+ }
+
+ mock.content.size = 0;
+
+ await validate
+ .response(mock)
+ .then(
+ function () {
+ const id = uuid.v4();
+
+ this.client.set(`bin:${id}`, JSON.stringify(mock));
+
+ res.view = "redirect";
+ res.status(201).location(util.format("/bin/%s", id)).body = id;
+ }.bind(this),
+ )
+
+ .catch((err) => {
+ res.body = {
+ errors: err.errors,
+ };
+ })
+
+ .then(next);
+};
diff --git a/lib/routes/bins/delete.js b/lib/routes/bins/delete.js
index 552078f7..8638612f 100644
--- a/lib/routes/bins/delete.js
+++ b/lib/routes/bins/delete.js
@@ -1,21 +1,21 @@
-var debug = require('debug')('mockbin')
+const debug = require("debug")("mockbin");
module.exports = function (req, res, next) {
- this.client.del('bin:' + req.params.uuid, (err) => {
- if (err) {
- debug(err)
+ this.client.del(`bin:${req.params.uuid}`, (err) => {
+ if (err) {
+ debug(err);
- throw err
- }
- next()
- })
+ throw err;
+ }
+ next();
+ });
- this.client.del('log:' + req.params.uuid, (err) => {
- if (err) {
- debug(err)
+ this.client.del(`log:${req.params.uuid}`, (err) => {
+ if (err) {
+ debug(err);
- throw err
- }
- next()
- })
-}
+ throw err;
+ }
+ next();
+ });
+};
diff --git a/lib/routes/bins/form.js b/lib/routes/bins/form.js
index 0ab7d7cd..5f0a258f 100644
--- a/lib/routes/bins/form.js
+++ b/lib/routes/bins/form.js
@@ -1,7 +1,5 @@
-'use strict'
+module.exports = (req, res, next) => {
+ res.view = "bin/create";
-module.exports = function (req, res, next) {
- res.view = 'bin/create'
-
- next()
-}
+ next();
+};
diff --git a/lib/routes/bins/index.js b/lib/routes/bins/index.js
index 9dc1432d..b2a403e8 100644
--- a/lib/routes/bins/index.js
+++ b/lib/routes/bins/index.js
@@ -1,3 +1 @@
-'use strict'
-
-module.exports = require('require-directory')(module)
+module.exports = require("require-directory")(module);
diff --git a/lib/routes/bins/log.js b/lib/routes/bins/log.js
index 7682d94e..6f1b27bb 100644
--- a/lib/routes/bins/log.js
+++ b/lib/routes/bins/log.js
@@ -1,35 +1,31 @@
-'use strict'
-
-var debug = require('debug')('mockbin')
-var pkg = require('../../../package.json')
+const debug = require("debug")("mockbin");
+const pkg = require("../../../package.json");
module.exports = function (req, res, next) {
- res.view = 'bin/log'
+ res.view = "bin/log";
- this.client.lrange('log:' + req.params.uuid, 0, -1, function (err, history) {
- if (err) {
- debug(err)
+ this.client.lrange(`log:${req.params.uuid}`, 0, -1, (err, history) => {
+ if (err) {
+ debug(err);
- throw err
- }
+ throw err;
+ }
- res.body = {
- log: {
- version: '1.2',
- creator: {
- name: 'mockbin.com',
- version: pkg.version
- },
- entries: []
- }
- }
+ res.body = {
+ log: {
+ version: "1.2",
+ creator: {
+ name: "mockbin.com",
+ version: pkg.version,
+ },
+ entries: [],
+ },
+ };
- if (history.length) {
- res.body.log.entries = history.map(function (request) {
- return JSON.parse(request)
- })
- }
+ if (history.length) {
+ res.body.log.entries = history.map((request) => JSON.parse(request));
+ }
- next()
- })
-}
+ next();
+ });
+};
diff --git a/lib/routes/bins/run.js b/lib/routes/bins/run.js
index 7144ee53..b193efa2 100644
--- a/lib/routes/bins/run.js
+++ b/lib/routes/bins/run.js
@@ -1,45 +1,49 @@
-'use strict'
-
-var debug = require('debug')('mockbin')
+const debug = require("debug")("mockbin");
module.exports = function (req, res, next) {
- this.client.get('bin:' + req.params.uuid, function (err, value) {
- if (err) {
- debug(err)
-
- throw err
- }
-
- if (value) {
- var har = JSON.parse(value)
-
- // log interaction & send the appropriate response based on HAR
- this.client.rpush('log:' + req.params.uuid, JSON.stringify(req.har.log.entries[0]))
- this.client.ltrim('log:' + req.params.uuid, 0, 100)
-
- // headers
- har.headers.map(function (header) {
- res.set(header.name, header.value)
- })
-
- // cookies
- har.cookies.map(function (cookie) {
- res.cookie(cookie.name, cookie.value)
- })
-
- // status
- res.httpVersion = har.httpVersion.split('/')[1]
- res.statusCode = har.status || 200
- res.statusMessage = har.statusText || 'OK'
-
- // special condition
- if (har.redirectURL !== '') {
- res.location(har.redirectURL)
- }
-
- return res.send(har.content.text ? har.content.text : null)
- }
-
- next()
- }.bind(this))
-}
+ this.client.get(
+ `bin:${req.params.uuid}`,
+ function (err, value) {
+ if (err) {
+ debug(err);
+
+ throw err;
+ }
+
+ if (value) {
+ const har = JSON.parse(value);
+
+ // log interaction & send the appropriate response based on HAR
+ this.client.rpush(
+ `log:${req.params.uuid}`,
+ JSON.stringify(req.har.log.entries[0]),
+ );
+ this.client.ltrim(`log:${req.params.uuid}`, 0, 100);
+
+ // headers
+ har.headers.map((header) => {
+ res.set(header.name, header.value);
+ });
+
+ // cookies
+ har.cookies.map((cookie) => {
+ res.cookie(cookie.name, cookie.value);
+ });
+
+ // status
+ res.httpVersion = har.httpVersion.split("/")[1];
+ res.statusCode = har.status || 200;
+ res.statusMessage = har.statusText || "OK";
+
+ // special condition
+ if (har.redirectURL !== "") {
+ res.location(har.redirectURL);
+ }
+
+ return res.send(har.content.text ? har.content.text : null);
+ }
+
+ next();
+ }.bind(this),
+ );
+};
diff --git a/lib/routes/bins/sample.js b/lib/routes/bins/sample.js
index 50a42912..40d7ddfe 100644
--- a/lib/routes/bins/sample.js
+++ b/lib/routes/bins/sample.js
@@ -1,63 +1,68 @@
-'use strict'
-
-var debug = require('debug')('mockbin')
-var util = require('util')
+const debug = require("debug")("mockbin");
+const util = require("util");
module.exports = function (req, res, next) {
- this.client.get('bin:' + req.params.uuid, function (err, value) {
- if (err) {
- debug(err)
+ this.client.get(`bin:${req.params.uuid}`, (err, value) => {
+ if (err) {
+ debug(err);
- throw err
- }
+ throw err;
+ }
- if (value) {
- res.json({
- method: 'POST',
- url: util.format('%s://%s/bin/%s', req.protocol, req.hostname, req.params.uuid),
- httpVersion: 'HTTP/1.1',
- queryString: [
- {
- name: 'foo',
- value: 'bar'
- },
- {
- name: 'foo',
- value: 'baz'
- }
- ],
- headers: [
- {
- name: 'Accept',
- value: 'application/json'
- },
- {
- name: 'Content-Type',
- value: 'application/x-www-form-urlencoded'
- }
- ],
- cookies: [
- {
- name: 'foo',
- value: 'bar'
- },
- {
- name: 'bar',
- value: 'baz'
- }
- ],
- postData: {
- mimeType: 'application/x-www-form-urlencoded',
- params: [{
- name: 'foo',
- value: 'bar'
- },
- {
- name: 'bar',
- value: 'baz'
- }]
- }
- })
- }
- })
-}
+ if (value) {
+ res.json({
+ method: "POST",
+ url: util.format(
+ "%s://%s/bin/%s",
+ req.protocol,
+ req.hostname,
+ req.params.uuid,
+ ),
+ httpVersion: "HTTP/1.1",
+ queryString: [
+ {
+ name: "foo",
+ value: "bar",
+ },
+ {
+ name: "foo",
+ value: "baz",
+ },
+ ],
+ headers: [
+ {
+ name: "Accept",
+ value: "application/json",
+ },
+ {
+ name: "Content-Type",
+ value: "application/x-www-form-urlencoded",
+ },
+ ],
+ cookies: [
+ {
+ name: "foo",
+ value: "bar",
+ },
+ {
+ name: "bar",
+ value: "baz",
+ },
+ ],
+ postData: {
+ mimeType: "application/x-www-form-urlencoded",
+ params: [
+ {
+ name: "foo",
+ value: "bar",
+ },
+ {
+ name: "bar",
+ value: "baz",
+ },
+ ],
+ },
+ });
+ }
+ });
+};
diff --git a/lib/routes/bins/update.js b/lib/routes/bins/update.js
index 6af0ad4e..ffd900c5 100644
--- a/lib/routes/bins/update.js
+++ b/lib/routes/bins/update.js
@@ -1,62 +1,62 @@
-var debug = require('debug')('mockbin')
-var util = require('util')
-var validate = require('har-validator')
+const debug = require("debug")("mockbin");
+const util = require("util");
+const validate = require("har-validator");
module.exports = function (req, res, next) {
- var id = req.params.uuid
- var mock = req.jsonBody
-
- // check for full HAR
- if (req.jsonBody && req.jsonBody.response) {
- mock = req.jsonBody.response
- }
-
- // exception for the web Form
- // TODO eliminate this and rely on req.simple.postData.text
- if (req.simple.postData.params && req.simple.postData.params.response) {
- try {
- mock = JSON.parse(req.simple.postData.params.response)
- } catch (e) {
- debug(e)
- }
- }
-
- // overritten by application/x-www-form-urlencoded or multipart/form-data
- if (req.simple.postData.text) {
- try {
- mock = JSON.parse(req.simple.postData.text)
- } catch (e) {
- debug(e)
- }
- }
-
- // provide optional values before validation
- mock.redirectURL = ''
- mock.bodySize = 0
- mock.headersSize = 0
-
- if (!mock.content) {
- mock.content = {}
- }
-
- mock.content.size = 0
-
- validate
- .response(mock)
- .then(
- function () {
- this.client.set('bin:' + id, JSON.stringify(mock))
-
- res.view = 'redirect'
- res.status(200).location(util.format('/bin/%s', id)).body = id
- }.bind(this)
- )
-
- .catch((err) => {
- res.body = {
- errors: err.errors
- }
- })
-
- .then(next)
-}
+ const id = req.params.uuid;
+ let mock = req.jsonBody;
+
+ // check for full HAR
+ if (req.jsonBody?.response) {
+ mock = req.jsonBody.response;
+ }
+
+ // exception for the web Form
+ // TODO eliminate this and rely on req.simple.postData.text
+ if (req.simple.postData.params?.response) {
+ try {
+ mock = JSON.parse(req.simple.postData.params.response);
+ } catch (e) {
+ debug(e);
+ }
+ }
+
+ // overritten by application/x-www-form-urlencoded or multipart/form-data
+ if (req.simple.postData.text) {
+ try {
+ mock = JSON.parse(req.simple.postData.text);
+ } catch (e) {
+ debug(e);
+ }
+ }
+
+ // provide optional values before validation
+ mock.redirectURL = "";
+ mock.bodySize = 0;
+ mock.headersSize = 0;
+
+ if (!mock.content) {
+ mock.content = {};
+ }
+
+ mock.content.size = 0;
+
+ validate
+ .response(mock)
+ .then(
+ function () {
+ this.client.set(`bin:${id}`, JSON.stringify(mock));
+
+ res.view = "redirect";
+ res.status(200).location(util.format("/bin/%s", id)).body = id;
+ }.bind(this),
+ )
+
+ .catch((err) => {
+ res.body = {
+ errors: err.errors,
+ };
+ })
+
+ .then(next);
+};
diff --git a/lib/routes/bins/view.js b/lib/routes/bins/view.js
index 84922ba3..338d907a 100644
--- a/lib/routes/bins/view.js
+++ b/lib/routes/bins/view.js
@@ -1,22 +1,20 @@
-'use strict'
-
-var debug = require('debug')('mockbin')
+const debug = require("debug")("mockbin");
module.exports = function (req, res, next) {
- this.client.get('bin:' + req.params.uuid, function (err, value) {
- if (err) {
- debug(err)
+ this.client.get(`bin:${req.params.uuid}`, (err, value) => {
+ if (err) {
+ debug(err);
- throw err
- }
+ throw err;
+ }
- if (value) {
- var har = JSON.parse(value)
+ if (value) {
+ const har = JSON.parse(value);
- res.view = 'bin/view'
- res.body = har
- }
+ res.view = "bin/view";
+ res.body = har;
+ }
- next()
- })
-}
+ next();
+ });
+};
diff --git a/lib/routes/cookies.js b/lib/routes/cookies.js
index e804c003..c8df4bf4 100644
--- a/lib/routes/cookies.js
+++ b/lib/routes/cookies.js
@@ -1,25 +1,27 @@
-'use strict'
-
module.exports = {
- one: function oneCookie (req, res, next) {
- var name = req.params.name.toLowerCase()
+ one: function oneCookie(req, res, next) {
+ const name = req.params.name.toLowerCase();
- res.body = req.cookies ? (req.cookies[name] ? req.cookies[name] : false) : false
+ res.body = req.cookies
+ ? req.cookies[name]
+ ? req.cookies[name]
+ : false
+ : false;
- next()
- },
+ next();
+ },
- set: function setCookie (req, res, next) {
- res.cookie(req.params.name, req.params.value)
+ set: function setCookie(req, res, next) {
+ res.cookie(req.params.name, req.params.value);
- res.body = req.params.value
+ res.body = req.params.value;
- next()
- },
+ next();
+ },
- all: function allCookies (req, res, next) {
- res.body = req.har.log.entries[0].request.cookies
+ all: function allCookies(req, res, next) {
+ res.body = req.har.log.entries[0].request.cookies;
- next()
- }
-}
+ next();
+ },
+};
diff --git a/lib/routes/delay.js b/lib/routes/delay.js
index efc1fc90..992725b3 100644
--- a/lib/routes/delay.js
+++ b/lib/routes/delay.js
@@ -1,17 +1,15 @@
-'use strict'
+module.exports = function delay(req, res, next) {
+ let delay = req.params.ms ? parseInt(req.params.ms, 10) : 200;
-module.exports = function delay (req, res, next) {
- var delay = req.params.ms ? parseInt(req.params.ms, 10) : 200
+ if (delay > 60000) {
+ delay = 60000;
+ }
- if (delay > 60000) {
- delay = 60000
- }
+ setTimeout(() => {
+ res.body = {
+ delay: delay,
+ };
- setTimeout(function () {
- res.body = {
- delay: delay
- }
-
- next()
- }, delay)
-}
+ next();
+ }, delay);
+};
diff --git a/lib/routes/echo.js b/lib/routes/echo.js
index 6cb05073..cdf10c9b 100644
--- a/lib/routes/echo.js
+++ b/lib/routes/echo.js
@@ -1,6 +1,4 @@
-'use strict'
-
-module.exports = function echo (req, res) {
- res.type(req.headers['content-type'] || 'text/plain')
- res.send(req.body || '')
-}
+module.exports = function echo(req, res) {
+ res.type(req.headers["content-type"] || "text/plain");
+ res.send(req.body || "");
+};
diff --git a/lib/routes/forwarded.js b/lib/routes/forwarded.js
index 93d6bdcf..c620d4fc 100644
--- a/lib/routes/forwarded.js
+++ b/lib/routes/forwarded.js
@@ -1,7 +1,5 @@
-'use strict'
+module.exports = function forwarded(req, res, next) {
+ res.body = req.forwarded;
-module.exports = function forwarded (req, res, next) {
- res.body = req.forwarded
-
- next()
-}
+ next();
+};
diff --git a/lib/routes/gzip.js b/lib/routes/gzip.js
index 693acea4..56b58038 100644
--- a/lib/routes/gzip.js
+++ b/lib/routes/gzip.js
@@ -1,8 +1,6 @@
-'use strict'
+module.exports = function gzip(req, res, next) {
+ // force compression
+ req.headers["accept-encoding"] = "gzip";
-module.exports = function gzip (req, res, next) {
- // force compression
- req.headers['accept-encoding'] = 'gzip'
-
- next()
-}
+ next();
+};
diff --git a/lib/routes/har.js b/lib/routes/har.js
index 376c8b1b..bc703e2d 100644
--- a/lib/routes/har.js
+++ b/lib/routes/har.js
@@ -1,10 +1,8 @@
-'use strict'
+module.exports = function har(req, res, next) {
+ res.view = "default";
+ res.yamlInline = 6;
-module.exports = function har (req, res, next) {
- res.view = 'default'
- res.yamlInline = 6
+ res.body = req.har;
- res.body = req.har
-
- next()
-}
+ next();
+};
diff --git a/lib/routes/headers.js b/lib/routes/headers.js
index cd3511a3..54daafb0 100644
--- a/lib/routes/headers.js
+++ b/lib/routes/headers.js
@@ -1,36 +1,38 @@
-'use strict'
-
module.exports = {
- one: function oneHeader (req, res, next) {
- var name = req.params.name.toLowerCase()
+ one: function oneHeader(req, res, next) {
+ const name = req.params.name.toLowerCase();
- res.body = req.headers ? (req.headers[name] ? req.headers[name] : false) : false
+ res.body = req.headers
+ ? req.headers[name]
+ ? req.headers[name]
+ : false
+ : false;
- next()
- },
+ next();
+ },
- agent: function agent (req, res, next) {
- req.params.name = 'user-agent'
+ agent: function agent(req, res, next) {
+ req.params.name = "user-agent";
- module.exports.one(req, res, next)
- },
+ module.exports.one(req, res, next);
+ },
- set: function setHeader (req, res, next) {
- res.set(req.params.name, req.params.value)
+ set: function setHeader(req, res, next) {
+ res.set(req.params.name, req.params.value);
- res.body = req.params.value
+ res.body = req.params.value;
- next()
- },
+ next();
+ },
- all: function allHeaders (req, res, next) {
- res.yamlInline = 2
+ all: function allHeaders(req, res, next) {
+ res.yamlInline = 2;
- res.body = {
- headers: req.har.log.entries[0].request.headers,
- headersSize: req.har.log.entries[0].request.headersSize
- }
+ res.body = {
+ headers: req.har.log.entries[0].request.headers,
+ headersSize: req.har.log.entries[0].request.headersSize,
+ };
- next()
- }
-}
+ next();
+ },
+};
diff --git a/lib/routes/hello.js b/lib/routes/hello.js
index 11f63fd6..6083c79f 100644
--- a/lib/routes/hello.js
+++ b/lib/routes/hello.js
@@ -1,9 +1,7 @@
-'use strict'
+module.exports = function hello(req, res, next) {
+ res.view = "index";
-module.exports = function hello (req, res, next) {
- res.view = 'index'
+ res.body = "Hello World!";
- res.body = 'Hello World!'
-
- next()
-}
+ next();
+};
diff --git a/lib/routes/index.js b/lib/routes/index.js
index 9dc1432d..b2a403e8 100644
--- a/lib/routes/index.js
+++ b/lib/routes/index.js
@@ -1,3 +1 @@
-'use strict'
-
-module.exports = require('require-directory')(module)
+module.exports = require("require-directory")(module);
diff --git a/lib/routes/ips.js b/lib/routes/ips.js
index 7f579ca2..41214bda 100644
--- a/lib/routes/ips.js
+++ b/lib/routes/ips.js
@@ -1,15 +1,13 @@
-'use strict'
-
module.exports = {
- one: function oneIP (req, res, next) {
- res.body = req.ip
+ one: function oneIP(req, res, next) {
+ res.body = req.ip;
- next()
- },
+ next();
+ },
- all: function allIPs (req, res, next) {
- res.body = req.forwarded.for
+ all: function allIPs(req, res, next) {
+ res.body = req.forwarded.for;
- next()
- }
-}
+ next();
+ },
+};
diff --git a/lib/routes/redirect.js b/lib/routes/redirect.js
index 8d0b35db..936b7e54 100644
--- a/lib/routes/redirect.js
+++ b/lib/routes/redirect.js
@@ -1,33 +1,39 @@
-'use strict'
-
-var util = require('util')
-
-module.exports = function redirect (req, res, next) {
- var count = req.params.count ? parseInt(req.params.count, 10) : 1
- var status = parseInt(req.params.status_code, 10) || 302
- var valid = [300, 301, 302, 303, 307, 308]
-
- if (count > 100) {
- count = 100
- }
-
- if (!~valid.indexOf(status)) {
- res.body = {
- error: 'invalid status code, must be one of ' + valid.join()
- }
-
- return next()
- }
-
- if (count > 0) {
- if (count === 1 && req.query.to) {
- return res.redirect(status, req.query.to)
- }
-
- return res.redirect(status, util.format('/redirect/%d/%d%s', status, count - 1, req.query.to ? '?to=' + req.query.to : ''))
- }
-
- res.body = 'redirect finished'
-
- next()
-}
+const util = require("util");
+
+module.exports = function redirect(req, res, next) {
+ let count = req.params.count ? parseInt(req.params.count, 10) : 1;
+ const status = parseInt(req.params.status_code, 10) || 302;
+ const valid = [300, 301, 302, 303, 307, 308];
+
+ if (count > 100) {
+ count = 100;
+ }
+
+ if (!~valid.indexOf(status)) {
+ res.body = {
+ error: `invalid status code, must be one of ${valid.join()}`,
+ };
+
+ return next();
+ }
+
+ if (count > 0) {
+ if (count === 1 && req.query.to) {
+ return res.redirect(status, req.query.to);
+ }
+
+ return res.redirect(
+ status,
+ util.format(
+ "/redirect/%d/%d%s",
+ status,
+ count - 1,
+ req.query.to ? `?to=${req.query.to}` : "",
+ ),
+ );
+ }
+
+ res.body = "redirect finished";
+
+ next();
+};
diff --git a/lib/routes/request.js b/lib/routes/request.js
index 5e43f6c9..e99f0073 100644
--- a/lib/routes/request.js
+++ b/lib/routes/request.js
@@ -1,9 +1,7 @@
-'use strict'
+module.exports = function rquest(req, res, next) {
+ res.yamlInline = 6;
-module.exports = function rquest (req, res, next) {
- res.yamlInline = 6
+ res.body = req.simple;
- res.body = req.simple
-
- next()
-}
+ next();
+};
diff --git a/lib/routes/status.js b/lib/routes/status.js
index 6743d4c0..ca0f2e48 100644
--- a/lib/routes/status.js
+++ b/lib/routes/status.js
@@ -1,13 +1,11 @@
-'use strict'
+module.exports = function status(req, res, next) {
+ res.statusCode = req.params.code || 200;
+ res.statusMessage = (req.params.reason || "OK").replace(/\+/g, " ");
-module.exports = function status (req, res, next) {
- res.statusCode = req.params.code || 200
- res.statusMessage = (req.params.reason || 'OK').replace(/\+/g, ' ')
+ res.body = {
+ code: res.statusCode,
+ message: res.statusMessage,
+ };
- res.body = {
- code: res.statusCode,
- message: res.statusMessage
- }
-
- next()
-}
+ next();
+};
diff --git a/lib/routes/stream.js b/lib/routes/stream.js
index 831d50ac..c1d21208 100644
--- a/lib/routes/stream.js
+++ b/lib/routes/stream.js
@@ -1,27 +1,27 @@
-'use strict'
+module.exports = function stream(req, res, next) {
+ res.set({
+ "Content-Type": "text/plain; charset=utf-8",
+ "Transfer-Encoding": "chunked",
+ });
-module.exports = function stream (req, res, next) {
- res.set({
- 'Content-Type': 'text/plain; charset=utf-8',
- 'Transfer-Encoding': 'chunked'
- })
+ // set default chunks to 10
+ let chunks = req.params.chunks ? parseInt(req.params.chunks, 10) : 10;
- // set default chunks to 10
- var chunks = req.params.chunks ? parseInt(req.params.chunks, 10) : 10
+ // max out chunks at 100
+ if (chunks > 100) {
+ chunks = 100;
+ }
- // max out chunks at 100
- if (chunks > 100) {
- chunks = 100
- }
+ let count = 1;
- var count = 1
+ while (count <= chunks) {
+ res.write(
+ `${JSON.stringify({
+ type: "stream",
+ chunk: count++,
+ })}\n`,
+ );
+ }
- while (count <= chunks) {
- res.write(JSON.stringify({
- type: 'stream',
- chunk: count++
- }) + '\n')
- }
-
- res.end()
-}
+ res.end();
+};
diff --git a/lib/utils.js b/lib/utils.js
index 8d66c1f9..97d09d37 100644
--- a/lib/utils.js
+++ b/lib/utils.js
@@ -1,94 +1,96 @@
-'use strict'
+const pkg = require("../package.json");
-var pkg = require('../package.json')
+const utils = {
+ objectToArray: (obj) => {
+ if (!obj || typeof obj !== "object") {
+ return [];
+ }
-var utils = {
- objectToArray: function (obj) {
- if (!obj || typeof obj !== 'object') {
- return []
- }
+ const results = Object.keys(obj).reduce((results, name) => {
+ results.push({
+ name: name,
+ value: obj[name],
+ });
- var results = Object.keys(obj).reduce(function (results, name) {
- results.push({
- name: name,
- value: obj[name]
- })
+ return results;
+ }, []);
- return results
- }, [])
+ return results;
+ },
- return results
- },
+ getReqHeaderSize: (req) => {
+ const keys = Object.keys(req.headers);
- getReqHeaderSize: function (req) {
- var keys = Object.keys(req.headers)
+ const values = keys.map((key) => req.headers[key]);
- var values = keys.map(function (key) {
- return req.headers[key]
- })
+ const headers =
+ req.method +
+ req.url +
+ req.versionMajor +
+ req.versionMinor +
+ keys.join() +
+ values.join();
- var headers = req.method + req.url + req.versionMajor + req.versionMinor + keys.join() + values.join()
+ // startline: [method] [url] HTTP/1.1\r\n = 12
+ // endline: \r\n = 2
+ // every header + \r\n = * 2
+ return Buffer.from(headers).length + keys.length * 2 + 12 + 2;
+ },
- // startline: [method] [url] HTTP/1.1\r\n = 12
- // endline: \r\n = 2
- // every header + \r\n = * 2
- return Buffer.from(headers).length + (keys.length * 2) + 12 + 2
- },
+ createHar: (req) => ({
+ log: {
+ version: "1.2",
+ creator: {
+ name: "mockbin.com",
+ version: pkg.version,
+ },
+ entries: [
+ {
+ startedDateTime: new Date().toISOString(),
+ clientIPAddress: req.ip,
+ request: {
+ method: req.method,
+ url: `${req.forwarded.proto}://${req.hostname}${req.originalUrl}`,
+ httpVersion: "HTTP/1.1",
+ // TODO, add cookie details
+ cookies: utils.objectToArray(req.cookies),
+ headers: utils.objectToArray(req.headers),
+ queryString: utils.objectToArray(req.query),
+ // TODO
+ postData: {
+ mimeType: req.contentType
+ ? req.contentType
+ : "application/octet-stream",
+ text: req.body,
+ params: [],
+ },
+ headersSize: utils.getReqHeaderSize(req),
+ bodySize: req.rawBody.length,
+ },
+ },
+ ],
+ },
+ }),
- createHar: function (req) {
- return {
- log: {
- version: '1.2',
- creator: {
- name: 'mockbin.com',
- version: pkg.version
- },
- entries: [{
- startedDateTime: new Date().toISOString(),
- clientIPAddress: req.ip,
- request: {
- method: req.method,
- url: req.forwarded.proto + '://' + req.hostname + req.originalUrl,
- httpVersion: 'HTTP/1.1',
- // TODO, add cookie details
- cookies: utils.objectToArray(req.cookies),
- headers: utils.objectToArray(req.headers),
- queryString: utils.objectToArray(req.query),
- // TODO
- postData: {
- mimeType: req.contentType ? req.contentType : 'application/octet-stream',
- text: req.body,
- params: []
- },
- headersSize: utils.getReqHeaderSize(req),
- bodySize: req.rawBody.length
- }
- }]
- }
- }
- },
+ createSimpleHar: (req) => ({
+ startedDateTime: new Date().toISOString(),
+ clientIPAddress: req.ip,
+ method: req.method,
+ url: `${req.forwarded.proto}://${req.hostname}${req.originalUrl}`,
+ httpVersion: "HTTP/1.1",
+ // TODO, add cookie details
+ cookies: req.cookies,
+ headers: req.headers,
+ queryString: req.query,
+ // TODO
+ postData: {
+ mimeType: req.contentType ? req.contentType : "application/octet-stream",
+ text: req.body,
+ params: [],
+ },
+ headersSize: utils.getReqHeaderSize(req),
+ bodySize: req.rawBody.length,
+ }),
+};
- createSimpleHar: function (req) {
- return {
- startedDateTime: new Date().toISOString(),
- clientIPAddress: req.ip,
- method: req.method,
- url: req.forwarded.proto + '://' + req.hostname + req.originalUrl,
- httpVersion: 'HTTP/1.1',
- // TODO, add cookie details
- cookies: req.cookies,
- headers: req.headers,
- queryString: req.query,
- // TODO
- postData: {
- mimeType: req.contentType ? req.contentType : 'application/octet-stream',
- text: req.body,
- params: []
- },
- headersSize: utils.getReqHeaderSize(req),
- bodySize: req.rawBody.length
- }
- }
-}
-
-module.exports = utils
+module.exports = utils;
diff --git a/server.js b/server.js
index b6455b04..923ec556 100644
--- a/server.js
+++ b/server.js
@@ -1,17 +1,15 @@
-'use strict'
+const app = require("./src");
+const dotenv = require("dotenv");
+const pkg = require("./package");
-var app = require('./src')
-var dotenv = require('dotenv')
-var pkg = require('./package')
+dotenv.config({ silent: true });
-dotenv.config({ silent: true })
+const options = {
+ port: process.env.MOCKBIN_PORT || pkg.config.port,
+ quiet: process.env.MOCKBIN_QUIET || pkg.config.quiet,
+ redis: process.env.MOCKBIN_REDIS || pkg.config.redis,
+};
-var options = {
- port: process.env.MOCKBIN_PORT || pkg.config.port,
- quiet: process.env.MOCKBIN_QUIET || pkg.config.quiet,
- redis: process.env.MOCKBIN_REDIS || pkg.config.redis
-}
-
-app(options, function () {
- console.info('starting server on port: %d', options.port)
-})
+app(options, () => {
+ console.info("starting server on port: %d", options.port);
+});
diff --git a/src/index.js b/src/index.js
index f94c4d06..b115a9f3 100644
--- a/src/index.js
+++ b/src/index.js
@@ -1,47 +1,45 @@
-'use strict'
-
-var compression = require('compression')
-var cookieParser = require('cookie-parser')
-var debug = require('debug')('mockbin')
-var express = require('express')
-var methodOverride = require('method-override')
-var morgan = require('morgan')
-var path = require('path')
-var router = require('../lib')
-
-module.exports = function (options, done) {
- if (!options) {
- throw Error('missing options')
- }
-
- debug('system started with options: %j', options)
-
- // setup ExpressJS
- var app = express()
-
- app.enable('view cache')
- app.enable('trust proxy')
- app.set('view engine', 'pug')
- app.set('views', path.join(__dirname, 'views'))
- app.set('jsonp callback name', '__callback')
-
- // add 3rd party middlewares
- app.use(compression())
- app.use(cookieParser())
- app.use(methodOverride('__method'))
- app.use(methodOverride('X-HTTP-Method-Override'))
- app.use('/static', express.static(path.join(__dirname, 'static')))
-
- if (options.quiet !== true) {
- app.use(morgan('dev'))
- }
-
- // magic starts here
- app.use('/', router(options))
-
- app.listen(options.port)
-
- if (typeof done === 'function') {
- done()
- }
-}
+const compression = require("compression");
+const cookieParser = require("cookie-parser");
+const debug = require("debug")("mockbin");
+const express = require("express");
+const methodOverride = require("method-override");
+const morgan = require("morgan");
+const path = require("path");
+const router = require("../lib");
+
+module.exports = (options, done) => {
+ if (!options) {
+ throw Error("missing options");
+ }
+
+ debug("system started with options: %j", options);
+
+ // setup ExpressJS
+ const app = express();
+
+ app.enable("view cache");
+ app.enable("trust proxy");
+ app.set("view engine", "pug");
+ app.set("views", path.join(__dirname, "views"));
+ app.set("jsonp callback name", "__callback");
+
+ // add 3rd party middlewares
+ app.use(compression());
+ app.use(cookieParser());
+ app.use(methodOverride("__method"));
+ app.use(methodOverride("X-HTTP-Method-Override"));
+ app.use("/static", express.static(path.join(__dirname, "static")));
+
+ if (options.quiet !== true) {
+ app.use(morgan("dev"));
+ }
+
+ // magic starts here
+ app.use("/", router(options));
+
+ app.listen(options.port);
+
+ if (typeof done === "function") {
+ done();
+ }
+};
diff --git a/src/static/bin/create.js b/src/static/bin/create.js
index 6757028a..9fab511f 100644
--- a/src/static/bin/create.js
+++ b/src/static/bin/create.js
@@ -1,199 +1,215 @@
/* globals $, hljs, FileReader */
-var sample = {
- text: {
- status: 200,
- statusText: 'OK',
- httpVersion: 'HTTP/1.1',
- headers: [
- {
- name: 'Content-Type',
- value: 'text/plain'
- }
- ],
- cookies: [],
- content: {
- mimeType: 'text/plain',
- text: 'Hello World'
- }
- },
-
- json: {
- status: 200,
- statusText: 'OK',
- httpVersion: 'HTTP/1.1',
- headers: [
- {
- name: 'Content-Type',
- value: 'application/json'
- }
- ],
- cookies: [],
- content: {
- mimeType: 'application/json',
- text: '{\n "foo": "Hello World"\n}'
- }
- },
-
- jsonp: {
- status: 200,
- statusText: 'OK',
- httpVersion: 'HTTP/1.1',
- headers: [
- {
- name: 'Content-Type',
- value: 'application/javascript'
- }
- ],
- cookies: [],
- content: {
- mimeType: 'application/javascript',
- text: 'callback({\n "foo": "Hello World"\n})'
- }
- },
-
- xml: {
- status: 200,
- statusText: 'OK',
- httpVersion: 'HTTP/1.1',
- headers: [
- {
- name: 'Content-Type',
- value: 'application/xml'
- }
- ],
- cookies: [],
- content: {
- mimeType: 'application/xml',
- text: '\n