Browse Source

Expose raw http and https servers (#33)

master
Luke Childs 6 years ago
committed by GitHub
parent
commit
99159f8e6c
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
  1. 12
      README.md
  2. 52
      src/index.js
  3. 12
      test/create-test-server.js

12
README.md

@ -227,6 +227,18 @@ Type: `string`
The CA certificate to validate the server certificate against.˜ 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() #### server.listen()
Type: `function` Type: `function`

52
src/index.js

@ -9,18 +9,18 @@ const bodyParser = require('body-parser');
const createTestServer = (opts = {}) => createCert(opts.certificate) const createTestServer = (opts = {}) => createCert(opts.certificate)
.then(keys => { .then(keys => {
const app = express(); const server = express();
const server = http.createServer(app); server.http = http.createServer(server);
const sslServer = https.createServer(keys, app); server.https = https.createServer(keys, server);
app.caCert = keys.caCert; server.caCert = keys.caCert;
app.set('etag', false); server.set('etag', false);
if (opts.bodyParser !== false) { if (opts.bodyParser !== false) {
app.use(bodyParser.json(Object.assign({ limit: '1mb', type: 'application/json' }, opts.bodyParser))); server.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))); server.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))); server.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.raw(Object.assign({ limit: '1mb', type: 'application/octet-stream' }, opts.bodyParser)));
} }
const send = fn => (req, res, next) => { const send = fn => (req, res, next) => {
@ -33,8 +33,8 @@ const createTestServer = (opts = {}) => createCert(opts.certificate)
}); });
}; };
const get = app.get.bind(app); const get = server.get.bind(server);
app.get = function () { server.get = function () {
const [path, ...handlers] = [...arguments]; const [path, ...handlers] = [...arguments];
for (const handler of handlers) { for (const handler of handlers) {
@ -42,29 +42,29 @@ const createTestServer = (opts = {}) => createCert(opts.certificate)
} }
}; };
app.listen = () => Promise.all([ server.listen = () => Promise.all([
pify(server.listen.bind(server))().then(() => { pify(server.http.listen.bind(server.http))().then(() => {
app.port = server.address().port; server.port = server.http.address().port;
app.url = `http://localhost:${app.port}`; server.url = `http://localhost:${server.port}`;
}), }),
pify(sslServer.listen.bind(sslServer))().then(() => { pify(server.https.listen.bind(server.https))().then(() => {
app.sslPort = sslServer.address().port; server.sslPort = server.https.address().port;
app.sslUrl = `https://localhost:${app.sslPort}`; server.sslUrl = `https://localhost:${server.sslPort}`;
}) })
]); ]);
app.close = () => Promise.all([ server.close = () => Promise.all([
pify(server.close.bind(server))().then(() => { pify(server.http.close.bind(server.http))().then(() => {
app.port = undefined; server.port = undefined;
app.url = undefined; server.url = undefined;
}), }),
pify(sslServer.close.bind(sslServer))().then(() => { pify(server.https.close.bind(server.https))().then(() => {
app.sslPort = undefined; server.sslPort = undefined;
app.sslUrl = undefined; server.sslUrl = undefined;
}) })
]); ]);
return app.listen().then(() => app); return server.listen().then(() => server);
}); });
module.exports = createTestServer; module.exports = createTestServer;

12
test/create-test-server.js

@ -242,3 +242,15 @@ test('accepts multiple callbacks in `.get()`', async t => {
const { body } = await got(server.url + '/foo'); const { body } = await got(server.url + '/foo');
t.is(body, 'bar'); 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);
});

Loading…
Cancel
Save