botium-core 1.13.0 → 1.13.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (57) hide show
  1. package/.eslintrc.js +6 -3
  2. package/dist/botium-cjs.js +305 -123
  3. package/dist/botium-cjs.js.map +1 -1
  4. package/dist/botium-es.js +323 -142
  5. package/dist/botium-es.js.map +1 -1
  6. package/package.json +17 -15
  7. package/src/Capabilities.js +2 -1
  8. package/src/containers/plugins/SimpleRestContainer.js +23 -16
  9. package/src/grid/inbound/proxy.js +2 -1
  10. package/src/helpers/RetryHelper.js +13 -7
  11. package/src/scripting/Convo.js +36 -10
  12. package/src/scripting/MatchFunctions.js +10 -0
  13. package/src/scripting/ScriptingProvider.js +106 -37
  14. package/src/scripting/logichook/LogicHookConsts.js +1 -1
  15. package/src/scripting/logichook/LogicHookUtils.js +1 -1
  16. package/src/scripting/logichook/asserter/WerAsserter.js +59 -0
  17. package/src/scripting/logichook/logichooks/UpdateCustomLogicHook.js +3 -2
  18. package/test/compiler/compilercsv.spec.js +104 -3
  19. package/test/compiler/compilerjson.spec.js +0 -2
  20. package/test/compiler/compilerxlsx.spec.js +1 -1
  21. package/test/compiler/convos/csv/utterances_liveperson2.csv +12 -0
  22. package/test/connectors/simplerest.spec.js +1012 -969
  23. package/test/convo/fillAndApplyScriptingMemory.spec.js +804 -785
  24. package/test/convo/partialconvo.spec.js +345 -339
  25. package/test/convo/retryconvo.spec.js +134 -0
  26. package/test/driver/capabilities.spec.js +156 -151
  27. package/test/logichooks/hookfromsrc.spec.js +79 -73
  28. package/test/plugins/plugins.spec.js +44 -42
  29. package/test/scripting/asserters/buttonsAsserter.spec.js +257 -240
  30. package/test/scripting/asserters/cardsAsserter.spec.js +214 -212
  31. package/test/scripting/asserters/convos/wer_threshold_nok.yml +7 -0
  32. package/test/scripting/asserters/convos/wer_threshold_ok.yml +7 -0
  33. package/test/scripting/asserters/intentConfidenceAsserter.spec.js +34 -35
  34. package/test/scripting/asserters/jsonpathAsserter.spec.js +307 -308
  35. package/test/scripting/asserters/mediaAsserter.spec.js +236 -234
  36. package/test/scripting/asserters/werAsserter.spec.js +51 -0
  37. package/test/scripting/logichooks/setClearScriptingMemory.spec.js +202 -192
  38. package/test/scripting/matching/matchingmode.spec.js +306 -258
  39. package/test/scripting/scriptingProvider.spec.js +666 -633
  40. package/test/scripting/scriptingmemory/fillScriptingMemoryFromFile.spec.js +299 -281
  41. package/test/scripting/scriptingmemory/useScriptingMemoryForAssertion.spec.js +94 -80
  42. package/test/scripting/userinputs/defaultUserInputs.spec.js +233 -127
  43. package/test/scripting/userinputs/mediaInputConvos.spec.js +409 -403
  44. package/test/scripting/utteranceexpansion/associateByIndex.spec.js +259 -0
  45. package/test/scripting/utteranceexpansion/convos/associate_utterances_by_index.json +33 -0
  46. package/test/scripting/utteranceexpansion/convos/media.convo.txt +19 -0
  47. package/test/scripting/utteranceexpansion/files/step0voice0.wav +0 -0
  48. package/test/scripting/utteranceexpansion/files/step0voice1.wav +0 -0
  49. package/test/scripting/utteranceexpansion/files/step0voice2.wav +0 -0
  50. package/test/scripting/utteranceexpansion/files/step1voice0.wav +0 -0
  51. package/test/scripting/utteranceexpansion/files/step2voice0.wav +0 -0
  52. package/test/scripting/utteranceexpansion/files/step2voice1.wav +0 -0
  53. package/test/scripting/utteranceexpansion/files/step2voice2.wav +0 -0
  54. package/test/scripting/utteranceexpansion/files/step2voice4.wav +0 -0
  55. package/test/scripting/utteranceexpansion/files/step2voice5.wav +0 -0
  56. package/test/security/allowUnsafe.spec.js +274 -268
  57. package/test/utils.spec.js +40 -38
