Add timeout to outgoing webhook (default: 5000 ms)

This commit is contained in:
nioc 2019-11-23 19:37:28 +01:00
parent 003ab013d3
commit 18f8df74c0
4 changed files with 32 additions and 1 deletions

View file

@ -84,6 +84,7 @@
{
"code": "w1",
"url": "https://domain.ltd:port/path/resource?parameter1=value1",
"timeout": 500,
"strictSSL": true,
"contentType": "application/json",
"authMethod": "basic",

View file

@ -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)

View file

@ -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
}
]
}

View file

@ -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)
})
})
})