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.˜
#### 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`

52
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;

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');
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