Browse Source

Reorganise code for readability

pull/33/head
Luke Childs 6 years ago
parent
commit
6b146d25dc
  1. 36
      src/index.js

36
src/index.js

@ -10,9 +10,19 @@ const bodyParser = require('body-parser');
const createTestServer = (opts = {}) => createCert(opts.certificate)
.then(keys => {
const app = express();
const get = app.get.bind(app);
const server = http.createServer(app);
const sslServer = https.createServer(keys, app);
app.caCert = keys.caCert;
app.set('etag', false);
if (opts.bodyParser !== false) {
app.use(bodyParser.json(Object.assign({ limit: '1mb', type: 'application/json' }, opts.bodyParser)));
app.use(bodyParser.text(Object.assign({ limit: '1mb', type: 'text/plain' }, opts.bodyParser)));
app.use(bodyParser.urlencoded(Object.assign({ limit: '1mb', type: 'application/x-www-form-urlencoded', extended: true }, opts.bodyParser)));
app.use(bodyParser.raw(Object.assign({ limit: '1mb', type: 'application/octet-stream' }, opts.bodyParser)));
}
const send = fn => (req, res, next) => {
const cb = typeof fn === 'function' ? fn(req, res, next) : fn;
@ -23,16 +33,14 @@ const createTestServer = (opts = {}) => createCert(opts.certificate)
});
};
app.set('etag', false);
if (opts.bodyParser !== false) {
app.use(bodyParser.json(Object.assign({ limit: '1mb', type: 'application/json' }, opts.bodyParser)));
app.use(bodyParser.text(Object.assign({ limit: '1mb', type: 'text/plain' }, opts.bodyParser)));
app.use(bodyParser.urlencoded(Object.assign({ limit: '1mb', type: 'application/x-www-form-urlencoded', extended: true }, opts.bodyParser)));
app.use(bodyParser.raw(Object.assign({ limit: '1mb', type: 'application/octet-stream' }, opts.bodyParser)));
}
const get = app.get.bind(app);
app.get = function () {
const [path, ...handlers] = [...arguments];
app.caCert = keys.caCert;
for (const handler of handlers) {
get(path, send(handler));
}
};
app.listen = () => Promise.all([
pify(server.listen.bind(server))().then(() => {
@ -56,14 +64,6 @@ const createTestServer = (opts = {}) => createCert(opts.certificate)
})
]);
app.get = function () {
const [path, ...handlers] = [...arguments];
for (const handler of handlers) {
get(path, send(handler));
}
};
return app.listen().then(() => app);
});

Loading…
Cancel
Save