Comparing version 3.1.0 to 3.1.1
@@ -90,3 +90,2 @@ 'use strict'; | ||
// run botmaster requestListeners first | ||
// console.log(this.__serverRequestListeners); | ||
for (const path in this.__serverRequestListeners) { | ||
@@ -132,9 +131,3 @@ if (req.url.indexOf(path) === 0) { | ||
if (bot.requiresWebhook) { | ||
const webhookEndpoint = bot.webhookEndpoint.replace(/^\/|\/$/g, ''); | ||
const path = this.settings.useDefaultMountPathPrepend | ||
? `/${bot.type}/${webhookEndpoint}` | ||
: `/${webhookEndpoint}`; | ||
console.log(path) | ||
const path = this.__getBotWebhookPath(bot); | ||
this.__serverRequestListeners[path] = bot.requestListener; | ||
@@ -154,2 +147,12 @@ } | ||
__getBotWebhookPath(bot) { | ||
const webhookEndpoint = bot.webhookEndpoint.replace(/^\/|\/$/g, ''); | ||
const path = this.settings.useDefaultMountPathPrepend | ||
? `/${bot.type}/${webhookEndpoint}` | ||
: `/${webhookEndpoint}`; | ||
return path; | ||
} | ||
/** | ||
@@ -207,3 +210,3 @@ * Extract First bot of given type or provided id. | ||
if (bot.requiresWebhook) { | ||
const path = `/${bot.type}/${bot.webhookEndpoint}`; | ||
const path = this.__getBotWebhookPath(bot); | ||
delete this.__serverRequestListeners[path]; | ||
@@ -210,0 +213,0 @@ } |
{ | ||
"name": "botmaster", | ||
"version": "3.1.0", | ||
"version": "3.1.1", | ||
"description": "Framework allowing developers to write bots that are agnostic with respect to the channel used by their users (messenger, telegram etc...)", | ||
@@ -5,0 +5,0 @@ "main": "./lib/index.js", |
@@ -27,6 +27,6 @@ import test from 'ava'; | ||
const arbitraryBotMacro = (t, botSettings) => { | ||
const arbitraryBotMacro = (t, { botmasterSettings, botSettings }) => { | ||
t.plan(3); | ||
return new Promise((resolve) => { | ||
const botmaster = new Botmaster(); | ||
const botmaster = new Botmaster(botmasterSettings); | ||
@@ -40,6 +40,10 @@ botmaster.on('listening', () => { | ||
const uri = botmaster.settings.useDefaultMountPathPrepend | ||
? `http://localhost:3000/${botSettings.type}/webhook/endpoint` | ||
: 'http://localhost:3000/webhook/endpoint'; | ||
const updateToSend = { text: 'Hello world' }; | ||
const requestOptions = { | ||
method: 'POST', | ||
uri: `http://localhost:3000/${botSettings.type}/webhook/endpoint`, | ||
uri, | ||
json: updateToSend, | ||
@@ -66,19 +70,37 @@ }; | ||
test('works with an express bot', arbitraryBotMacro, { | ||
requiresWebhook: true, | ||
webhookEndpoint: 'webhook/endpoint', | ||
type: 'express', | ||
botSettings: { | ||
requiresWebhook: true, | ||
webhookEndpoint: 'webhook/endpoint', | ||
type: 'express', | ||
}, | ||
}); | ||
test('works with a koa bot', arbitraryBotMacro, { | ||
requiresWebhook: true, | ||
webhookEndpoint: 'webhook/endpoint', | ||
type: 'koa', | ||
botSettings: { | ||
requiresWebhook: true, | ||
webhookEndpoint: 'webhook/endpoint', | ||
type: 'koa', | ||
}, | ||
}); | ||
test('works with a webhook that has slash bot', arbitraryBotMacro, { | ||
requiresWebhook: true, | ||
webhookEndpoint: '/webhook/endpoint/', | ||
type: 'express', | ||
botSettings: { | ||
requiresWebhook: true, | ||
webhookEndpoint: '/webhook/endpoint/', | ||
type: 'express', | ||
}, | ||
}); | ||
// this test could also have been in constructor. As it spans over both constructor and bot adding | ||
test('should accept requests where expected when useDefaultMountPathPrepend is truthy', arbitraryBotMacro, { | ||
botmasterSettings: { | ||
useDefaultMountPathPrepend: false, | ||
}, | ||
botSettings: { | ||
requiresWebhook: true, | ||
webhookEndpoint: 'webhook/endpoint', | ||
type: 'express', | ||
}, | ||
}); | ||
test('works with an express server AND both an express and a koa bot', (t) => { | ||
@@ -85,0 +107,0 @@ t.plan(6); |
@@ -93,46 +93,2 @@ import test from 'ava'; | ||
// this test could also have been in add-bot. As it spans over both constructor and bot adding | ||
test('should accept requests where expected when useDefaultMountPathPrepend is truthy', (t) => { | ||
t.plan(3); | ||
return new Promise((resolve) => { | ||
const botmaster = new Botmaster({ | ||
useDefaultMountPathPrepend: false, | ||
}); | ||
botmaster.on('listening', () => { | ||
const bot = new MockBot({ | ||
requiresWebhook: true, | ||
webhookEndpoint: 'webhook/endpoint', | ||
type: 'express', | ||
}); | ||
botmaster.addBot(bot); | ||
t.is(Object.keys(botmaster.__serverRequestListeners).length, 1); | ||
t.is(botmaster.bots.length, 1); | ||
const updateToSend = { text: 'Hello world' }; | ||
const requestOptions = { | ||
method: 'POST', | ||
uri: 'http://localhost:3000/webhook/endpoint', | ||
json: updateToSend, | ||
}; | ||
request(requestOptions); | ||
botmaster.use({ | ||
type: 'incoming', | ||
controller: (onUpdateBot, update) => { | ||
t.deepEqual(update.raw, updateToSend); | ||
botmaster.server.close(resolve); | ||
}, | ||
}); | ||
botmaster.on('error', () => { | ||
botmaster.server.close(resolve); | ||
}); | ||
}); | ||
}); | ||
}); | ||
test('should throw and error when server and port passed in settings', (t) => { | ||
@@ -139,0 +95,0 @@ t.plan(1); |
@@ -27,6 +27,7 @@ import test from 'ava'; | ||
const arbitraryBotMacro = (t, botSettings) => { | ||
const arbitraryBotMacro = (t, { botmasterSettings, botSettings }) => { | ||
t.plan(3); | ||
console.log(botSettings); | ||
return new Promise((resolve) => { | ||
const botmaster = new Botmaster(); | ||
const botmaster = new Botmaster(botmasterSettings); | ||
@@ -60,9 +61,23 @@ botmaster.on('listening', () => { | ||
test('works with an express bot', arbitraryBotMacro, { | ||
requiresWebhook: true, | ||
webhookEndpoint: 'express', | ||
botSettings: { | ||
requiresWebhook: true, | ||
webhookEndpoint: 'express', | ||
}, | ||
}); | ||
test('works with a koa bot', arbitraryBotMacro, { | ||
requiresWebhook: true, | ||
webhookEndpoint: 'koa', | ||
botSettings: { | ||
requiresWebhook: true, | ||
webhookEndpoint: 'koa', | ||
}, | ||
}); | ||
test('Removes path if useDefaultMountPathPrepend is false', arbitraryBotMacro, { | ||
botmasterSettings: { | ||
useDefaultMountPathPrepend: false, | ||
}, | ||
botSettings: { | ||
requiresWebhook: true, | ||
webhookEndpoint: '/express/', | ||
}, | ||
}); |
324282