Skip to content

Commit

Permalink
Merge pull request #61 from snyk/feat/configurable-client-healthcheck
Browse files Browse the repository at this point in the history
Feat/configurable client healthcheck
  • Loading branch information
gjvis authored Jun 29, 2017
2 parents 13f7b6d + e894b14 commit 0699cc3
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
4 changes: 2 additions & 2 deletions lib/client/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module.exports = ({ port = null, config = {}, filters = {} }) => {
const { app, server } = require('../webserver')(config, port);

// IMPORTANT: defined before relay (`app.all('/*', ...`)
app.get('/healthcheck', (req, res, next) => {
app.get(config.brokerHealthcheckPath || '/healthcheck', (req, res) => {
return res.status(200).json({ ok: true });
});

Expand All @@ -30,7 +30,7 @@ module.exports = ({ port = null, config = {}, filters = {} }) => {
close: done => {
logger.info('closing');
server.close();
io.destroy(done || (() => debug('closed')));
io.destroy(done || (() => logger.info('closed')));
},
};
};
32 changes: 31 additions & 1 deletion test/functional/healthcheck.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,16 @@ test('proxy requests originating from behind the broker client', t => {
const clientPort = port();
let client = app.main({ port: clientPort });

t.plan(6);
t.plan(7);

const serverHealth = `http://localhost:${serverPort}/healthcheck`;
const connectionStatus = `http://localhost:${serverPort}/` +
`connection-status/${BROKER_TOKEN}`;
const clientHealth = `http://localhost:${clientPort}/healthcheck`;

// instantiated and connected later
let customHealthClient;

t.test('server healthcheck', t => {
request({url: serverHealth, json: true }, (err, res) => {
if (err) { return t.threw(err); }
Expand Down Expand Up @@ -95,7 +99,33 @@ test('proxy requests originating from behind the broker client', t => {
}, 20);
});

t.test('custom healthcheck endpoint', t => {
// launch second client to test custom client healthcheck
process.env.BROKER_HEALTHCHECK_PATH = '/custom/healthcheck/endpoint';
const customClientPort = port();
const customClientHealth =
`http://localhost:${customClientPort}/custom/healthcheck/endpoint`;

customHealthClient = app.main({ port: customClientPort });

server.io.once('connection', socket => {
socket.once('identify', () => {
t.test('client custom healthcheck', t => {
request({url: customClientHealth, json: true }, (err, res) => {
if (err) { return t.threw(err); }

t.equal(res.statusCode, 200, '200 statusCode');
t.equal(res.body['ok'], true, '{ ok: true } in body');
t.end();
});
});
t.end();
});
});
});

t.test('clean up', t => {
customHealthClient.close();
client.close();
setTimeout(() => {
server.close();
Expand Down

0 comments on commit 0699cc3

Please sign in to comment.