From 18f8df74c08dc67fc879a694f26556611cd781e6 Mon Sep 17 00:00:00 2001 From: nioc Date: Sat, 23 Nov 2019 19:37:28 +0100 Subject: [PATCH] Add timeout to outgoing webhook (default: 5000 ms) --- lib/config/config.json.dist | 1 + lib/outgoing/index.js | 1 + test/config.json | 11 +++++++++++ test/outgoing.js | 20 +++++++++++++++++++- 4 files changed, 32 insertions(+), 1 deletion(-) diff --git a/lib/config/config.json.dist b/lib/config/config.json.dist index 07fcf0f..b396cf8 100644 --- a/lib/config/config.json.dist +++ b/lib/config/config.json.dist @@ -84,6 +84,7 @@ { "code": "w1", "url": "https://domain.ltd:port/path/resource?parameter1=value1", + "timeout": 500, "strictSSL": true, "contentType": "application/json", "authMethod": "basic", diff --git a/lib/outgoing/index.js b/lib/outgoing/index.js index c3b57ce..42dba33 100644 --- a/lib/outgoing/index.js +++ b/lib/outgoing/index.js @@ -67,6 +67,7 @@ module.exports = async (logger, config, xmpp, user, destination, message, type, default: break } + options.timeout = webhook.timeout || 5000 logger.trace('Outgoing webhook options:', options) try { const { statusCode, body } = await request(options) diff --git a/test/config.json b/test/config.json index 1430465..fc3534d 100644 --- a/test/config.json +++ b/test/config.json @@ -124,6 +124,17 @@ "user": null, "password": null, "bearer": null + }, + { + "code": "timeout-error", + "url": "https://domain.ltd:port/path/timeout-error", + "timeout": 500, + "strictSSL": true, + "contentType": "application/json", + "authMethod": null, + "user": null, + "password": null, + "bearer": null } ] } diff --git a/test/outgoing.js b/test/outgoing.js index f9a810e..beff123 100644 --- a/test/outgoing.js +++ b/test/outgoing.js @@ -8,7 +8,7 @@ const nock = require('nock') const Outgoing = require('./../lib/outgoing') describe('Outgoing webhook component', () => { - let logger, config, xmppSendStub, xmpp, scope, scopeUnauthorized, scopeWithError, reqSpy + let logger, config, xmppSendStub, xmpp, scope, scopeUnauthorized, scopeWithError, scopeWithTimeout, reqSpy before('Setup', () => { // create default logger @@ -50,6 +50,12 @@ describe('Outgoing webhook component', () => { .replyWithError('error in request') scopeWithError.on('request', reqSpy) + scopeWithTimeout = nock('https://domain.ltd:port') + .post('/path/timeout-error') + .delay(1000) + .reply(200, { reply: 'This is a reply' }) + scopeWithTimeout.on('request', reqSpy) + done() }) @@ -131,4 +137,16 @@ describe('Outgoing webhook component', () => { sinon.assert.calledOnce(reqSpy) }) }) + + describe('POST with timeout', () => { + it('Should handle error and throw an exception', async () => { + try { + await Outgoing(logger, config, xmpp, 'user', 'destination', 'This a second message', 'type', 'timeout-error') + should.fail(0, 1, 'Exception not thrown') + } catch (error) { + error.message.should.equal('ESOCKETTIMEDOUT') + } + sinon.assert.calledOnce(reqSpy) + }) + }) })