From 22814c858841fa9b38d524c8aaa85445339e94e9 Mon Sep 17 00:00:00 2001 From: yago Date: Sun, 5 Jul 2015 11:37:27 +0200 Subject: [PATCH] HTTPS WebHook test --- test/index.js | 51 ++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 48 insertions(+), 3 deletions(-) diff --git a/test/index.js b/test/index.js index 327bc58..202a80f 100644 --- a/test/index.js +++ b/test/index.js @@ -1,8 +1,9 @@ -var Telegram = require('../src/telegram'); +var Telegram = require('../index'); var request = require('request'); var should = require('should'); var fs = require('fs'); +process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; var TOKEN = process.env.TEST_TELEGRAM_TOKEN; if (!TOKEN) { throw new Error('Bot token not provided'); @@ -12,6 +13,24 @@ var USERID = process.env.TEST_USER_ID || 777000; describe('Telegram', function () { + describe('#setWebHook', function () { + it('should set a webHook', function (done) { + var bot = new Telegram(TOKEN); + bot.setWebHook('216.58.210.174').then(function (resp) { + resp.should.be.exactly(true); + done(); + }); + }); + + it('should delete the webHook', function (done) { + var bot = new Telegram(TOKEN); + bot.setWebHook('').then(function (resp) { + resp.should.be.exactly(true); + done(); + }); + }); + }); + describe('#emit', function () { it('should emit a `message` on polling', function (done) { var bot = new Telegram(TOKEN); @@ -30,10 +49,10 @@ describe('Telegram', function () { bot._polling(); }); - it('should emit a `message` on WebHook', function (done) { + it('should emit a `message` on HTTP WebHook', function (done) { var bot = new Telegram(TOKEN, {webHook: true}); bot.on('message', function (msg) { - //msg.should.be.an.instanceOf(Object); + bot._webServer.close(); done(); }); var url = 'http://localhost:8443/bot'+TOKEN; @@ -47,6 +66,32 @@ describe('Telegram', function () { body: JSON.stringify({update_id: 0, message: {text: 'test'}}) }); }); + + it('should emit a `message` on HTTPS WebHook', function (done) { + var opts = { + webHook: { + port: 8443, + key: __dirname+'/../examples/key.pem', + cert: __dirname+'/../examples/crt.pem' + } + }; + var bot = new Telegram(TOKEN, opts); + bot.on('message', function (msg) { + bot._webServer.close(); + done(); + }); + var url = 'https://localhost:8443/bot'+TOKEN; + request({ + url: url, + method: 'POST', + json: true, + headers: { + "content-type": "application/json", + }, + rejectUnhauthorized: false, + body: {update_id: 0, message: {text: 'test'}} + }); + }); }); describe('#getMe', function () {