diff --git a/README.md b/README.md index 859f189..c082071 100644 --- a/README.md +++ b/README.md @@ -227,6 +227,18 @@ Type: `string` The CA certificate to validate the server certificate against.˜ +#### server.http + +Type: [`http.server`](https://nodejs.org/api/http.html#http_class_http_server) + +The underlying HTTP server instance. + +#### server.https + +Type: [`https.server`](https://nodejs.org/api/https.html#https_class_https_server) + +The underlying HTTPS server instance. + #### server.listen() Type: `function` diff --git a/src/index.js b/src/index.js index f6f4672..30b40cc 100644 --- a/src/index.js +++ b/src/index.js @@ -9,18 +9,18 @@ const bodyParser = require('body-parser'); const createTestServer = (opts = {}) => createCert(opts.certificate) .then(keys => { - const app = express(); - const server = http.createServer(app); - const sslServer = https.createServer(keys, app); - app.caCert = keys.caCert; + const server = express(); + server.http = http.createServer(server); + server.https = https.createServer(keys, server); + server.caCert = keys.caCert; - app.set('etag', false); + server.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))); + server.use(bodyParser.json(Object.assign({ limit: '1mb', type: 'application/json' }, opts.bodyParser))); + server.use(bodyParser.text(Object.assign({ limit: '1mb', type: 'text/plain' }, opts.bodyParser))); + server.use(bodyParser.urlencoded(Object.assign({ limit: '1mb', type: 'application/x-www-form-urlencoded', extended: true }, opts.bodyParser))); + server.use(bodyParser.raw(Object.assign({ limit: '1mb', type: 'application/octet-stream' }, opts.bodyParser))); } const send = fn => (req, res, next) => { @@ -33,8 +33,8 @@ const createTestServer = (opts = {}) => createCert(opts.certificate) }); }; - const get = app.get.bind(app); - app.get = function () { + const get = server.get.bind(server); + server.get = function () { const [path, ...handlers] = [...arguments]; for (const handler of handlers) { @@ -42,29 +42,29 @@ const createTestServer = (opts = {}) => createCert(opts.certificate) } }; - app.listen = () => Promise.all([ - pify(server.listen.bind(server))().then(() => { - app.port = server.address().port; - app.url = `http://localhost:${app.port}`; + server.listen = () => Promise.all([ + pify(server.http.listen.bind(server.http))().then(() => { + server.port = server.http.address().port; + server.url = `http://localhost:${server.port}`; }), - pify(sslServer.listen.bind(sslServer))().then(() => { - app.sslPort = sslServer.address().port; - app.sslUrl = `https://localhost:${app.sslPort}`; + pify(server.https.listen.bind(server.https))().then(() => { + server.sslPort = server.https.address().port; + server.sslUrl = `https://localhost:${server.sslPort}`; }) ]); - app.close = () => Promise.all([ - pify(server.close.bind(server))().then(() => { - app.port = undefined; - app.url = undefined; + server.close = () => Promise.all([ + pify(server.http.close.bind(server.http))().then(() => { + server.port = undefined; + server.url = undefined; }), - pify(sslServer.close.bind(sslServer))().then(() => { - app.sslPort = undefined; - app.sslUrl = undefined; + pify(server.https.close.bind(server.https))().then(() => { + server.sslPort = undefined; + server.sslUrl = undefined; }) ]); - return app.listen().then(() => app); + return server.listen().then(() => server); }); module.exports = createTestServer; diff --git a/test/create-test-server.js b/test/create-test-server.js index 24ded1a..74550fc 100644 --- a/test/create-test-server.js +++ b/test/create-test-server.js @@ -242,3 +242,15 @@ test('accepts multiple callbacks in `.get()`', async t => { const { body } = await got(server.url + '/foo'); t.is(body, 'bar'); }); + +test('raw http and https servers are exposed', async t => { + const server = await createTestServer(); + + t.true(server.http.listening); + t.true(server.https.listening); + + await server.close(); + + t.false(server.http.listening); + t.false(server.https.listening); +});