@@ -11,15 +11,24 @@ const nock = require('nock')
11
11
  const myCapsGet = {
12
12
  [Capabilities.CONTAINERMODE]: 'simplerest',
13
13
  [Capabilities.SIMPLEREST_URL]: 'http://my-host.com/api/endpoint/{{msg.messageText}}',
14
- [Capabilities.SIMPLEREST_HEADERS_TEMPLATE]: { HEADER1: 'HEADER1VALUE', HEADER2: '{{msg.token}}' },
14
+ [Capabilities.SIMPLEREST_HEADERS_TEMPLATE]: {
15
+ HEADER1: 'HEADER1VALUE',
16
+ HEADER2: '{{msg.token}}'
17
+ },
15
18
  [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: ['$']
16
19
  }
17
20
  const myCapsPost = {
18
21
  [Capabilities.CONTAINERMODE]: 'simplerest',
19
22
  [Capabilities.SIMPLEREST_URL]: 'http://my-host.com/api/endpoint',
20
23
  [Capabilities.SIMPLEREST_METHOD]: 'POST',
21
- [Capabilities.SIMPLEREST_HEADERS_TEMPLATE]: { HEADER1: 'HEADER1VALUE', HEADER2: '{{msg.token}}' },
22
- [Capabilities.SIMPLEREST_BODY_TEMPLATE]: { BODY1: 'BODY1VALUE', BODY2: '{{msg.messageText}}' },
24
+ [Capabilities.SIMPLEREST_HEADERS_TEMPLATE]: {
25
+ HEADER1: 'HEADER1VALUE',
26
+ HEADER2: '{{msg.token}}'
27
+ },
28
+ [Capabilities.SIMPLEREST_BODY_TEMPLATE]: {
29
+ BODY1: 'BODY1VALUE',
30
+ BODY2: '{{msg.messageText}}'
31
+ },
23
32
  [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: ['$']
24
33
  }
25
34
  const myCapsFormPost = {
@@ -64,7 +73,10 @@ const myCapsConvAndStepId = {
64
73
  [Capabilities.SIMPLEREST_METHOD]: 'POST',
65
74
  [Capabilities.SIMPLEREST_CONVERSATION_ID_TEMPLATE]: '{{fnc.timestamp}}',
66
75
  [Capabilities.SIMPLEREST_STEP_ID_TEMPLATE]: '{{#fnc.random}}7{{/fnc.random}}',
67
- [Capabilities.SIMPLEREST_BODY_TEMPLATE]: { SESSION_ID: '{{botium.conversationId}}', MESSAGE_ID: '{{botium.stepId}}' },
76
+ [Capabilities.SIMPLEREST_BODY_TEMPLATE]: {
77
+ SESSION_ID: '{{botium.conversationId}}',
78
+ MESSAGE_ID: '{{botium.stepId}}'
79
+ },
68
80
  [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: ['$']
69
81
  }
70
82
 
@@ -72,7 +84,10 @@ const myCapsHookBase = {
72
84
  [Capabilities.CONTAINERMODE]: 'simplerest',
73
85
  [Capabilities.SIMPLEREST_URL]: 'http://my-host.com/api/endpoint',
74
86
  [Capabilities.SIMPLEREST_METHOD]: 'POST',
75
- [Capabilities.SIMPLEREST_BODY_TEMPLATE]: { SESSION_ID: '{{botium.conversationId}}', MESSAGE_ID: '{{botium.stepId}}' },
87
+ [Capabilities.SIMPLEREST_BODY_TEMPLATE]: {
88
+ SESSION_ID: '{{botium.conversationId}}',
89
+ MESSAGE_ID: '{{botium.stepId}}'
90
+ },
76
91
  [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: ['$']
77
92
  }
78
93
  const myCapsRequestHookFromString = Object.assign({
@@ -135,1090 +150,1118 @@ const _assertHook = async (myCaps) => {
135
150
  await container.Clean()
136
151
  }
137
152
 
138
- describe('connectors.simplerest.nock', function () {
139
- it('should build JSON GET url', async () => {
140
- const caps = {
141
- [Capabilities.CONTAINERMODE]: 'simplerest',
142
- [Capabilities.SIMPLEREST_URL]: 'http://my-host.com/api/endpoint/{{msg.messageText}}',
143
- [Capabilities.SIMPLEREST_HEADERS_TEMPLATE]: { HEADER1: 'HEADER1VALUE', HEADER2: '{{msg.token}}' },
144
- [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: ['$'],
145
- [Capabilities.SIMPLEREST_PING_URL]: 'https://mock.com/pingget',
146
- [Capabilities.SIMPLEREST_COOKIE_REPLICATION]: false
147
- }
148
- const scope = nock('https://mock.com')
149
- .get('/pingget')
150
- .reply(200, {
151
- status: 'ok'
152
- })
153
- .persist()
154
- const driver = new BotDriver(caps)
155
- const container = await driver.Build()
156
- const body = JSON.stringify({})
157
- const pingConfig = {
158
- method: 'GET',
159
- uri: 'https://mock.com/pingget',
160
- body,
161
- timeout: 10000
162
- }
163
- const responseBody = await container.pluginInstance._waitForUrlResponse(pingConfig, 2)
164
- assert.equal(responseBody, '{"status":"ok"}')
165
- scope.persist(false)
166
- })
167
- it('post ping endpoint', async () => {
168
- const caps = {
169
- [Capabilities.CONTAINERMODE]: 'simplerest',
170
- [Capabilities.SIMPLEREST_URL]: 'http://my-host.com/api/endpoint/{{msg.messageText}}',
171
- [Capabilities.SIMPLEREST_HEADERS_TEMPLATE]: { HEADER1: 'HEADER1VALUE', HEADER2: '{{msg.token}}' },
172
- [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: ['$'],
173
- [Capabilities.SIMPLEREST_PING_URL]: 'https://mock.com/pingpost',
174
- [Capabilities.SIMPLEREST_PING_RETRIES]: 2,
175
- [Capabilities.SIMPLEREST_COOKIE_REPLICATION]: false
176
- }
177
- const scope = nock('https://mock.com')
178
- .post('/pingpost', { status: 'ok?' }, null)
179
- .reply(200, {
180
- status: 'ok'
181
- })
182
- .persist()
183
- const driver = new BotDriver(caps)
184
- const container = await driver.Build()
185
- const body = JSON.stringify({ status: 'ok?' })
186
- const pingConfig = {
187
- method: 'POST',
188
- uri: 'https://mock.com/pingpost',
189
- body,
190
- timeout: 100
191
- }
192
- const responseBody = await container.pluginInstance._waitForUrlResponse(pingConfig, 2)
193
- assert.equal(responseBody, '{"status":"ok"}')
194
- scope.persist(false)
195
- })
196
- it('post stop endpoint', async () => {
197
- const caps = {
198
- [Capabilities.CONTAINERMODE]: 'simplerest',
199
- [Capabilities.SIMPLEREST_URL]: 'http://my-host.com/api/endpoint/{{msg.messageText}}',
200
- [Capabilities.SIMPLEREST_HEADERS_TEMPLATE]: { HEADER1: 'HEADER1VALUE', HEADER2: '{{msg.token}}' },
201
- [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: ['$'],
202
- [Capabilities.SIMPLEREST_STOP_URL]: 'https://mock.com/stoppost',
203
- [Capabilities.SIMPLEREST_STOP_RETRIES]: 2,
204
- [Capabilities.SIMPLEREST_STOP_VERB]: 'POST',
205
- [Capabilities.SIMPLEREST_STOP_BODY]: { status: 'ok?' },
206
- [Capabilities.SIMPLEREST_COOKIE_REPLICATION]: false
207
- }
208
- const scope = nock('https://mock.com')
209
- .post('/stoppost', { status: 'ok?' }, null)
210
- .reply(200, {
211
- status: 'ok'
212
- })
213
- .persist()
214
- const driver = new BotDriver(caps)
215
- const container = await driver.Build()
216
- const responseBody = await container.pluginInstance._makeCall('SIMPLEREST_STOP')
217
- assert.equal(responseBody.status, 'ok')
218
- scope.persist(false)
219
- })
220
- it('error case can\'t connect', async () => {
221
- const caps = {
222
- [Capabilities.CONTAINERMODE]: 'simplerest',
223
- [Capabilities.SIMPLEREST_URL]: 'http://my-host.com/api/endpoint/{{msg.messageText}}',
224
- [Capabilities.SIMPLEREST_HEADERS_TEMPLATE]: { HEADER1: 'HEADER1VALUE', HEADER2: '{{msg.token}}' },
225
- [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: ['$'],
226
- [Capabilities.SIMPLEREST_PING_URL]: 'https://mock.com/pingfail',
227
- [Capabilities.SIMPLEREST_PING_RETRIES]: 2,
228
- [Capabilities.SIMPLEREST_COOKIE_REPLICATION]: false
229
- }
230
- const scope = nock('https://mock.com')
231
- .get('/pingfail')
232
- .reply(404, {
233
- error: 'notOk'
234
- })
235
- .persist()
236
- const driver = new BotDriver(caps)
237
- const container = await driver.Build()
238
- const body = JSON.stringify({})
239
- const pingConfig = {
240
- method: 'GET',
241
- uri: 'https://mock.com/pingfail',
242
- body,
243
- timeout: 100
244
- }
245
- try {
246
- await container.pluginInstance._waitForUrlResponse(pingConfig, 2)
247
- assert.fail('expected ping error')
248
- } catch (err) {
249
- }
250
- scope.persist(false)
251
- })
252
- it('should store cookies from ping and use for user says request', async () => {
253
- const caps = {
254
- [Capabilities.CONTAINERMODE]: 'simplerest',
255
- [Capabilities.SIMPLEREST_URL]: 'https://mock2.com/endpoint',
256
- [Capabilities.SIMPLEREST_HEADERS_TEMPLATE]: { HEADER1: 'HEADER1VALUE', HEADER2: '{{msg.token}}' },
257
- [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: ['$.text'],
258
- [Capabilities.SIMPLEREST_PING_URL]: 'https://mock2.com/pingget'
259
- }
260
- const scope = nock('https://mock2.com')
261
- .get('/pingget')
262
- .reply(200, {
263
- status: 'ok'
264
- }, {
265
- 'set-cookie': 'botium=test-cookie'
266
- }).persist()
267
-
268
- const scope2 = nock('https://mock2.com', {
269
- reqheaders: {
270
- cookie: 'botium=test-cookie'
153
+ describe('connectors.simplerest', function () {
154
+ describe('nock', function () {
155
+ it('should build JSON GET url', async function () {
156
+ const caps = {
157
+ [Capabilities.CONTAINERMODE]: 'simplerest',
158
+ [Capabilities.SIMPLEREST_URL]: 'http://my-host.com/api/endpoint/{{msg.messageText}}',
159
+ [Capabilities.SIMPLEREST_HEADERS_TEMPLATE]: {
160
+ HEADER1: 'HEADER1VALUE',
161
+ HEADER2: '{{msg.token}}'
162
+ },
163
+ [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: ['$'],
164
+ [Capabilities.SIMPLEREST_PING_URL]: 'https://mock.com/pingget',
165
+ [Capabilities.SIMPLEREST_COOKIE_REPLICATION]: false
166
+ }
167
+ const scope = nock('https://mock.com')
168
+ .get('/pingget')
169
+ .reply(200, {
170
+ status: 'ok'
171
+ })
172
+ .persist()
173
+ const driver = new BotDriver(caps)
174
+ const container = await driver.Build()
175
+ const body = JSON.stringify({})
176
+ const pingConfig = {
177
+ method: 'GET',
178
+ uri: 'https://mock.com/pingget',
179
+ body,
180
+ timeout: 10000
181
+ }
182
+ const response = await container.pluginInstance._waitForUrlResponse(pingConfig, 2)
183
+ assert.equal(response.body, '{"status":"ok"}')
184
+ scope.persist(false)
185
+ })
186
+ it('post ping endpoint', async function () {
187
+ const caps = {
188
+ [Capabilities.CONTAINERMODE]: 'simplerest',
189
+ [Capabilities.SIMPLEREST_URL]: 'http://my-host.com/api/endpoint/{{msg.messageText}}',
190
+ [Capabilities.SIMPLEREST_HEADERS_TEMPLATE]: {
191
+ HEADER1: 'HEADER1VALUE',
192
+ HEADER2: '{{msg.token}}'
193
+ },
194
+ [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: ['$'],
195
+ [Capabilities.SIMPLEREST_PING_URL]: 'https://mock.com/pingpost',
196
+ [Capabilities.SIMPLEREST_PING_RETRIES]: 2,
197
+ [Capabilities.SIMPLEREST_COOKIE_REPLICATION]: false
198
+ }
199
+ const scope = nock('https://mock.com')
200
+ .post('/pingpost', { status: 'ok?' }, null)
201
+ .reply(200, {
202
+ status: 'ok'
203
+ })
204
+ .persist()
205
+ const driver = new BotDriver(caps)
206
+ const container = await driver.Build()
207
+ const body = JSON.stringify({ status: 'ok?' })
208
+ const pingConfig = {
209
+ method: 'POST',
210
+ uri: 'https://mock.com/pingpost',
211
+ body,
212
+ timeout: 100
213
+ }
214
+ const response = await container.pluginInstance._waitForUrlResponse(pingConfig, 2)
215
+ assert.equal(response.body, '{"status":"ok"}')
216
+ scope.persist(false)
217
+ })
218
+ it('post stop endpoint', async function () {
219
+ const caps = {
220
+ [Capabilities.CONTAINERMODE]: 'simplerest',
221
+ [Capabilities.SIMPLEREST_URL]: 'http://my-host.com/api/endpoint/{{msg.messageText}}',
222
+ [Capabilities.SIMPLEREST_HEADERS_TEMPLATE]: {
223
+ HEADER1: 'HEADER1VALUE',
224
+ HEADER2: '{{msg.token}}'
225
+ },
226
+ [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: ['$'],
227
+ [Capabilities.SIMPLEREST_STOP_URL]: 'https://mock.com/stoppost',
228
+ [Capabilities.SIMPLEREST_STOP_RETRIES]: 2,
229
+ [Capabilities.SIMPLEREST_STOP_VERB]: 'POST',
230
+ [Capabilities.SIMPLEREST_STOP_BODY]: { status: 'ok?' },
231
+ [Capabilities.SIMPLEREST_COOKIE_REPLICATION]: false
271
232
  }
233
+ const scope = nock('https://mock.com')
234
+ .post('/stoppost', { status: 'ok?' }, null)
235
+ .reply(200, {
236
+ status: 'ok'
237
+ })
238
+ .persist()
239
+ const driver = new BotDriver(caps)
240
+ const container = await driver.Build()
241
+ const response = await container.pluginInstance._makeCall('SIMPLEREST_STOP')
242
+ assert.equal(response.body.status, 'ok')
243
+ scope.persist(false)
272
244
  })
273
- .get('/endpoint')
274
- .reply(200, {
275
- text: 'you called me'
245
+ it('error case can\'t connect', async function () {
246
+ const caps = {
247
+ [Capabilities.CONTAINERMODE]: 'simplerest',
248
+ [Capabilities.SIMPLEREST_URL]: 'http://my-host.com/api/endpoint/{{msg.messageText}}',
249
+ [Capabilities.SIMPLEREST_HEADERS_TEMPLATE]: {
250
+ HEADER1: 'HEADER1VALUE',
251
+ HEADER2: '{{msg.token}}'
252
+ },
253
+ [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: ['$'],
254
+ [Capabilities.SIMPLEREST_PING_URL]: 'https://mock.com/pingfail',
255
+ [Capabilities.SIMPLEREST_PING_RETRIES]: 2,
256
+ [Capabilities.SIMPLEREST_COOKIE_REPLICATION]: false
257
+ }
258
+ const scope = nock('https://mock.com')
259
+ .get('/pingfail')
260
+ .reply(404, {
261
+ error: 'notOk'
262
+ })
263
+ .persist()
264
+ const driver = new BotDriver(caps)
265
+ const container = await driver.Build()
266
+ const body = JSON.stringify({})
267
+ const pingConfig = {
268
+ method: 'GET',
269
+ uri: 'https://mock.com/pingfail',
270
+ body,
271
+ timeout: 100
272
+ }
273
+ try {
274
+ await container.pluginInstance._waitForUrlResponse(pingConfig, 2)
275
+ assert.fail('expected ping error')
276
+ } catch (err) {
277
+ }
278
+ scope.persist(false)
279
+ })
280
+ it('should store cookies from ping and use for user says request', async function () {
281
+ const caps = {
282
+ [Capabilities.CONTAINERMODE]: 'simplerest',
283
+ [Capabilities.SIMPLEREST_URL]: 'https://mock2.com/endpoint',
284
+ [Capabilities.SIMPLEREST_HEADERS_TEMPLATE]: {
285
+ HEADER1: 'HEADER1VALUE',
286
+ HEADER2: '{{msg.token}}'
287
+ },
288
+ [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: ['$.text'],
289
+ [Capabilities.SIMPLEREST_PING_URL]: 'https://mock2.com/pingget'
290
+ }
291
+ const scope = nock('https://mock2.com')
292
+ .get('/pingget')
293
+ .reply(200, {
294
+ status: 'ok'
295
+ }, {
296
+ 'set-cookie': 'botium=test-cookie'
297
+ }).persist()
298
+
299
+ const scope2 = nock('https://mock2.com', {
300
+ reqheaders: {
301
+ cookie: 'botium=test-cookie'
302
+ }
276
303
  })
277
- .persist()
278
- const driver = new BotDriver(caps)
279
- const container = await driver.Build()
280
- await container.Start()
281
-
282
- await container.UserSays({ text: 'hallo' })
283
- await container.WaitBotSays()
284
-
285
- await container.Stop()
286
- await container.Clean()
287
- scope.persist(false)
288
- scope2.persist(false)
304
+ .get('/endpoint')
305
+ .reply(200, {
306
+ text: 'you called me'
307
+ })
308
+ .persist()
309
+ const driver = new BotDriver(caps)
310
+ const container = await driver.Build()
311
+ await container.Start()
312
+
313
+ await container.UserSays({ text: 'hallo' })
314
+ await container.WaitBotSays()
315
+
316
+ await container.Stop()
317
+ await container.Clean()
318
+ scope.persist(false)
319
+ scope2.persist(false)
320
+ })
289
321
  })
290
- })
291
322
 
292
- describe('connectors.simplerest.build', function () {
293
- it('should build JSON GET url', async function () {
294
- const myCaps = Object.assign({}, myCapsGet)
295
- const driver = new BotDriver(myCaps)
296
- const container = await driver.Build()
297
- assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
323
+ describe('build', function () {
324
+ it('should build JSON GET url', async function () {
325
+ const myCaps = Object.assign({}, myCapsGet)
326
+ const driver = new BotDriver(myCaps)
327
+ const container = await driver.Build()
328
+ assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
298
329
 
299
- await container.Start()
300
- const request = await container.pluginInstance._buildRequest(msg)
330
+ await container.Start()
331
+ const request = await container.pluginInstance._buildRequest(msg)
301
332
 
302
- assert.isUndefined(request.json)
303
- assert.isObject(request.headers)
304
- assert.isUndefined(request.body)
305
- assert.equal(request.headers.HEADER2, msg.token)
306
- assert.equal(request.uri, 'http://my-host.com/api/endpoint/messageText')
333
+ assert.isUndefined(request.json)
334
+ assert.isObject(request.headers)
335
+ assert.isUndefined(request.body)
336
+ assert.equal(request.headers.HEADER2, msg.token)
337
+ assert.equal(request.uri, 'http://my-host.com/api/endpoint/messageText')
307
338
 
308
- await container.Clean()
309
- })
310
- it('should build JSON GET url from encoded characters', async function () {
311
- const myCaps = Object.assign({}, myCapsGet)
312
- myCaps[Capabilities.SIMPLEREST_HEADERS_TEMPLATE] = { ORIG: '{{{msg.messageText}}}' }
313
-
314
- const myMsg = Object.assign({}, msg)
315
- myMsg.messageText = '&?äüö'
316
-
317
- const driver = new BotDriver(myCaps)
318
- const container = await driver.Build()
319
- assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
320
-
321
- await container.Start()
322
- const request = await container.pluginInstance._buildRequest(myMsg)
323
- assert.isUndefined(request.json)
324
- assert.isObject(request.headers)
325
- assert.isUndefined(request.body)
326
- assert.equal(request.headers.ORIG, myMsg.messageText)
327
- assert.equal(request.uri, 'http://my-host.com/api/endpoint/' + encodeURIComponent(myMsg.messageText))
328
-
329
- await container.Clean()
330
- })
339
+ await container.Clean()
340
+ })
341
+ it('should build JSON GET url from encoded characters', async function () {
342
+ const myCaps = Object.assign({}, myCapsGet)
343
+ myCaps[Capabilities.SIMPLEREST_HEADERS_TEMPLATE] = { ORIG: '{{{msg.messageText}}}' }
344
+
345
+ const myMsg = Object.assign({}, msg)
346
+ myMsg.messageText = '&?äüö'
347
+
348
+ const driver = new BotDriver(myCaps)
349
+ const container = await driver.Build()
350
+ assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
351
+
352
+ await container.Start()
353
+ const request = await container.pluginInstance._buildRequest(myMsg)
354
+ assert.isUndefined(request.json)
355
+ assert.isObject(request.headers)
356
+ assert.isUndefined(request.body)
357
+ assert.equal(request.headers.ORIG, myMsg.messageText)
358
+ assert.equal(request.uri, 'http://my-host.com/api/endpoint/' + encodeURIComponent(myMsg.messageText))
359
+
360
+ await container.Clean()
361
+ })
331
362
 
332
- it('should build JSON POST request body', async function () {
333
- const myCaps = Object.assign({}, myCapsPost)
334
- const driver = new BotDriver(myCaps)
335
- const container = await driver.Build()
336
- assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
363
+ it('should build JSON POST request body', async function () {
364
+ const myCaps = Object.assign({}, myCapsPost)
365
+ const driver = new BotDriver(myCaps)
366
+ const container = await driver.Build()
367
+ assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
337
368
 
338
- await container.Start()
339
- const request = await container.pluginInstance._buildRequest(msg)
369
+ await container.Start()
370
+ const request = await container.pluginInstance._buildRequest(msg)
340
371
 
341
- assert.isTrue(request.json)
342
- assert.isObject(request.headers)
343
- assert.isObject(request.body)
344
- assert.equal(request.headers.HEADER2, msg.token)
345
- assert.equal(request.body.BODY2, msg.messageText)
372
+ assert.isTrue(request.json)
373
+ assert.isObject(request.headers)
374
+ assert.isObject(request.body)
375
+ assert.equal(request.headers.HEADER2, msg.token)
376
+ assert.equal(request.body.BODY2, msg.messageText)
346
377
 
347
- await container.Clean()
348
- })
349
- it('should build JSON POST request body with special chars', async function () {
350
- const myCaps = Object.assign({}, myCapsPost)
351
- myCaps[Capabilities.SIMPLEREST_HEADERS_TEMPLATE] = { HEADER1: 'HEADER1VALUE', HEADER2: '{{#fnc.jsonify}}{{msg.token}}{{/fnc.jsonify}}' }
352
-
353
- const driver = new BotDriver(myCaps)
354
- const container = await driver.Build()
355
- assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
356
-
357
- await container.Start()
358
- const request = await container.pluginInstance._buildRequest(msgSpecial)
359
- assert.isTrue(request.json)
360
- assert.isObject(request.headers)
361
- assert.isObject(request.body)
362
- assert.equal(request.headers.HEADER2, msgSpecial.token)
363
- assert.equal(request.body.BODY2, msgSpecial.messageText)
364
-
365
- await container.Clean()
366
- })
367
- it('should build JSON POST request body from strings', async function () {
368
- const myCaps = Object.assign({}, myCapsPost)
369
- myCaps[Capabilities.SIMPLEREST_BODY_TEMPLATE] = JSON.stringify(myCaps[Capabilities.SIMPLEREST_BODY_TEMPLATE])
370
- myCaps[Capabilities.SIMPLEREST_HEADERS_TEMPLATE] = JSON.stringify(myCaps[Capabilities.SIMPLEREST_HEADERS_TEMPLATE])
378
+ await container.Clean()
379
+ })
380
+ it('should build JSON POST request body with special chars', async function () {
381
+ const myCaps = Object.assign({}, myCapsPost)
382
+ myCaps[Capabilities.SIMPLEREST_HEADERS_TEMPLATE] = {
383
+ HEADER1: 'HEADER1VALUE',
384
+ HEADER2: '{{#fnc.jsonify}}{{msg.token}}{{/fnc.jsonify}}'
385
+ }
371
386
 
372
- const driver = new BotDriver(myCaps)
373
- const container = await driver.Build()
374
- assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
387
+ const driver = new BotDriver(myCaps)
388
+ const container = await driver.Build()
389
+ assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
375
390
 
376
- await container.Start()
377
- const request = await container.pluginInstance._buildRequest(msg)
391
+ await container.Start()
392
+ const request = await container.pluginInstance._buildRequest(msgSpecial)
393
+ assert.isTrue(request.json)
394
+ assert.isObject(request.headers)
395
+ assert.isObject(request.body)
396
+ assert.equal(request.headers.HEADER2, msgSpecial.token)
397
+ assert.equal(request.body.BODY2, msgSpecial.messageText)
378
398
 
379
- assert.isTrue(request.json)
380
- assert.isObject(request.headers)
381
- assert.isObject(request.body)
382
- assert.equal(request.headers.HEADER2, msg.token)
383
- assert.equal(request.body.BODY2, msg.messageText)
399
+ await container.Clean()
400
+ })
401
+ it('should build JSON POST request body from strings', async function () {
402
+ const myCaps = Object.assign({}, myCapsPost)
403
+ myCaps[Capabilities.SIMPLEREST_BODY_TEMPLATE] = JSON.stringify(myCaps[Capabilities.SIMPLEREST_BODY_TEMPLATE])
404
+ myCaps[Capabilities.SIMPLEREST_HEADERS_TEMPLATE] = JSON.stringify(myCaps[Capabilities.SIMPLEREST_HEADERS_TEMPLATE])
384
405
 
385
- await container.Clean()
386
- })
387
- it('should build url-form-encoded POST request body', async function () {
388
- const myCaps = Object.assign({}, myCapsPost)
389
- myCaps[Capabilities.SIMPLEREST_BODY_RAW] = true
390
- myCaps[Capabilities.SIMPLEREST_HEADERS_TEMPLATE] = { 'Content-Type': 'application/x-www-form-urlencoded' }
391
- myCaps[Capabilities.SIMPLEREST_BODY_TEMPLATE] = 'BODY1=BODY1VALUE&BODY2={{msg.messageText}}'
392
-
393
- const driver = new BotDriver(myCaps)
394
- const container = await driver.Build()
395
- assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
396
-
397
- await container.Start()
398
- const request = await container.pluginInstance._buildRequest(msg)
399
- assert.isObject(request.headers)
400
- assert.isString(request.body)
401
- assert.equal(request.body, 'BODY1=BODY1VALUE&BODY2=messageText')
402
-
403
- await container.Clean()
404
- })
405
- it('should use scriptingMemory variables', async function () {
406
- process.env.SAMPLE_ENV = 'SAMPLE_ENV'
406
+ const driver = new BotDriver(myCaps)
407
+ const container = await driver.Build()
408
+ assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
407
409
 
408
- const myCaps = Object.assign({}, myCapsScriptingMemory)
409
- const driver = new BotDriver(myCaps)
410
- const container = await driver.Build()
410
+ await container.Start()
411
+ const request = await container.pluginInstance._buildRequest(msg)
411
412
 
412
- await container.Start()
413
- const request = await container.pluginInstance._buildRequest(msg)
413
+ assert.isTrue(request.json)
414
+ assert.isObject(request.headers)
415
+ assert.isObject(request.body)
416
+ assert.equal(request.headers.HEADER2, msg.token)
417
+ assert.equal(request.body.BODY2, msg.messageText)
414
418
 
415
- assert.isTrue(request.json)
416
- assert.exists(request.body)
419
+ await container.Clean()
420
+ })
421
+ it('should build url-form-encoded POST request body', async function () {
422
+ const myCaps = Object.assign({}, myCapsPost)
423
+ myCaps[Capabilities.SIMPLEREST_BODY_RAW] = true
424
+ myCaps[Capabilities.SIMPLEREST_HEADERS_TEMPLATE] = { 'Content-Type': 'application/x-www-form-urlencoded' }
425
+ myCaps[Capabilities.SIMPLEREST_BODY_TEMPLATE] = 'BODY1=BODY1VALUE&BODY2={{msg.messageText}}'
426
+
427
+ const driver = new BotDriver(myCaps)
428
+ const container = await driver.Build()
429
+ assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
430
+
431
+ await container.Start()
432
+ const request = await container.pluginInstance._buildRequest(msg)
433
+ assert.isObject(request.headers)
434
+ assert.isString(request.body)
435
+ assert.equal(request.body, 'BODY1=BODY1VALUE&BODY2=messageText')
436
+
437
+ await container.Clean()
438
+ })
439
+ it('should use scriptingMemory variables', async function () {
440
+ process.env.SAMPLE_ENV = 'SAMPLE_ENV'
417
441
 
418
- assert.exists(request.body.FUNCTION_WITHOUT_PARAM)
419
- assert.equal(request.body.FUNCTION_WITHOUT_PARAM.length, 4)
442
+ const myCaps = Object.assign({}, myCapsScriptingMemory)
443
+ const driver = new BotDriver(myCaps)
444
+ const container = await driver.Build()
420
445
 
421
- assert.exists(request.body.FUNCTION_WITH_PARAM)
422
- assert.equal(request.body.FUNCTION_WITH_PARAM.length, 5)
446
+ await container.Start()
447
+ const request = await container.pluginInstance._buildRequest(msg)
423
448
 
424
- assert.exists(request.body.FUNCTION_WITH_PARAM_FROM_SCRIPTING_MEMORY)
425
- assert.equal(request.body.FUNCTION_WITH_PARAM_FROM_SCRIPTING_MEMORY.length, 7)
449
+ assert.isTrue(request.json)
450
+ assert.exists(request.body)
426
451
 
427
- assert.exists(request.body.SAMPLE_ENV)
428
- assert.equal(request.body.SAMPLE_ENV, 'SAMPLE_ENV')
452
+ assert.exists(request.body.FUNCTION_WITHOUT_PARAM)
453
+ assert.equal(request.body.FUNCTION_WITHOUT_PARAM.length, 4)
429
454
 
430
- assert.exists(request.body.USING_CODE)
431
- assert.equal(request.body.USING_CODE, 3)
455
+ assert.exists(request.body.FUNCTION_WITH_PARAM)
456
+ assert.equal(request.body.FUNCTION_WITH_PARAM.length, 5)
432
457
 
433
- assert.exists(request.body.VARIABLE)
434
- assert.equal(request.body.VARIABLE, 'varvalue')
458
+ assert.exists(request.body.FUNCTION_WITH_PARAM_FROM_SCRIPTING_MEMORY)
459
+ assert.equal(request.body.FUNCTION_WITH_PARAM_FROM_SCRIPTING_MEMORY.length, 7)
435
460
 
436
- assert.equal(request.body.PROJECTNAME, 'MYPROJECTNAME')
437
- assert.equal(request.body.TESTSESSIONNAME, 'MYTESTSESSIONNAME')
438
- assert.equal(request.body.TESTCASENAME, 'MYTESTCASENAME')
439
- assert.equal(request.body.CUSTOMCAPABILITY, 'MYCUSTOMCAPABILITY')
461
+ assert.exists(request.body.SAMPLE_ENV)
462
+ assert.equal(request.body.SAMPLE_ENV, 'SAMPLE_ENV')
440
463
 
441
- await container.Clean()
442
- })
443
- it('should parse string template', async function () {
444
- const myCaps = Object.assign({}, myCapsStringTemplate)
445
- const driver = new BotDriver(myCaps)
446
- const container = await driver.Build()
464
+ assert.exists(request.body.USING_CODE)
465
+ assert.equal(request.body.USING_CODE, 3)
447
466
 
448
- await container.Start()
449
- const request = await container.pluginInstance._buildRequest(msg)
467
+ assert.exists(request.body.VARIABLE)
468
+ assert.equal(request.body.VARIABLE, 'varvalue')
450
469
 
451
- assert.isTrue(request.json)
452
- assert.exists(request.body)
470
+ assert.equal(request.body.PROJECTNAME, 'MYPROJECTNAME')
471
+ assert.equal(request.body.TESTSESSIONNAME, 'MYTESTSESSIONNAME')
472
+ assert.equal(request.body.TESTCASENAME, 'MYTESTCASENAME')
473
+ assert.equal(request.body.CUSTOMCAPABILITY, 'MYCUSTOMCAPABILITY')
453
474
 
454
- assert.exists(request.body.timestamp)
475
+ await container.Clean()
476
+ })
477
+ it('should parse string template', async function () {
478
+ const myCaps = Object.assign({}, myCapsStringTemplate)
479
+ const driver = new BotDriver(myCaps)
480
+ const container = await driver.Build()
455
481
 
456
- await container.Clean()
457
- })
458
- it('should use scriptingMemory variables for step, and conversation id if template is set', async function () {
459
- const myCaps = Object.assign({}, myCapsConvAndStepId)
460
- const driver = new BotDriver(myCaps)
461
- const container = await driver.Build()
482
+ await container.Start()
483
+ const request = await container.pluginInstance._buildRequest(msg)
462
484
 
463
- await container.Start()
464
- const request = await container.pluginInstance._buildRequest(msg)
485
+ assert.isTrue(request.json)
486
+ assert.exists(request.body)
465
487
 
466
- assert.isTrue(request.json)
467
- assert.exists(request.body)
488
+ assert.exists(request.body.timestamp)
468
489
 
469
- assert.exists(request.body.SESSION_ID)
470
- assert.equal(request.body.SESSION_ID.length, 13)
490
+ await container.Clean()
491
+ })
492
+ it('should use scriptingMemory variables for step, and conversation id if template is set', async function () {
493
+ const myCaps = Object.assign({}, myCapsConvAndStepId)
494
+ const driver = new BotDriver(myCaps)
495
+ const container = await driver.Build()
471
496
 
472
- assert.exists(request.body.MESSAGE_ID)
473
- assert.equal(request.body.MESSAGE_ID.length, 7)
497
+ await container.Start()
498
+ const request = await container.pluginInstance._buildRequest(msg)
474
499
 
475
- await container.Clean()
476
- })
477
- it('should use request hook, from string', async function () {
478
- await _assertHook(Object.assign({}, myCapsRequestHookFromString))
479
- })
480
- it('should use request hook, from function', async function () {
481
- await _assertHook(Object.assign({}, myCapsRequestHookFromFunction))
482
- })
483
- it('should use request hook, from function2', async function () {
484
- await _assertHook(Object.assign({}, myCapsRequestHookFromFunction))
485
- })
486
- it('should use request hook, from module', async function () {
487
- await _assertHook(Object.assign({}, myCapsRequestHookFromModule))
488
- })
489
- it('should use request hook, from invalid string', async function () {
490
- const driver = new BotDriver(myCapsRequestHookFromStringInvalid)
491
- try {
492
- await driver.Build()
493
- assert.fail('it should have failed')
494
- } catch (err) {
495
- assert.isTrue(err.message.includes('Cant load hook, syntax is not valid'))
496
- }
497
- })
498
- it('should add query params from UPDATE_CUSTOM (without "?")', async function () {
499
- const myCaps = Object.assign({}, myCapsGet)
500
- const myMsg = Object.assign({}, msg)
501
- myMsg.ADD_QUERY_PARAM = {
502
- queryparam1: 'valueparam1',
503
- queryparam2: '{{msg.messageText}}'
504
- }
505
-
506
- const driver = new BotDriver(myCaps)
507
- const container = await driver.Build()
508
- assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
509
-
510
- await container.Start()
511
- const request = await container.pluginInstance._buildRequest(myMsg)
512
- assert.isObject(request.headers)
513
- assert.equal(request.uri, 'http://my-host.com/api/endpoint/messageText?queryparam1=valueparam1&queryparam2=messageText')
514
-
515
- await container.Clean()
516
- })
517
- it('should add query params from UPDATE_CUSTOM (with "?")', async function () {
518
- const myCaps = Object.assign({}, myCapsGet)
519
- myCaps.SIMPLEREST_URL = 'http://my-host.com/api/endpoint/messageText?const1=const1'
520
- const myMsg = Object.assign({}, msg)
521
- myMsg.ADD_QUERY_PARAM = {
522
- queryparam1: 'valueparam1',
523
- queryparam2: '{{msg.messageText}}'
524
- }
525
-
526
- const driver = new BotDriver(myCaps)
527
- const container = await driver.Build()
528
- assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
529
-
530
- await container.Start()
531
- const request = await container.pluginInstance._buildRequest(myMsg)
532
- assert.isObject(request.headers)
533
- assert.equal(request.uri, 'http://my-host.com/api/endpoint/messageText?const1=const1&queryparam1=valueparam1&queryparam2=messageText')
534
-
535
- await container.Clean()
536
- })
537
- it('should handle non string query params from UPDATE_CUSTOM', async function () {
538
- const myCaps = Object.assign({}, myCapsGet)
539
- const myMsg = Object.assign({}, msg)
540
- const jsonObject = { firstName: 'First', middleName: '{{msg.messageText}}', lastName: 'Last' }
541
- myMsg.ADD_QUERY_PARAM = {
542
- queryparam1: 'valueparam1',
543
- queryparam2: jsonObject,
544
- queryparam3: 11
545
- }
546
- jsonObject.middleName = 'messageText'
547
-
548
- const driver = new BotDriver(myCaps)
549
- const container = await driver.Build()
550
- assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
551
-
552
- await container.Start()
553
- const request = await container.pluginInstance._buildRequest(myMsg)
554
- assert.isObject(request.headers)
555
- assert.equal(request.uri,
556
- `http://my-host.com/api/endpoint/messageText?queryparam1=valueparam1&queryparam2=${encodeURIComponent(JSON.stringify(jsonObject))}&queryparam3=11`)
557
-
558
- await container.Clean()
559
- })
560
- it('should add form params from UPDATE_CUSTOM (without "?")', async function () {
561
- const myCaps = Object.assign({}, myCapsFormPost)
562
- const myMsg = Object.assign({}, msg)
563
- myMsg.ADD_FORM_PARAM = {
564
- formparam1: 'valueparam1',
565
- formparam2: '{{msg.messageText}}'
566
- }
567
-
568
- const driver = new BotDriver(myCaps)
569
- const container = await driver.Build()
570
- assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
571
-
572
- await container.Start()
573
- const request = await container.pluginInstance._buildRequest(myMsg)
574
- assert.isObject(request.form)
575
- assert.equal(request.form.formparam1, 'valueparam1')
576
- assert.equal(request.form.formparam2, 'messageText')
577
-
578
- await container.Clean()
579
- })
580
- it('should add header from UPDATE_CUSTOM', async function () {
581
- const myCaps = Object.assign({}, myCapsGet)
582
- const myMsg = Object.assign({}, msg)
583
- myMsg.ADD_HEADER = {
584
- headerparam1: 'headerparam1',
585
- headerparam2: '{{msg.messageText}}'
586
- }
587
-
588
- const driver = new BotDriver(myCaps)
589
- const container = await driver.Build()
590
- assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
591
-
592
- await container.Start()
593
- const request = await container.pluginInstance._buildRequest(myMsg)
594
- assert.isObject(request.headers)
595
- assert.equal(request.headers.headerparam1, 'headerparam1')
596
- assert.equal(request.headers.headerparam2, 'messageText')
597
-
598
- await container.Clean()
599
- })
600
- it('should handle and add non string header from UPDATE_CUSTOM', async function () {
601
- const myCaps = Object.assign({}, myCapsGet)
602
- const myMsg = Object.assign({}, msg)
603
- myMsg.ADD_HEADER = {
604
- headerparam1: 'headerparam1',
605
- headerparam2: { firstName: 'First', middleName: null, lastName: 'Last' }
606
- }
607
-
608
- const driver = new BotDriver(myCaps)
609
- const container = await driver.Build()
610
- assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
611
-
612
- await container.Start()
613
- const request = await container.pluginInstance._buildRequest(myMsg)
614
- assert.isObject(request.headers)
615
- assert.equal(request.headers.headerparam1, 'headerparam1')
616
- assert.isObject(request.headers.headerparam2)
617
- assert.equal(request.headers.headerparam2.firstName, 'First')
618
- assert.isNull(request.headers.headerparam2.middleName)
619
- assert.equal(request.headers.headerparam2.lastName, 'Last')
620
-
621
- await container.Clean()
622
- })
623
- })
624
- describe('connectors.simplerest.processBody', function () {
625
- it('should process simple response from hook', async function () {
626
- const myCaps = Object.assign({}, myCapsResponseHook)
627
- const driver = new BotDriver(myCaps)
628
- const container = await driver.Build()
629
- assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
500
+ assert.isTrue(request.json)
501
+ assert.exists(request.body)
630
502
 
631
- await container.Start()
632
- const msgs = await container.pluginInstance._processBodyAsyncImpl({}, true)
503
+ assert.exists(request.body.SESSION_ID)
504
+ assert.equal(request.body.SESSION_ID.length, 13)
633
505
 
634
- assert.exists(msgs)
635
- assert.equal(msgs.length, 1)
636
- assert.equal(msgs[0].messageText, 'message text from hook')
506
+ assert.exists(request.body.MESSAGE_ID)
507
+ assert.equal(request.body.MESSAGE_ID.length, 7)
637
508
 
638
- await container.Clean()
639
- })
640
- it('should ignore empty response', async function () {
641
- const myCaps = Object.assign({}, myCapsGet, {
642
- [Capabilities.SIMPLEREST_BODY_JSONPATH]: '$.responses[*]',
643
- [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: '$.text',
644
- [Capabilities.SIMPLEREST_MEDIA_JSONPATH]: '$.media',
645
- [Capabilities.SIMPLEREST_IGNORE_EMPTY]: true
509
+ await container.Clean()
646
510
  })
647
- const driver = new BotDriver(myCaps)
648
- const container = await driver.Build()
649
- assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
511
+ it('should use request hook, from string', async function () {
512
+ await _assertHook(Object.assign({}, myCapsRequestHookFromString))
513
+ })
514
+ it('should use request hook, from function', async function () {
515
+ await _assertHook(Object.assign({}, myCapsRequestHookFromFunction))
516
+ })
517
+ it('should use request hook, from function2', async function () {
518
+ await _assertHook(Object.assign({}, myCapsRequestHookFromFunction))
519
+ })
520
+ it('should use request hook, from module', async function () {
521
+ await _assertHook(Object.assign({}, myCapsRequestHookFromModule))
522
+ })
523
+ it('should use request hook, from invalid string', async function () {
524
+ const driver = new BotDriver(myCapsRequestHookFromStringInvalid)
525
+ try {
526
+ await driver.Build()
527
+ assert.fail('it should have failed')
528
+ } catch (err) {
529
+ assert.isTrue(err.message.includes('Cant load hook, syntax is not valid'))
530
+ }
531
+ })
532
+ it('should add query params from UPDATE_CUSTOM (without "?")', async function () {
533
+ const myCaps = Object.assign({}, myCapsGet)
534
+ const myMsg = Object.assign({}, msg)
535
+ myMsg.ADD_QUERY_PARAM = {
536
+ queryparam1: 'valueparam1',
537
+ queryparam2: '{{msg.messageText}}'
538
+ }
650
539
 
651
- await container.Start()
652
- const msgs = await container.pluginInstance._processBodyAsyncImpl({}, true)
540
+ const driver = new BotDriver(myCaps)
541
+ const container = await driver.Build()
542
+ assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
653
543
 
654
- assert.exists(msgs)
655
- assert.equal(msgs.length, 0)
544
+ await container.Start()
545
+ const request = await container.pluginInstance._buildRequest(myMsg)
546
+ assert.isObject(request.headers)
547
+ assert.equal(request.uri, 'http://my-host.com/api/endpoint/messageText?queryparam1=valueparam1&queryparam2=messageText')
656
548
 
657
- await container.Clean()
658
- })
659
- it('should not ignore empty response', async function () {
660
- const myCaps = Object.assign({}, myCapsGet, {
661
- [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: '$.text',
662
- [Capabilities.SIMPLEREST_MEDIA_JSONPATH]: '$.media',
663
- [Capabilities.SIMPLEREST_IGNORE_EMPTY]: false
549
+ await container.Clean()
664
550
  })
665
- const driver = new BotDriver(myCaps)
666
- const container = await driver.Build()
667
- assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
551
+ it('should add query params from UPDATE_CUSTOM (with "?")', async function () {
552
+ const myCaps = Object.assign({}, myCapsGet)
553
+ myCaps.SIMPLEREST_URL = 'http://my-host.com/api/endpoint/messageText?const1=const1'
554
+ const myMsg = Object.assign({}, msg)
555
+ myMsg.ADD_QUERY_PARAM = {
556
+ queryparam1: 'valueparam1',
557
+ queryparam2: '{{msg.messageText}}'
558
+ }
668
559
 
669
- await container.Start()
670
- const msgs = await container.pluginInstance._processBodyAsyncImpl({}, true)
560
+ const driver = new BotDriver(myCaps)
561
+ const container = await driver.Build()
562
+ assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
671
563
 
672
- assert.exists(msgs)
673
- assert.equal(msgs.length, 1)
674
- assert.equal(msgs[0].messageText, '')
564
+ await container.Start()
565
+ const request = await container.pluginInstance._buildRequest(myMsg)
566
+ assert.isObject(request.headers)
567
+ assert.equal(request.uri, 'http://my-host.com/api/endpoint/messageText?const1=const1&queryparam1=valueparam1&queryparam2=messageText')
675
568
 
676
- await container.Clean()
677
- })
678
- it('should not ignore empty response with media', async function () {
679
- const myCaps = Object.assign({}, myCapsGet, {
680
- [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: '$.text',
681
- [Capabilities.SIMPLEREST_MEDIA_JSONPATH]: '$.media',
682
- [Capabilities.SIMPLEREST_IGNORE_EMPTY]: true
569
+ await container.Clean()
683
570
  })
684
- const driver = new BotDriver(myCaps)
685
- const container = await driver.Build()
686
- assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
571
+ it('should handle non string query params from UPDATE_CUSTOM', async function () {
572
+ const myCaps = Object.assign({}, myCapsGet)
573
+ const myMsg = Object.assign({}, msg)
574
+ const jsonObject = {
575
+ firstName: 'First',
576
+ middleName: '{{msg.messageText}}',
577
+ lastName: 'Last'
578
+ }
579
+ myMsg.ADD_QUERY_PARAM = {
580
+ queryparam1: 'valueparam1',
581
+ queryparam2: jsonObject,
582
+ queryparam3: 11
583
+ }
584
+ jsonObject.middleName = 'messageText'
687
585
 
688
- await container.Start()
689
- const msgs = await container.pluginInstance._processBodyAsyncImpl({ media: ['hugo.jpg'] }, true)
586
+ const driver = new BotDriver(myCaps)
587
+ const container = await driver.Build()
588
+ assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
690
589
 
691
- assert.exists(msgs)
692
- assert.equal(msgs.length, 1)
693
- assert.equal(msgs[0].messageText, '')
694
- assert.equal(msgs[0].media.length, 1)
695
- assert.equal(msgs[0].media[0].mediaUri, 'hugo.jpg')
590
+ await container.Start()
591
+ const request = await container.pluginInstance._buildRequest(myMsg)
592
+ assert.isObject(request.headers)
593
+ assert.equal(request.uri,
594
+ `http://my-host.com/api/endpoint/messageText?queryparam1=valueparam1&queryparam2=${encodeURIComponent(JSON.stringify(jsonObject))}&queryparam3=11`)
696
595
 
697
- await container.Clean()
698
- })
699
- it('should not ignore empty response with hook NLP data', async function () {
700
- const myCaps = Object.assign({}, myCapsGet, {
701
- [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: '$.text',
702
- [Capabilities.SIMPLEREST_MEDIA_JSONPATH]: '$.media',
703
- [Capabilities.SIMPLEREST_RESPONSE_HOOK]: `
704
- botMsg.nlp = { intent: { name: 'hugo' } }
705
- `,
706
- [Capabilities.SIMPLEREST_IGNORE_EMPTY]: true
596
+ await container.Clean()
707
597
  })
708
- const driver = new BotDriver(myCaps)
709
- const container = await driver.Build()
710
- assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
598
+ it('should add form params from UPDATE_CUSTOM (without "?")', async function () {
599
+ const myCaps = Object.assign({}, myCapsFormPost)
600
+ const myMsg = Object.assign({}, msg)
601
+ myMsg.ADD_FORM_PARAM = {
602
+ formparam1: 'valueparam1',
603
+ formparam2: '{{msg.messageText}}'
604
+ }
711
605
 
712
- await container.Start()
713
- const msgs = await container.pluginInstance._processBodyAsyncImpl({}, true)
606
+ const driver = new BotDriver(myCaps)
607
+ const container = await driver.Build()
608
+ assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
714
609
 
715
- assert.exists(msgs)
716
- assert.equal(msgs.length, 1)
717
- assert.equal(msgs[0].messageText, '')
718
- assert.isNotNull(msgs[0].nlp)
610
+ await container.Start()
611
+ const request = await container.pluginInstance._buildRequest(myMsg)
612
+ assert.isObject(request.form)
613
+ assert.equal(request.form.formparam1, 'valueparam1')
614
+ assert.equal(request.form.formparam2, 'messageText')
719
615
 
720
- await container.Clean()
721
- })
722
- it('should not ignore empty response with custom hook data', async function () {
723
- const myCaps = Object.assign({}, myCapsGet, {
724
- [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: '$.text',
725
- [Capabilities.SIMPLEREST_MEDIA_JSONPATH]: '$.media',
726
- [Capabilities.SIMPLEREST_RESPONSE_HOOK]: `
727
- botMsg.someextradata = 'message text from hook'
728
- `,
729
- [Capabilities.SIMPLEREST_IGNORE_EMPTY]: true
616
+ await container.Clean()
730
617
  })
731
- const driver = new BotDriver(myCaps)
732
- const container = await driver.Build()
733
- assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
618
+ it('should add header from UPDATE_CUSTOM', async function () {
619
+ const myCaps = Object.assign({}, myCapsGet)
620
+ const myMsg = Object.assign({}, msg)
621
+ myMsg.ADD_HEADER = {
622
+ headerparam1: 'headerparam1',
623
+ headerparam2: '{{msg.messageText}}'
624
+ }
734
625
 
735
- await container.Start()
736
- const msgs = await container.pluginInstance._processBodyAsyncImpl({}, true)
626
+ const driver = new BotDriver(myCaps)
627
+ const container = await driver.Build()
628
+ assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
737
629
 
738
- assert.exists(msgs)
739
- assert.equal(msgs.length, 1)
740
- assert.equal(msgs[0].messageText, '')
741
- assert.equal(msgs[0].someextradata, 'message text from hook')
630
+ await container.Start()
631
+ const request = await container.pluginInstance._buildRequest(myMsg)
632
+ assert.isObject(request.headers)
633
+ assert.equal(request.headers.headerparam1, 'headerparam1')
634
+ assert.equal(request.headers.headerparam2, 'messageText')
742
635
 
743
- await container.Clean()
744
- })
745
- it('should not ignore empty response with messageText filled in response hook', async function () {
746
- const myCaps = Object.assign({}, myCapsGet, {
747
- [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: '$.text',
748
- [Capabilities.SIMPLEREST_MEDIA_JSONPATH]: '$.media',
749
- [Capabilities.SIMPLEREST_RESPONSE_HOOK]: `
750
- botMsg.messageText = 'message text from hook'
751
- `,
752
- [Capabilities.SIMPLEREST_IGNORE_EMPTY]: true
636
+ await container.Clean()
753
637
  })
754
- const driver = new BotDriver(myCaps)
755
- const container = await driver.Build()
756
- assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
638
+ it('should handle and add non string header from UPDATE_CUSTOM', async function () {
639
+ const myCaps = Object.assign({}, myCapsGet)
640
+ const myMsg = Object.assign({}, msg)
641
+ myMsg.ADD_HEADER = {
642
+ headerparam1: 'headerparam1',
643
+ headerparam2: {
644
+ firstName: 'First',
645
+ middleName: null,
646
+ lastName: 'Last'
647
+ }
648
+ }
757
649
 
758
- await container.Start()
759
- const msgs = await container.pluginInstance._processBodyAsyncImpl({}, true)
650
+ const driver = new BotDriver(myCaps)
651
+ const container = await driver.Build()
652
+ assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
760
653
 
761
- assert.exists(msgs)
762
- assert.equal(msgs.length, 1)
763
- assert.equal(msgs[0].messageText, 'message text from hook')
654
+ await container.Start()
655
+ const request = await container.pluginInstance._buildRequest(myMsg)
656
+ assert.isObject(request.headers)
657
+ assert.equal(request.headers.headerparam1, 'headerparam1')
658
+ assert.isObject(request.headers.headerparam2)
659
+ assert.equal(request.headers.headerparam2.firstName, 'First')
660
+ assert.isNull(request.headers.headerparam2.middleName)
661
+ assert.equal(request.headers.headerparam2.lastName, 'Last')
764
662
 
765
- await container.Clean()
766
- })
767
- it('should process multiple responses', async function () {
768
- const myCaps = Object.assign({}, myCapsGet, {
769
- [Capabilities.SIMPLEREST_BODY_JSONPATH]: '$.responses[*]',
770
- [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: '$.text',
771
- [Capabilities.SIMPLEREST_MEDIA_JSONPATH]: '$.media'
663
+ await container.Clean()
772
664
  })
773
- const driver = new BotDriver(myCaps)
774
- const container = await driver.Build()
775
- assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
776
-
777
- await container.Start()
778
- const msgs = await container.pluginInstance._processBodyAsyncImpl({
779
- responses: [
780
- {
781
- text: 'text 1',
782
- media: 'http://botium.at/1.jpg'
783
- },
784
- {
785
- text: 'text 2',
786
- media: 'http://botium.at/2.jpg'
787
- },
788
- {
789
- text: 'text 3',
790
- media: 'http://botium.at/3.jpg'
791
- }
792
- ]
793
- }, true)
794
-
795
- assert.exists(msgs)
796
- assert.equal(msgs.length, 3)
797
- assert.equal(msgs[0].messageText, 'text 1')
798
- assert.equal(msgs[0].media[0].mediaUri, 'http://botium.at/1.jpg')
799
- assert.equal(msgs[1].messageText, 'text 2')
800
- assert.equal(msgs[1].media[0].mediaUri, 'http://botium.at/2.jpg')
801
- assert.equal(msgs[2].messageText, 'text 3')
802
- assert.equal(msgs[2].media[0].mediaUri, 'http://botium.at/3.jpg')
803
-
804
- await container.Clean()
805
665
  })
806
- it('should process card responses', async function () {
807
- const myCaps = Object.assign({}, myCapsGet, {
808
- [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: '$.text',
809
- [Capabilities.SIMPLEREST_CARDS_JSONPATH]: '$.cards',
810
- [Capabilities.SIMPLEREST_CARD_TEXT_JSONPATH]: '$.title',
811
- [Capabilities.SIMPLEREST_CARD_SUBTEXT_JSONPATH]: '$.subTitle',
812
- [Capabilities.SIMPLEREST_CARD_ATTACHMENTS_JSONPATH]: '$.media',
813
- [Capabilities.SIMPLEREST_CARD_BUTTONS_JSONPATH]: '$.buttons[*].text'
666
+ describe('processBody', function () {
667
+ it('should process simple response from hook', async function () {
668
+ const myCaps = Object.assign({}, myCapsResponseHook)
669
+ const driver = new BotDriver(myCaps)
670
+ const container = await driver.Build()
671
+ assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
672
+
673
+ await container.Start()
674
+ const msgs = await container.pluginInstance._processBodyAsyncImpl({}, {}, true)
675
+
676
+ assert.exists(msgs)
677
+ assert.equal(msgs.length, 1)
678
+ assert.equal(msgs[0].messageText, 'message text from hook')
679
+
680
+ await container.Clean()
814
681
  })
815
- const driver = new BotDriver(myCaps)
816
- const container = await driver.Build()
817
- assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
818
-
819
- await container.Start()
820
- const msgs = await container.pluginInstance._processBodyAsyncImpl({
821
- cards: [
822
- {
823
- title: 'card1',
824
- subTitle: 'card1 sub',
825
- media: 'http://botium.at/1.jpg',
826
- buttons: [
827
- {
828
- text: 'c1b1'
829
- },
830
- {
831
- text: 'c1b2'
832
- }
833
- ]
834
- },
835
- {
836
- title: 'card2',
837
- subTitle: 'card2 sub',
838
- media: 'http://botium.at/2.jpg',
839
- buttons: [
840
- {
841
- text: 'c2b1'
842
- }
843
- ]
844
- }
845
- ]
846
- }, true)
847
-
848
- assert.exists(msgs)
849
- assert.equal(msgs.length, 1)
850
- assert.equal(msgs[0].cards.length, 2)
851
- assert.equal(msgs[0].cards[0].text, 'card1')
852
- assert.equal(msgs[0].cards[0].subtext, 'card1 sub')
853
- assert.equal(msgs[0].cards[0].media.length, 1)
854
- assert.equal(msgs[0].cards[0].media[0].mediaUri, 'http://botium.at/1.jpg')
855
- assert.equal(msgs[0].cards[0].media[0].mimeType, 'image/jpeg')
856
- assert.equal(msgs[0].cards[0].buttons.length, 2)
857
- assert.equal(msgs[0].cards[0].buttons[0].text, 'c1b1')
858
- assert.equal(msgs[0].cards[0].buttons[1].text, 'c1b2')
859
-
860
- assert.equal(msgs[0].cards[1].text, 'card2')
861
- assert.equal(msgs[0].cards[1].subtext, 'card2 sub')
862
- assert.equal(msgs[0].cards[1].media.length, 1)
863
- assert.equal(msgs[0].cards[1].media[0].mediaUri, 'http://botium.at/2.jpg')
864
- assert.equal(msgs[0].cards[1].media[0].mimeType, 'image/jpeg')
865
- assert.equal(msgs[0].cards[1].buttons.length, 1)
866
- assert.equal(msgs[0].cards[1].buttons[0].text, 'c2b1')
867
-
868
- await container.Clean()
869
- })
870
- })
871
- describe('connectors.simplerest.parseCapabilities', function () {
872
- it('should get multiple cap values from array', async function () {
873
- const values = getAllCapValues(Capabilities.SIMPLEREST_RESPONSE_JSONPATH, {
874
- [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: () => [
875
- '$.1',
876
- '$.2'
877
- ]
682
+ it('should ignore empty response', async function () {
683
+ const myCaps = Object.assign({}, myCapsGet, {
684
+ [Capabilities.SIMPLEREST_BODY_JSONPATH]: '$.responses[*]',
685
+ [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: '$.text',
686
+ [Capabilities.SIMPLEREST_MEDIA_JSONPATH]: '$.media',
687
+ [Capabilities.SIMPLEREST_IGNORE_EMPTY]: true
688
+ })
689
+ const driver = new BotDriver(myCaps)
690
+ const container = await driver.Build()
691
+ assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
692
+
693
+ await container.Start()
694
+ const msgs = await container.pluginInstance._processBodyAsyncImpl({}, {}, true)
695
+
696
+ assert.exists(msgs)
697
+ assert.equal(msgs.length, 0)
698
+
699
+ await container.Clean()
878
700
  })
879
- assert.lengthOf(values, 2)
880
- assert.deepEqual(values, ['$.1', '$.2'])
881
- })
882
- it('should get multiple cap values from splitted string', async function () {
883
- const values = getAllCapValues(Capabilities.SIMPLEREST_RESPONSE_JSONPATH, {
884
- [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: () => '$.1,$.2'
701
+ it('should not ignore empty response', async function () {
702
+ const myCaps = Object.assign({}, myCapsGet, {
703
+ [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: '$.text',
704
+ [Capabilities.SIMPLEREST_MEDIA_JSONPATH]: '$.media',
705
+ [Capabilities.SIMPLEREST_IGNORE_EMPTY]: false
706
+ })
707
+ const driver = new BotDriver(myCaps)
708
+ const container = await driver.Build()
709
+ assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
710
+
711
+ await container.Start()
712
+ const msgs = await container.pluginInstance._processBodyAsyncImpl({}, {}, true)
713
+
714
+ assert.exists(msgs)
715
+ assert.equal(msgs.length, 1)
716
+ assert.equal(msgs[0].messageText, '')
717
+
718
+ await container.Clean()
885
719
  })
886
- assert.lengthOf(values, 2)
887
- assert.deepEqual(values, ['$.1', '$.2'])
888
- })
889
- it('should get multiple cap values from multiple string keys', async function () {
890
- const values = getAllCapValues(Capabilities.SIMPLEREST_RESPONSE_JSONPATH, {
891
- SIMPLEREST_RESPONSE_JSONPATH_0: '$.1,$.2',
892
- SIMPLEREST_RESPONSE_JSONPATH_1: '$.3,$.4'
720
+ it('should not ignore empty response with media', async function () {
721
+ const myCaps = Object.assign({}, myCapsGet, {
722
+ [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: '$.text',
723
+ [Capabilities.SIMPLEREST_MEDIA_JSONPATH]: '$.media',
724
+ [Capabilities.SIMPLEREST_IGNORE_EMPTY]: true
725
+ })
726
+ const driver = new BotDriver(myCaps)
727
+ const container = await driver.Build()
728
+ assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
729
+
730
+ await container.Start()
731
+ const msgs = await container.pluginInstance._processBodyAsyncImpl({ media: ['hugo.jpg'] }, {}, true)
732
+
733
+ assert.exists(msgs)
734
+ assert.equal(msgs.length, 1)
735
+ assert.equal(msgs[0].messageText, '')
736
+ assert.equal(msgs[0].media.length, 1)
737
+ assert.equal(msgs[0].media[0].mediaUri, 'hugo.jpg')
738
+
739
+ await container.Clean()
893
740
  })
894
- assert.lengthOf(values, 4)
895
- assert.deepEqual(values, ['$.1', '$.2', '$.3', '$.4'])
896
- })
897
- it('should get multiple cap values from mixed keys', async function () {
898
- const values = getAllCapValues(Capabilities.SIMPLEREST_RESPONSE_JSONPATH, {
899
- SIMPLEREST_RESPONSE_JSONPATH_0: [
900
- '$.1',
901
- '$.2'
902
- ],
903
- SIMPLEREST_RESPONSE_JSONPATH_1: '$.3,$.4'
741
+ it('should not ignore empty response with hook NLP data', async function () {
742
+ const myCaps = Object.assign({}, myCapsGet, {
743
+ [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: '$.text',
744
+ [Capabilities.SIMPLEREST_MEDIA_JSONPATH]: '$.media',
745
+ [Capabilities.SIMPLEREST_RESPONSE_HOOK]: `
746
+ botMsg.nlp = { intent: { name: 'hugo' } }
747
+ `,
748
+ [Capabilities.SIMPLEREST_IGNORE_EMPTY]: true
749
+ })
750
+ const driver = new BotDriver(myCaps)
751
+ const container = await driver.Build()
752
+ assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
753
+
754
+ await container.Start()
755
+ const msgs = await container.pluginInstance._processBodyAsyncImpl({}, {}, true)
756
+
757
+ assert.exists(msgs)
758
+ assert.equal(msgs.length, 1)
759
+ assert.equal(msgs[0].messageText, '')
760
+ assert.isNotNull(msgs[0].nlp)
761
+
762
+ await container.Clean()
904
763
  })
905
- assert.lengthOf(values, 4)
906
- assert.deepEqual(values, ['$.1', '$.2', '$.3', '$.4'])
907
- })
908
- })
764
+ it('should not ignore empty response with custom hook data', async function () {
765
+ const myCaps = Object.assign({}, myCapsGet, {
766
+ [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: '$.text',
767
+ [Capabilities.SIMPLEREST_MEDIA_JSONPATH]: '$.media',
768
+ [Capabilities.SIMPLEREST_RESPONSE_HOOK]: `
769
+ botMsg.someextradata = 'message text from hook'
770
+ `,
771
+ [Capabilities.SIMPLEREST_IGNORE_EMPTY]: true
772
+ })
773
+ const driver = new BotDriver(myCaps)
774
+ const container = await driver.Build()
775
+ assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
909
776
 
910
- describe('connectors.simplerest.useresponse', function () {
911
- beforeEach(async function () {
912
- this.init = async (caps) => {
913
- this.scope = nock('https://mock.com')
914
- .get('/ping').reply(200, { text: 'response from ping' })
915
- .get('/pingtrash').reply(200, 'asdfasdfasdfasdf')
916
- .get('/pingencoded').reply(200, { text: JSON.stringify({ prop: 'response from ping' }) })
917
- .get('/pingstring').reply(200, 'response from ping')
918
- .get('/start').reply(200, { text: 'response from start' })
919
- .get('/starttrash').reply(200, 'asdfasdfasdfasdf')
920
- .get('/startencoded').reply(200, { text: JSON.stringify({ prop: 'response from start' }) })
921
- .get('/startstring').reply(200, 'response from start')
922
- .get('/msg').reply(200, { text: 'response from msg' })
923
- .get('/msgfail').reply(400, { error: 'failure text' })
924
- .persist()
777
+ await container.Start()
778
+ const msgs = await container.pluginInstance._processBodyAsyncImpl({}, {}, true)
925
779
 
926
- const myCaps = Object.assign({
927
- [Capabilities.CONTAINERMODE]: 'simplerest',
928
- [Capabilities.WAITFORBOTTIMEOUT]: 1000,
929
- [Capabilities.SIMPLEREST_URL]: 'http://mock.com/msg',
930
- [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: '$.text'
931
- }, caps)
932
- this.driver = new BotDriver(myCaps)
933
- this.compiler = this.driver.BuildCompiler()
934
- this.container = await this.driver.Build()
935
- await this.container.Start()
936
- }
937
- })
938
- afterEach(async function () {
939
- await this.container.Stop()
940
- await this.container.Clean()
941
- this.scope.persist(false)
942
- })
780
+ assert.exists(msgs)
781
+ assert.equal(msgs.length, 1)
782
+ assert.equal(msgs[0].messageText, '')
783
+ assert.equal(msgs[0].someextradata, 'message text from hook')
943
784
 
944
- it('should use response from ping', async function () {
945
- await this.init({
946
- [Capabilities.SIMPLEREST_PING_URL]: 'https://mock.com/ping',
947
- [Capabilities.SIMPLEREST_PING_PROCESS_RESPONSE]: true
785
+ await container.Clean()
948
786
  })
787
+ it('should not ignore empty response with messageText filled in response hook', async function () {
788
+ const myCaps = Object.assign({}, myCapsGet, {
789
+ [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: '$.text',
790
+ [Capabilities.SIMPLEREST_MEDIA_JSONPATH]: '$.media',
791
+ [Capabilities.SIMPLEREST_RESPONSE_HOOK]: `
792
+ botMsg.messageText = 'message text from hook'
793
+ `,
794
+ [Capabilities.SIMPLEREST_IGNORE_EMPTY]: true
795
+ })
796
+ const driver = new BotDriver(myCaps)
797
+ const container = await driver.Build()
798
+ assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
799
+
800
+ await container.Start()
801
+ const msgs = await container.pluginInstance._processBodyAsyncImpl({}, {}, true)
949
802
 
950
- this.compiler.ReadScript(path.resolve(__dirname, 'convos'), 'responsefromping.convo.txt')
951
- const transcript = await this.compiler.convos[0].Run(this.container)
952
- assert.equal(transcript.steps.length, 1)
953
- assert.equal(transcript.steps[0].actual.messageText, 'response from ping')
803
+ assert.exists(msgs)
804
+ assert.equal(msgs.length, 1)
805
+ assert.equal(msgs[0].messageText, 'message text from hook')
806
+
807
+ await container.Clean()
808
+ })
809
+ it('should process multiple responses', async function () {
810
+ const myCaps = Object.assign({}, myCapsGet, {
811
+ [Capabilities.SIMPLEREST_BODY_JSONPATH]: '$.responses[*]',
812
+ [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: '$.text',
813
+ [Capabilities.SIMPLEREST_MEDIA_JSONPATH]: '$.media'
814
+ })
815
+ const driver = new BotDriver(myCaps)
816
+ const container = await driver.Build()
817
+ assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
818
+
819
+ await container.Start()
820
+ const msgs = await container.pluginInstance._processBodyAsyncImpl({
821
+ responses: [
822
+ {
823
+ text: 'text 1',
824
+ media: 'http://botium.at/1.jpg'
825
+ },
826
+ {
827
+ text: 'text 2',
828
+ media: 'http://botium.at/2.jpg'
829
+ },
830
+ {
831
+ text: 'text 3',
832
+ media: 'http://botium.at/3.jpg'
833
+ }
834
+ ]
835
+ }, {}, true)
836
+
837
+ assert.exists(msgs)
838
+ assert.equal(msgs.length, 3)
839
+ assert.equal(msgs[0].messageText, 'text 1')
840
+ assert.equal(msgs[0].media[0].mediaUri, 'http://botium.at/1.jpg')
841
+ assert.equal(msgs[1].messageText, 'text 2')
842
+ assert.equal(msgs[1].media[0].mediaUri, 'http://botium.at/2.jpg')
843
+ assert.equal(msgs[2].messageText, 'text 3')
844
+ assert.equal(msgs[2].media[0].mediaUri, 'http://botium.at/3.jpg')
845
+
846
+ await container.Clean()
847
+ })
848
+ it('should process card responses', async function () {
849
+ const myCaps = Object.assign({}, myCapsGet, {
850
+ [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: '$.text',
851
+ [Capabilities.SIMPLEREST_CARDS_JSONPATH]: '$.cards',
852
+ [Capabilities.SIMPLEREST_CARD_TEXT_JSONPATH]: '$.title',
853
+ [Capabilities.SIMPLEREST_CARD_SUBTEXT_JSONPATH]: '$.subTitle',
854
+ [Capabilities.SIMPLEREST_CARD_ATTACHMENTS_JSONPATH]: '$.media',
855
+ [Capabilities.SIMPLEREST_CARD_BUTTONS_JSONPATH]: '$.buttons[*].text'
856
+ })
857
+ const driver = new BotDriver(myCaps)
858
+ const container = await driver.Build()
859
+ assert.equal(container.pluginInstance.constructor.name, 'SimpleRestContainer')
860
+
861
+ await container.Start()
862
+ const msgs = await container.pluginInstance._processBodyAsyncImpl({
863
+ cards: [
864
+ {
865
+ title: 'card1',
866
+ subTitle: 'card1 sub',
867
+ media: 'http://botium.at/1.jpg',
868
+ buttons: [
869
+ {
870
+ text: 'c1b1'
871
+ },
872
+ {
873
+ text: 'c1b2'
874
+ }
875
+ ]
876
+ },
877
+ {
878
+ title: 'card2',
879
+ subTitle: 'card2 sub',
880
+ media: 'http://botium.at/2.jpg',
881
+ buttons: [
882
+ {
883
+ text: 'c2b1'
884
+ }
885
+ ]
886
+ }
887
+ ]
888
+ }, {}, true)
889
+
890
+ assert.exists(msgs)
891
+ assert.equal(msgs.length, 1)
892
+ assert.equal(msgs[0].cards.length, 2)
893
+ assert.equal(msgs[0].cards[0].text, 'card1')
894
+ assert.equal(msgs[0].cards[0].subtext, 'card1 sub')
895
+ assert.equal(msgs[0].cards[0].media.length, 1)
896
+ assert.equal(msgs[0].cards[0].media[0].mediaUri, 'http://botium.at/1.jpg')
897
+ assert.equal(msgs[0].cards[0].media[0].mimeType, 'image/jpeg')
898
+ assert.equal(msgs[0].cards[0].buttons.length, 2)
899
+ assert.equal(msgs[0].cards[0].buttons[0].text, 'c1b1')
900
+ assert.equal(msgs[0].cards[0].buttons[1].text, 'c1b2')
901
+
902
+ assert.equal(msgs[0].cards[1].text, 'card2')
903
+ assert.equal(msgs[0].cards[1].subtext, 'card2 sub')
904
+ assert.equal(msgs[0].cards[1].media.length, 1)
905
+ assert.equal(msgs[0].cards[1].media[0].mediaUri, 'http://botium.at/2.jpg')
906
+ assert.equal(msgs[0].cards[1].media[0].mimeType, 'image/jpeg')
907
+ assert.equal(msgs[0].cards[1].buttons.length, 1)
908
+ assert.equal(msgs[0].cards[1].buttons[0].text, 'c2b1')
909
+
910
+ await container.Clean()
911
+ })
912
+ })
913
+ describe('parseCapabilities', function () {
914
+ it('should get multiple cap values from array', async function () {
915
+ const values = getAllCapValues(Capabilities.SIMPLEREST_RESPONSE_JSONPATH, {
916
+ [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: () => [
917
+ '$.1',
918
+ '$.2'
919
+ ]
920
+ })
921
+ assert.lengthOf(values, 2)
922
+ assert.deepEqual(values, ['$.1', '$.2'])
923
+ })
924
+ it('should get multiple cap values from splitted string', async function () {
925
+ const values = getAllCapValues(Capabilities.SIMPLEREST_RESPONSE_JSONPATH, {
926
+ [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: () => '$.1,$.2'
927
+ })
928
+ assert.lengthOf(values, 2)
929
+ assert.deepEqual(values, ['$.1', '$.2'])
930
+ })
931
+ it('should get multiple cap values from multiple string keys', async function () {
932
+ const values = getAllCapValues(Capabilities.SIMPLEREST_RESPONSE_JSONPATH, {
933
+ SIMPLEREST_RESPONSE_JSONPATH_0: '$.1,$.2',
934
+ SIMPLEREST_RESPONSE_JSONPATH_1: '$.3,$.4'
935
+ })
936
+ assert.lengthOf(values, 4)
937
+ assert.deepEqual(values, ['$.1', '$.2', '$.3', '$.4'])
938
+ })
939
+ it('should get multiple cap values from mixed keys', async function () {
940
+ const values = getAllCapValues(Capabilities.SIMPLEREST_RESPONSE_JSONPATH, {
941
+ SIMPLEREST_RESPONSE_JSONPATH_0: [
942
+ '$.1',
943
+ '$.2'
944
+ ],
945
+ SIMPLEREST_RESPONSE_JSONPATH_1: '$.3,$.4'
946
+ })
947
+ assert.lengthOf(values, 4)
948
+ assert.deepEqual(values, ['$.1', '$.2', '$.3', '$.4'])
949
+ })
954
950
  })
955
951
 
956
- it('should not use trash response from ping', async function () {
957
- await this.init({
958
- [Capabilities.SIMPLEREST_PING_URL]: 'https://mock.com/pingtrash',
959
- [Capabilities.SIMPLEREST_PING_PROCESS_RESPONSE]: true
952
+ describe('useresponse', function () {
953
+ beforeEach(async function () {
954
+ this.init = async (caps) => {
955
+ this.scope = nock('https://mock.com')
956
+ .get('/ping').reply(200, { text: 'response from ping' })
957
+ .get('/pingtrash').reply(200, 'asdfasdfasdfasdf')
958
+ .get('/pingencoded').reply(200, { text: JSON.stringify({ prop: 'response from ping' }) })
959
+ .get('/pingstring').reply(200, 'response from ping')
960
+ .get('/start').reply(200, { text: 'response from start' })
961
+ .get('/starttrash').reply(200, 'asdfasdfasdfasdf')
962
+ .get('/startencoded').reply(200, { text: JSON.stringify({ prop: 'response from start' }) })
963
+ .get('/startstring').reply(200, 'response from start')
964
+ .get('/msg').reply(200, { text: 'response from msg' })
965
+ .get('/msgfail').reply(400, { error: 'failure text' })
966
+ .persist()
967
+
968
+ const myCaps = Object.assign({
969
+ [Capabilities.CONTAINERMODE]: 'simplerest',
970
+ [Capabilities.WAITFORBOTTIMEOUT]: 1000,
971
+ [Capabilities.SIMPLEREST_URL]: 'http://mock.com/msg',
972
+ [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: '$.text'
973
+ }, caps)
974
+ this.driver = new BotDriver(myCaps)
975
+ this.compiler = this.driver.BuildCompiler()
976
+ this.container = await this.driver.Build()
977
+ await this.container.Start()
978
+ }
979
+ })
980
+ afterEach(async function () {
981
+ await this.container.Stop()
982
+ await this.container.Clean()
983
+ this.scope.persist(false)
960
984
  })
961
985
 
962
- this.compiler.ReadScript(path.resolve(__dirname, 'convos'), 'responsefromping.convo.txt')
963
- try {
964
- await this.compiler.convos[0].Run(this.container)
965
- assert.fail('should have failed')
966
- } catch (err) {
967
- assert.isTrue(err.message.indexOf('Bot did not respond within') >= 0)
968
- }
969
- })
986
+ it('should use response from ping', async function () {
987
+ await this.init({
988
+ [Capabilities.SIMPLEREST_PING_URL]: 'https://mock.com/ping',
989
+ [Capabilities.SIMPLEREST_PING_PROCESS_RESPONSE]: true
990
+ })
970
991
 
971
- it('should use parser to parse response from ping', async function () {
972
- await this.init({
973
- [Capabilities.SIMPLEREST_PING_URL]: 'https://mock.com/pingencoded',
974
- [Capabilities.SIMPLEREST_PING_PROCESS_RESPONSE]: true,
975
- [Capabilities.SIMPLEREST_PARSER_HOOK]: 'body.text = JSON.parse(body.text).prop'
992
+ this.compiler.ReadScript(path.resolve(__dirname, 'convos'), 'responsefromping.convo.txt')
993
+ const transcript = await this.compiler.convos[0].Run(this.container)
994
+ assert.equal(transcript.steps.length, 1)
995
+ assert.equal(transcript.steps[0].actual.messageText, 'response from ping')
976
996
  })
977
997
 
978
- this.compiler.ReadScript(path.resolve(__dirname, 'convos'), 'responsefromping.convo.txt')
979
- const transcript = await this.compiler.convos[0].Run(this.container)
980
- assert.equal(transcript.steps.length, 1)
981
- assert.equal(transcript.steps[0].actual.messageText, 'response from ping')
982
- })
998
+ it('should not use trash response from ping', async function () {
999
+ await this.init({
1000
+ [Capabilities.SIMPLEREST_PING_URL]: 'https://mock.com/pingtrash',
1001
+ [Capabilities.SIMPLEREST_PING_PROCESS_RESPONSE]: true
1002
+ })
983
1003
 
984
- it('should use parser to parse string from ping', async function () {
985
- await this.init({
986
- [Capabilities.SIMPLEREST_PING_URL]: 'https://mock.com/pingstring',
987
- [Capabilities.SIMPLEREST_PING_PROCESS_RESPONSE]: true,
988
- [Capabilities.SIMPLEREST_PARSER_HOOK]: 'changeBody({ text: body })'
1004
+ this.compiler.ReadScript(path.resolve(__dirname, 'convos'), 'responsefromping.convo.txt')
1005
+ try {
1006
+ await this.compiler.convos[0].Run(this.container)
1007
+ assert.fail('should have failed')
1008
+ } catch (err) {
1009
+ assert.isTrue(err.message.indexOf('Bot did not respond within') >= 0)
1010
+ }
989
1011
  })
990
1012
 
991
- this.compiler.ReadScript(path.resolve(__dirname, 'convos'), 'responsefromping.convo.txt')
992
- const transcript = await this.compiler.convos[0].Run(this.container)
993
- assert.equal(transcript.steps.length, 1)
994
- assert.equal(transcript.steps[0].actual.messageText, 'response from ping')
995
- })
1013
+ it('should use parser to parse response from ping', async function () {
1014
+ await this.init({
1015
+ [Capabilities.SIMPLEREST_PING_URL]: 'https://mock.com/pingencoded',
1016
+ [Capabilities.SIMPLEREST_PING_PROCESS_RESPONSE]: true,
1017
+ [Capabilities.SIMPLEREST_PARSER_HOOK]: 'body.text = JSON.parse(body.text).prop'
1018
+ })
996
1019
 
997
- it('should use response from start', async function () {
998
- await this.init({
999
- [Capabilities.SIMPLEREST_START_URL]: 'https://mock.com/start',
1000
- [Capabilities.SIMPLEREST_START_PROCESS_RESPONSE]: true
1020
+ this.compiler.ReadScript(path.resolve(__dirname, 'convos'), 'responsefromping.convo.txt')
1021
+ const transcript = await this.compiler.convos[0].Run(this.container)
1022
+ assert.equal(transcript.steps.length, 1)
1023
+ assert.equal(transcript.steps[0].actual.messageText, 'response from ping')
1001
1024
  })
1002
1025
 
1003
- this.compiler.ReadScript(path.resolve(__dirname, 'convos'), 'responsefromstart.convo.txt')
1004
- const transcript = await this.compiler.convos[0].Run(this.container)
1005
- assert.equal(transcript.steps.length, 1)
1006
- assert.equal(transcript.steps[0].actual.messageText, 'response from start')
1007
- })
1026
+ it('should use parser to parse string from ping', async function () {
1027
+ await this.init({
1028
+ [Capabilities.SIMPLEREST_PING_URL]: 'https://mock.com/pingstring',
1029
+ [Capabilities.SIMPLEREST_PING_PROCESS_RESPONSE]: true,
1030
+ [Capabilities.SIMPLEREST_PARSER_HOOK]: 'changeBody({ text: body })'
1031
+ })
1008
1032
 
1009
- it('should not use trash response from start', async function () {
1010
- await this.init({
1011
- [Capabilities.SIMPLEREST_START_URL]: 'https://mock.com/starttrash',
1012
- [Capabilities.SIMPLEREST_START_PROCESS_RESPONSE]: true
1033
+ this.compiler.ReadScript(path.resolve(__dirname, 'convos'), 'responsefromping.convo.txt')
1034
+ const transcript = await this.compiler.convos[0].Run(this.container)
1035
+ assert.equal(transcript.steps.length, 1)
1036
+ assert.equal(transcript.steps[0].actual.messageText, 'response from ping')
1013
1037
  })
1014
1038
 
1015
- this.compiler.ReadScript(path.resolve(__dirname, 'convos'), 'responsefromstart.convo.txt')
1016
- try {
1017
- await this.compiler.convos[0].Run(this.container)
1018
- assert.fail('should have failed')
1019
- } catch (err) {
1020
- assert.isTrue(err.message.indexOf('Bot did not respond within') >= 0)
1021
- }
1022
- })
1039
+ it('should use response from start', async function () {
1040
+ await this.init({
1041
+ [Capabilities.SIMPLEREST_START_URL]: 'https://mock.com/start',
1042
+ [Capabilities.SIMPLEREST_START_PROCESS_RESPONSE]: true
1043
+ })
1023
1044
 
1024
- it('should use parser to parse response from ping', async function () {
1025
- await this.init({
1026
- [Capabilities.SIMPLEREST_START_URL]: 'https://mock.com/startencoded',
1027
- [Capabilities.SIMPLEREST_START_PROCESS_RESPONSE]: true,
1028
- [Capabilities.SIMPLEREST_PARSER_HOOK]: 'body.text = JSON.parse(body.text).prop'
1045
+ this.compiler.ReadScript(path.resolve(__dirname, 'convos'), 'responsefromstart.convo.txt')
1046
+ const transcript = await this.compiler.convos[0].Run(this.container)
1047
+ assert.equal(transcript.steps.length, 1)
1048
+ assert.equal(transcript.steps[0].actual.messageText, 'response from start')
1029
1049
  })
1030
1050
 
1031
- this.compiler.ReadScript(path.resolve(__dirname, 'convos'), 'responsefromstart.convo.txt')
1032
- const transcript = await this.compiler.convos[0].Run(this.container)
1033
- assert.equal(transcript.steps.length, 1)
1034
- assert.equal(transcript.steps[0].actual.messageText, 'response from start')
1035
- })
1051
+ it('should not use trash response from start', async function () {
1052
+ await this.init({
1053
+ [Capabilities.SIMPLEREST_START_URL]: 'https://mock.com/starttrash',
1054
+ [Capabilities.SIMPLEREST_START_PROCESS_RESPONSE]: true
1055
+ })
1036
1056
 
1037
- it('should use parser to parse string from ping', async function () {
1038
- await this.init({
1039
- [Capabilities.SIMPLEREST_START_URL]: 'https://mock.com/startstring',
1040
- [Capabilities.SIMPLEREST_START_PROCESS_RESPONSE]: true,
1041
- [Capabilities.SIMPLEREST_PARSER_HOOK]: 'changeBody({ text: body })'
1057
+ this.compiler.ReadScript(path.resolve(__dirname, 'convos'), 'responsefromstart.convo.txt')
1058
+ try {
1059
+ await this.compiler.convos[0].Run(this.container)
1060
+ assert.fail('should have failed')
1061
+ } catch (err) {
1062
+ assert.isTrue(err.message.indexOf('Bot did not respond within') >= 0)
1063
+ }
1042
1064
  })
1043
1065
 
1044
- this.compiler.ReadScript(path.resolve(__dirname, 'convos'), 'responsefromstart.convo.txt')
1045
- const transcript = await this.compiler.convos[0].Run(this.container)
1046
- assert.equal(transcript.steps.length, 1)
1047
- assert.equal(transcript.steps[0].actual.messageText, 'response from start')
1048
- })
1066
+ it('should use parser to parse response from ping 2', async function () {
1067
+ await this.init({
1068
+ [Capabilities.SIMPLEREST_START_URL]: 'https://mock.com/startencoded',
1069
+ [Capabilities.SIMPLEREST_START_PROCESS_RESPONSE]: true,
1070
+ [Capabilities.SIMPLEREST_PARSER_HOOK]: 'body.text = JSON.parse(body.text).prop'
1071
+ })
1049
1072
 
1050
- it('should use error body content', async function () {
1051
- await this.init({
1052
- [Capabilities.SIMPLEREST_URL]: 'https://mock.com/msgfail'
1073
+ this.compiler.ReadScript(path.resolve(__dirname, 'convos'), 'responsefromstart.convo.txt')
1074
+ const transcript = await this.compiler.convos[0].Run(this.container)
1075
+ assert.equal(transcript.steps.length, 1)
1076
+ assert.equal(transcript.steps[0].actual.messageText, 'response from start')
1053
1077
  })
1054
1078
 
1055
- this.compiler.ReadScript(path.resolve(__dirname, 'convos'), 'hello.convo.txt')
1056
- try {
1057
- await this.compiler.convos[0].Run(this.container)
1058
- } catch (err) {
1059
- assert.isTrue(err.message.indexOf('failure text') >= 0)
1060
- }
1061
- })
1062
- })
1079
+ it('should use parser to parse string from ping 2', async function () {
1080
+ await this.init({
1081
+ [Capabilities.SIMPLEREST_START_URL]: 'https://mock.com/startstring',
1082
+ [Capabilities.SIMPLEREST_START_PROCESS_RESPONSE]: true,
1083
+ [Capabilities.SIMPLEREST_PARSER_HOOK]: 'changeBody({ text: body })'
1084
+ })
1063
1085
 
1064
- describe('connectors.simplerest.inbound', function () {
1065
- it('should accept inbound message with matching jsonpath', async function () {
1066
- const myCaps = Object.assign({}, myCapsGet)
1067
- myCaps[Capabilities.SIMPLEREST_RESPONSE_JSONPATH] = '$.text'
1068
- myCaps[Capabilities.SIMPLEREST_INBOUND_SELECTOR_JSONPATH] = '$.body.conversationId'
1069
- myCaps[Capabilities.SIMPLEREST_INBOUND_SELECTOR_VALUE] = '{{botium.conversationId}}'
1070
-
1071
- const driver = new BotDriver(myCaps)
1072
- const container = await driver.Build()
1073
- await container.Start()
1074
-
1075
- let resultResolve, resultReject
1076
- const result = new Promise((resolve, reject) => {
1077
- resultResolve = resolve
1078
- resultReject = reject
1086
+ this.compiler.ReadScript(path.resolve(__dirname, 'convos'), 'responsefromstart.convo.txt')
1087
+ const transcript = await this.compiler.convos[0].Run(this.container)
1088
+ assert.equal(transcript.steps.length, 1)
1089
+ assert.equal(transcript.steps[0].actual.messageText, 'response from start')
1079
1090
  })
1080
1091
 
1081
- container.pluginInstance.queueBotSays = (botMsg) => {
1092
+ it('should use error body content', async function () {
1093
+ await this.init({
1094
+ [Capabilities.SIMPLEREST_URL]: 'https://mock.com/msgfail'
1095
+ })
1096
+
1097
+ this.compiler.ReadScript(path.resolve(__dirname, 'convos'), 'hello.convo.txt')
1082
1098
  try {
1083
- assert.isDefined(botMsg)
1084
- assert.isDefined(botMsg.sourceData.conversationId)
1085
- container.Clean().then(resultResolve)
1099
+ await this.compiler.convos[0].Run(this.container)
1086
1100
  } catch (err) {
1087
- resultReject(err)
1088
- }
1089
- }
1090
- container.pluginInstance._processInboundEvent({
1091
- originalUrl: '/api/inbound/xxxx',
1092
- originalMethod: 'POST',
1093
- body: {
1094
- conversationId: container.pluginInstance.view.botium.conversationId,
1095
- text: 'hallo'
1101
+ assert.isTrue(err.message.indexOf('failure text') >= 0)
1096
1102
  }
1097
1103
  })
1098
-
1099
- return result
1100
1104
  })
1101
- it('should reorder multiple inbound message with order jsonpath', async function () {
1102
- const myCaps = Object.assign({}, myCapsGet)
1103
- myCaps[Capabilities.SIMPLEREST_RESPONSE_JSONPATH] = '$.text'
1104
- myCaps[Capabilities.SIMPLEREST_INBOUND_SELECTOR_JSONPATH] = '$.body.conversationId'
1105
- myCaps[Capabilities.SIMPLEREST_INBOUND_SELECTOR_VALUE] = '{{botium.conversationId}}'
1106
- myCaps[Capabilities.SIMPLEREST_INBOUND_ORDER_UNSETTLED_EVENTS_JSONPATH] = '$.body.timestamp'
1107
- myCaps[Capabilities.SIMPLEREST_INBOUND_DEBOUNCE_TIMEOUT] = 300
1108
-
1109
- const driver = new BotDriver(myCaps)
1110
- const container = await driver.Build()
1111
- await container.Start()
1112
-
1113
- let resultResolve, resultReject
1114
- const result = new Promise((resolve, reject) => {
1115
- resultResolve = resolve
1116
- resultReject = reject
1117
- })
1118
1105
 
1119
- let i = 0
1120
- container.pluginInstance.queueBotSays = (botMsg) => {
1121
- try {
1122
- if (i === 0) {
1123
- assert.equal(botMsg.messageText, 'Message1')
1124
- container.Clean().then(resultResolve)
1125
- } else if (i === 1) {
1126
- assert.equal(botMsg.messageText, 'Message2')
1106
+ describe('inbound', function () {
1107
+ it('should accept inbound message with matching jsonpath', async function () {
1108
+ const myCaps = Object.assign({}, myCapsGet)
1109
+ myCaps[Capabilities.SIMPLEREST_RESPONSE_JSONPATH] = '$.text'
1110
+ myCaps[Capabilities.SIMPLEREST_INBOUND_SELECTOR_JSONPATH] = '$.body.conversationId'
1111
+ myCaps[Capabilities.SIMPLEREST_INBOUND_SELECTOR_VALUE] = '{{botium.conversationId}}'
1112
+
1113
+ const driver = new BotDriver(myCaps)
1114
+ const container = await driver.Build()
1115
+ await container.Start()
1116
+
1117
+ let resultResolve, resultReject
1118
+ const result = new Promise((resolve, reject) => {
1119
+ resultResolve = resolve
1120
+ resultReject = reject
1121
+ })
1122
+
1123
+ container.pluginInstance.queueBotSays = (botMsg) => {
1124
+ try {
1125
+ assert.isDefined(botMsg)
1126
+ assert.isDefined(botMsg.sourceData.conversationId)
1127
1127
  container.Clean().then(resultResolve)
1128
+ } catch (err) {
1129
+ resultReject(err)
1128
1130
  }
1129
- i++
1130
- } catch (err) {
1131
- resultReject(err)
1132
- }
1133
- }
1134
-
1135
- const now = Date.now()
1136
- const littlebitLater = now + 100
1137
-
1138
- container.pluginInstance._processInboundEvent({
1139
- originalUrl: '/api/inbound/xxxx',
1140
- originalMethod: 'POST',
1141
- body: {
1142
- conversationId: container.pluginInstance.view.botium.conversationId,
1143
- timestamp: littlebitLater,
1144
- text: 'Message2'
1145
1131
  }
1132
+ container.pluginInstance._processInboundEvent({
1133
+ originalUrl: '/api/inbound/xxxx',
1134
+ originalMethod: 'POST',
1135
+ body: {
1136
+ conversationId: container.pluginInstance.view.botium.conversationId,
1137
+ text: 'hallo'
1138
+ }
1139
+ })
1140
+
1141
+ return result
1146
1142
  })
1143
+ it('should reorder multiple inbound message with order jsonpath', async function () {
1144
+ const myCaps = Object.assign({}, myCapsGet)
1145
+ myCaps[Capabilities.SIMPLEREST_RESPONSE_JSONPATH] = '$.text'
1146
+ myCaps[Capabilities.SIMPLEREST_INBOUND_SELECTOR_JSONPATH] = '$.body.conversationId'
1147
+ myCaps[Capabilities.SIMPLEREST_INBOUND_SELECTOR_VALUE] = '{{botium.conversationId}}'
1148
+ myCaps[Capabilities.SIMPLEREST_INBOUND_ORDER_UNSETTLED_EVENTS_JSONPATH] = '$.body.timestamp'
1149
+ myCaps[Capabilities.SIMPLEREST_INBOUND_DEBOUNCE_TIMEOUT] = 300
1150
+
1151
+ const driver = new BotDriver(myCaps)
1152
+ const container = await driver.Build()
1153
+ await container.Start()
1154
+
1155
+ let resultResolve, resultReject
1156
+ const result = new Promise((resolve, reject) => {
1157
+ resultResolve = resolve
1158
+ resultReject = reject
1159
+ })
1147
1160
 
1148
- container.pluginInstance._processInboundEvent({
1149
- originalUrl: '/api/inbound/xxxx',
1150
- originalMethod: 'POST',
1151
- body: {
1152
- conversationId: container.pluginInstance.view.botium.conversationId,
1153
- timestamp: now,
1154
- text: 'Message1'
1161
+ let i = 0
1162
+ container.pluginInstance.queueBotSays = (botMsg) => {
1163
+ try {
1164
+ if (i === 0) {
1165
+ assert.equal(botMsg.messageText, 'Message1')
1166
+ container.Clean().then(resultResolve)
1167
+ } else if (i === 1) {
1168
+ assert.equal(botMsg.messageText, 'Message2')
1169
+ container.Clean().then(resultResolve)
1170
+ }
1171
+ i++
1172
+ } catch (err) {
1173
+ resultReject(err)
1174
+ }
1155
1175
  }
1156
- })
1157
1176
 
1158
- return result
1159
- })
1160
- })
1177
+ const now = Date.now()
1178
+ const littlebitLater = now + 100
1161
1179
 
1162
- describe('connectors.simplerest.polling', function () {
1163
- it('should poll HTTP url', async () => {
1164
- const caps = {
1165
- [Capabilities.CONTAINERMODE]: 'simplerest',
1166
- [Capabilities.SIMPLEREST_URL]: () => 'https://mock.com/endpoint',
1167
- [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: () => ['$.text'],
1168
- [Capabilities.SIMPLEREST_POLL_URL]: () => 'https://mock.com/poll'
1169
- }
1170
- const scope = nock('https://mock.com')
1171
- .get('/endpoint')
1172
- .reply(200, {
1173
- text: 'you called me'
1174
- })
1175
- .get('/poll')
1176
- .reply(200, {
1177
- text: 'you called me'
1178
- })
1179
- .persist()
1180
-
1181
- const driver = new BotDriver(caps)
1182
- const container = await driver.Build()
1183
- await container.Start()
1184
-
1185
- await container.UserSays({ text: 'hallo' })
1186
- await container.WaitBotSays()
1187
- await container.WaitBotSays()
1188
-
1189
- await container.Stop()
1190
- await container.Clean()
1191
- scope.persist(false)
1192
- }).timeout(5000)
1193
- it('should use request hook for polling', async () => {
1194
- const caps = {
1195
- [Capabilities.CONTAINERMODE]: 'simplerest',
1196
- [Capabilities.SIMPLEREST_URL]: () => 'https://mock.com/endpoint',
1197
- [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: () => ['$.text'],
1198
- [Capabilities.SIMPLEREST_POLL_URL]: () => 'https://mock.com/poll',
1199
- [Capabilities.SIMPLEREST_POLL_REQUEST_HOOK]: 'requestOptions.uri = "https://mock.com/_from_hook"'
1200
- }
1201
- const scope = nock('https://mock.com')
1202
- .get('/endpoint')
1203
- .reply(200, {
1204
- text: 'you called me'
1180
+ container.pluginInstance._processInboundEvent({
1181
+ originalUrl: '/api/inbound/xxxx',
1182
+ originalMethod: 'POST',
1183
+ body: {
1184
+ conversationId: container.pluginInstance.view.botium.conversationId,
1185
+ timestamp: littlebitLater,
1186
+ text: 'Message2'
1187
+ }
1205
1188
  })
1206
- .get('/_from_hook')
1207
- .reply(200, {
1208
- text: 'you called me'
1189
+
1190
+ container.pluginInstance._processInboundEvent({
1191
+ originalUrl: '/api/inbound/xxxx',
1192
+ originalMethod: 'POST',
1193
+ body: {
1194
+ conversationId: container.pluginInstance.view.botium.conversationId,
1195
+ timestamp: now,
1196
+ text: 'Message1'
1197
+ }
1209
1198
  })
1210
- .persist()
1211
1199
 
1212
- const driver = new BotDriver(caps)
1213
- const container = await driver.Build()
1214
- await container.Start()
1200
+ return result
1201
+ })
1202
+ })
1203
+
1204
+ describe('polling', function () {
1205
+ it('should poll HTTP url', async function () {
1206
+ const caps = {
1207
+ [Capabilities.CONTAINERMODE]: 'simplerest',
1208
+ [Capabilities.SIMPLEREST_URL]: () => 'https://mock.com/endpoint',
1209
+ [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: () => ['$.text'],
1210
+ [Capabilities.SIMPLEREST_POLL_URL]: () => 'https://mock.com/poll'
1211
+ }
1212
+ const scope = nock('https://mock.com')
1213
+ .get('/endpoint')
1214
+ .reply(200, {
1215
+ text: 'you called me'
1216
+ })
1217
+ .get('/poll')
1218
+ .reply(200, {
1219
+ text: 'you called me'
1220
+ })
1221
+ .persist()
1222
+
1223
+ const driver = new BotDriver(caps)
1224
+ const container = await driver.Build()
1225
+ await container.Start()
1215
1226
 
1216
- await container.UserSays({ text: 'hallo' })
1217
- await container.WaitBotSays()
1218
- await container.WaitBotSays()
1227
+ await container.UserSays({ text: 'hallo' })
1228
+ await container.WaitBotSays()
1229
+ await container.WaitBotSays()
1219
1230
 
1220
- await container.Stop()
1221
- await container.Clean()
1222
- scope.persist(false)
1223
- }).timeout(5000)
1231
+ await container.Stop()
1232
+ await container.Clean()
1233
+ scope.persist(false)
1234
+ }).timeout(5000)
1235
+ it('should use request hook for polling', async function () {
1236
+ const caps = {
1237
+ [Capabilities.CONTAINERMODE]: 'simplerest',
1238
+ [Capabilities.SIMPLEREST_URL]: () => 'https://mock.com/endpoint',
1239
+ [Capabilities.SIMPLEREST_RESPONSE_JSONPATH]: () => ['$.text'],
1240
+ [Capabilities.SIMPLEREST_POLL_URL]: () => 'https://mock.com/poll',
1241
+ [Capabilities.SIMPLEREST_POLL_REQUEST_HOOK]: 'requestOptions.uri = "https://mock.com/_from_hook"'
1242
+ }
1243
+ const scope = nock('https://mock.com')
1244
+ .get('/endpoint')
1245
+ .reply(200, {
1246
+ text: 'you called me'
1247
+ })
1248
+ .get('/_from_hook')
1249
+ .reply(200, {
1250
+ text: 'you called me'
1251
+ })
1252
+ .persist()
1253
+
1254
+ const driver = new BotDriver(caps)
1255
+ const container = await driver.Build()
1256
+ await container.Start()
1257
+
1258
+ await container.UserSays({ text: 'hallo' })
1259
+ await container.WaitBotSays()
1260
+ await container.WaitBotSays()
1261
+
1262
+ await container.Stop()
1263
+ await container.Clean()
1264
+ scope.persist(false)
1265
+ }).timeout(5000)
1266
+ })
1224
1267
  })