Search code examples
node.jstestingmockingibm-watsonwatson-assistant

Intercepting IBM's Watson Botkit middle-ware for testing


My project uses the botkit-middleware-watson to talk to IBM Watson. Now I need to make a mock server for this Watson service.

This is just one line of code which calls Watson's API.

await watsonMiddleware.sendToWatsonAsync(bot, msg, null);

My question is, how can I intercept the API call and re-direct the call to my Watson Mock-Server?


Solution

  • botkit-middleware-watson examples like this one show how WatsonMiddleware can be configured:

    const WatsonMiddleware = require('botkit-middleware-watson').WatsonMiddleware;
    
    const middleware = new WatsonMiddleware({
      iam_apikey: process.env.ASSISTANT_IAM_APIKEY,
      workspace_id: process.env.WORKSPACE_ID,
      url: process.env.ASSISTANT_URL || 'https://gateway.watsonplatform.net/assistant/api',
      version: '2018-07-10'
    });
    

    ...where ASSISTANT_URL is set in .env. This seems to be a place where to configure the URL of the mock server/simulator.

    Another option could be for botkit to call the simulator as HTTP proxy, if the simulator you are using supports running as HTTP proxy.