diff --git a/CHANGELOG.md b/CHANGELOG.md index dc873c3..110fa9f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -5,6 +5,9 @@ This project adheres to [Semantic Versioning](http://semver.org/). ## [Unreleased][Unreleased] +Added: + +1. Add health-check endpoint (by @mironov) * * * diff --git a/src/telegramWebHook.js b/src/telegramWebHook.js index 88e2f5b..d35c649 100644 --- a/src/telegramWebHook.js +++ b/src/telegramWebHook.js @@ -136,23 +136,20 @@ class TelegramBotWebHook { debug('WebHook request headers: %j', req.headers); if (this._regex.test(req.url)) { - if (req.method === 'POST') { - req - .pipe(bl(this._parseBody)) - .on('finish', () => res.end('OK')); - } else { - // Authorized but not a POST + if (req.method !== 'POST') { debug('WebHook request isn\'t a POST'); res.statusCode = 418; // I'm a teabot! res.end(); + } else { + req + .pipe(bl(this._parseBody)) + .on('finish', () => res.end('OK')); } } else if (this._healthRegex.test(req.url)) { - // If this is a health check debug('WebHook health check passed'); res.statusCode = 200; res.end('OK'); - } else if (!this._regex.test(req.url)) { - // If there isn't token on URL + } else { debug('WebHook request unauthorized'); res.statusCode = 401; res.end(); diff --git a/test/telegram.js b/test/telegram.js index 82f2474..7a5b89a 100644 --- a/test/telegram.js +++ b/test/telegram.js @@ -133,7 +133,7 @@ describe('TelegramBot', function telegramSuite() { }); describe('WebHook', function webHookSuite() { - it('returns OK for healthz endpoint', function test(done) { + it('returns 200 OK for health endpoint', function test(done) { utils.sendWebHookRequest(webHookPort2, '/healthz', { json: false }).then(resp => { assert.equal(resp, 'OK'); return done();