diff --git a/src/index.js b/src/index.js index 191316d..c47cf1d 100644 --- a/src/index.js +++ b/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); });