Skip to content

Commit

Permalink
Fix: Coverage and typos
Browse files Browse the repository at this point in the history
Fixed a typo, added a `gracefulClose` options for `opts` which allows servers not to have
_SIGINT_/_SIGTERM_ listeners (not recommended to use tho) and improved the coverage
  • Loading branch information
Berkmann18 committed Jan 2, 2019
1 parent 8ad4ab4 commit aff7103
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 10 deletions.
14 changes: 7 additions & 7 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ const normalizePort = (val) => {

/**
* @description Default options for {@link Server.constructor}.
* @type {{name: string, useHttps: boolean, securityOptions: Object, callback: function(Server), showPublicIP: boolean}}
* @type {{name: string, useHttps: boolean, securityOptions: Object, callback: function(Server), showPublicIP: boolean, silent: boolean, gracefulClose: boolean}}
*/
const DEFAULT_OPTS = {
name: 'Server',
Expand All @@ -37,7 +37,8 @@ const DEFAULT_OPTS = {
securityOptions: {},
callback: () => {},
showPublicIP: false,
silent: false
silent: false,
gracefulClose: true
};

/**
Expand All @@ -57,7 +58,7 @@ const getEnv = (app) => {
* @returns {(http.Server|https.Server|http2.Server)} HTTP* server
*/
const createServer = (instance) => {
if (instance._usesHttp2) return require('http2').createSecureServer(instance._options, instance._app);
if (instance._useHttp2) return require('http2').createSecureServer(instance._options, instance._app);
return instance._useHttps ?
require('https').createServer(instance._options, instance._app) :
require('http').createServer(instance._app);
Expand All @@ -72,7 +73,7 @@ class Server {
* @description Create a NodeJS HTTP(s) server.
* @param {express} associatedApp Associated express application
* @param {(string|number)} [port=(process.env.PORT || 3e3)] Port/pipe to use
* @param {{name: string, useHttps: boolean, useHttp2: boolean, securityOptions: object, callback: function(Server), showPublicIP: boolean, silent: boolean}} [opts={name: 'Server', useHttps: false, securityOptions: {}, callback: (server) => {}, showPublicIP: false, silent: false}]
* @param {{name: string, useHttps: boolean, useHttp2: boolean, securityOptions: object, callback: function(Server), showPublicIP: boolean, silent: boolean, gracefulClose: boolean}} [opts={name: 'Server', useHttps: false, securityOptions: {}, callback: (server) => {}, showPublicIP: false, silent: false, gracefulClose: true}]
* Options including the server's name, HTTPS, options needed for the HTTPs server (public keys and certificates), callback called within the <code>listen</code> event and whether it should show its public
* IP and whether it needs to be silent (<em>which won't affect the public IP log</em>).
*
Expand Down Expand Up @@ -110,8 +111,7 @@ class Server {
});
}

process.on('SIGTERM', () => this.close());
process.on('SIGINT', () => this.close());
opts.gracefulClose && process.on('SIGTERM', () => this.close()) && process.on('SIGINT', () => this.close());
}

/**
Expand Down Expand Up @@ -236,7 +236,7 @@ class Server {
* @public
*/
set options(value) {
this._serverOptions = value;
this._options = value;
}

/**
Expand Down
35 changes: 34 additions & 1 deletion test/server.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ describe('HTTP/2', (done) => {
expect(ser.options).to.deep.equal(securityOptions);
expect(ser.protocol).to.equal('https');
expect(ser.address).to.equal(`https://localhost:${port}`);
expect(ser.server.constructor.name).to.equal('Server'); //Since the class isn't exported by http2
expect(ser.server.constructor.name).to.equal('Http2SecureServer'); //Since the class isn't exported by http2
});

it('should have methods', () => {
Expand All @@ -161,4 +161,37 @@ describe('HTTP/2', (done) => {
err => console.error('getPort error:', err))
.catch(err => console.error('HTTP/2 test error:', err)).
then(done)
});

describe('Wrongs', () => {
it('should have fail on port setting', () => {
expect(() => new Server(smallApp, 'port', { silent: true, gracefulClose: false })).to.throw('Port should be >= 0 and < 65536. Received NaN');
});

it('should set bad things', () => {
let ser = new Server(smallApp, 5e3, { silent: true });
let newApp = (req, res) => console.log('res=', res);;
ser.app = newApp;
expect(ser.app).to.equal(newApp);
ser.port = 4890;
expect(ser.port).to.equal(4890);
ser.name = 'Lorem';
expect(ser.name).to.equal('Lorem');
ser.useHttps = true;
expect(ser.useHttps).to.equal(true);
ser.options = securityOptions;
expect(ser.options).to.deep.equal(securityOptions);
ser.silent = false;
expect(ser.silent).to.equal(false);
ser.useHttp2 = true;
expect(ser.useHttp2).to.equal(true);
});

it('should alert', () => {
let port = 5000;
// s0 = new Server(smallApp, port, { silent: true });

expect(() => new Server(smallApp, port, { silent: true, gracefulClose: false })).to.throw(`${port} is already in use`);

});
});
13 changes: 11 additions & 2 deletions test/stdout.test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
const stdout = require('test-console').stdout,
expect = require('chai').expect;
const Server = require('../index');
const Server = require('../index'),
{ use } = require('../src/utils');

/**
* Creates an application for a given server.
Expand Down Expand Up @@ -77,4 +78,12 @@ describe('Initial output', () => {
.then(closed => expect(closed).to.equal(true) && done())
.catch(err => console.log('Closing error:', err));
});
});
});

describe('Setting', () => {
it('should reveal its public ip', () => {
let port = 4567;
const output = stdout.inspectSync(() => server = new Server(smallApp, prompt, { silent: true, showPublicIP: true }));
expect(output).to.deep.equal([`Public IP: http://localhost:${port}`]);
});
})

0 comments on commit aff7103

Please sign in to comment.