Mocking API’s

Start Mock Server - start()

This API allows to start the mock server.

midway.start(options, callback);

The following attributes are supported:

Attribute Description Required
options JSON object with the attributes described in the table below No
callback The first argument to callback function is the server instance if the start is successful, else it is an error. No

where options has the following attributes:

Attribute Description Required
host Hostname for mock server (default: localhost) No
port Port for mock server (default: 8080) No
httpsPort Https port for mock server No
mockedDirectory Path to the mocked data directory (default: resources/mocked-data w.r.t working directory). No
sessions Number of parallel sessions to start the mock server with (default: 0) No
collectMetrics Enable mock server to collect usage metrics (default: true) No
project Name for your project (default: default) No

Example

var midway = require('testarmada-midway');
midway.start({
  host: 'localhost',
  port: 12000,
  httpsPort: 12001,
  mockedDirectory: '/resources/mockedData',
  sessions: 3,
  project: 'My Project'
});


Stop Mock Server - stop()

This API allows to stop the mock server.

midway.stop(server, callback);

The following attributes are supported:

Attribute Description Required
server server instance returned by start() method Yes
callback The first argument to callback function is an error if an error is encountered in stopping the server, null otherwise No

Example

var midway = require('testarmada-midway');
var server = midway.start(options, callback);

// do something with mock server

midway.stop(server, function (error) {
  if (error) {
      console.log('Unable to stop mock server');
  } else {
      console.log('Mock Server stopped');
  }
});


Create Mocked Route - route()

This API allows to create/add required mocked REST endpoints.

midway.route(options);

The following attributes are supported:

Attribute Description Required
options JSON object with the attributes described in the table below Yes

where options has the following attributes:

Attribute Description Required
id The unique route id for the mock server Yes
label The route label used for display on the Midway Admin Panel No
path The route path Yes
method The HTTP route method (defaults to GET) No
handler The HAPI route handler which provides the route response. This is optional because you could use multiple variants to handle the response (See Variants) No

Example

var midway = require('testarmada-midway');
midway.route({
  id: 'my_route',
  label: 'My Route',
  path: '/api/foo',
  method: 'GET',
  handler: function(request, reply) {
    // Add logic for handler
    reply('Hello');
  }
});


Create Variant - variant()

This API allows to create/add variants. Variants are route handlers that you can select manually (via Midway Admin panel) or Rest call or through Node API to select a different dataset for the response for a given route. Variants are defined using the variant() method on the Route object (returned by calling the route method).

midway.route(routeOptions).variant(options)

The following attributes are supported:

Attribute Description Required
options JSON object with the attributes described in the table below Yes

where options has the following attributes:

Attribute Description Required
id The unique variant id for a given route Yes
label The route label used for display on the Admin Panel No
handler The HAPI route handler which provides the variant response for the route No

Example

var midway = require('testarmada-midway');
midway.route({
  id: 'my_route',
  path: '/api/foo',
  handler: function(request, reply) {
    // this is essentially the same as the "default" variant
    reply({firstName: 'John'});
  }
})
.variant({
  id: 'Billy',
  handler: function(request, reply) {
    reply({firstName: 'Billy'});
  }
})
.variant({
  id: 'Clark',
  handler: function(request, reply) {
    reply({firstName: 'Billy'});
  }
});


Set variant - setMockVariant()

setMockVariant can be used to set a variant to an existing API path.

midway.setMockVariant(options, callback) // with Midway library

or 

browser.setMockVariant(options, callback) // when using Magellan

The following attributes are supported:

Attribute Description Required
options JSON object with the attributes described in the table below No
callback callback function to be called after setMockVariant() Yes

where options has the following attributes:

Attribute Description Required
fixture Id defined in route Yes
variant The variant defined in endpoint.js for the fixture you entered Yes
portNumber Port number where the mock server is running on No
midwaySessionId Midway session id, if using parallel sessions No

Example

If the routes are defined like

var midway = require('testarmada-midway');
midway.route({
  id: 'hello',
  path: '/helloWorld',
  handler: function(request, reply) {
    reply('Hello World');
  }
})
.variant({
  id: 'universe',
  handler: function(request, reply) {
    reply('Hello Universe');
  }
});

For the route and variant defined as above, you can set the variant to universe as follows:

// when using Midway library
midway.setMockVariant({
    fixture: 'hello', // same as id in the .route() options
    variant: 'universe' // same as id in the .variant() options
}, function (err) {
    if (err) {
        console.log('Error in setting variant:' + err);
    } else {
        console.log('Successfully set variant');
    }
});

or 

// When using Magellan
browser.setMockVariant({ fixture: "hello", variant: "universe" });

or 

Alternately, you can also use `curl` call to set a variant with this POST call to `{host}:{port}/midway/api/route/{routeId}`

curl -H "Content-Type: application/json" -X POST -d '{"variant":"{universe}"}' http://localhost:8080/midway/api/route/hello?returnConfig=true

You can confirm if this works by going to Admin panel and see that for helloWorld route, the variant universe will be highlighted. Also, hitting this url http://localhost:8080/helloWorld will reply with Hello Universe.

If the variant does not exist on the route, mock server returns with an Internal Server error (HTTP 500).



Add global variants - addGlobalVariant()

You can also add global variants that will affect all routes. The attributes to the options are same as that of variant().

midway.route(routeOptions).addGlobalVariant(options)

where

options - JSON object with the same attributes as of variant described in this section

Example

var midway = require('testarmada-midway');

midway.addGlobalVariant({
  id: '500',
  label: '500 error',
  handler: function(request, reply) {
    reply({
      statusCode: 500,
      error: 'Internal Server Error'
    }).code(500);
  }
})


Respond With File - respondWithFile()

This API allows to respond with static data stored in a file instead of hard coding the response data in the routes definition. Based on the path of the URL that is being mocked, the response file can be dropped in the directory location and the file will be automatically used by Midway for sending the response. It also allows to specify the absolute path of the response files.

midway.util.respondWithFile(route, reply, options);

The following attributes are supported:

Attribute Description Required
route Handle to the midway route object Yes
reply Handle to the reply object Yes
options JSON object with additional options described below No
Attribute Description Required
code HTTP Status code to reply with No
filePath Static file path of the mocked data No
delay Delay response time by this value (in milliseconds) No

To use this feature, you can call respondWithFile() from inside route configuration as follows:

Example

var midway = require('testarmada-midway');

// Automatic reply of the file
midway.route({
  id: 'Get Collection',
  label: 'Get Collections',
  path: '/product/grouping/api/collection/{collectionId}'

  handler: function(req, reply) {
    midway.util.respondWithFile(this, reply);
  }
})

.variant({
  id: 'mixItem',
  label: 'Mix Item'

  handler: function(req, reply) {
    midway.util.respondWithFile(this, reply, {code: 204, filePath: '../mocked-data/fileName.json', delay: 1000});
  }
})

Note the Use of midway.util to access the method respondWithFile



Respond with mock variant - respondWithMockVariant()

This API allows to respond with a variant on the main route handler. The ‘variant’ passed in MUST be the variant on existing route.

midway.util.respondWithMockVariant(route, variant, req, reply)

The following attributes are supported:

Attribute Description Required
route The route object Yes
variant Variant on the route Yes
request Request object Yes
reply Reply object Yes

Example

var midway = require('testarmada-midway');
midway.route({
    id: 'respondWithVariant',
    label: 'Respond With Variant',
    path: '/respondWithVariant',
    variantLabel: 'Respond With Main Route',
    handler: function(req, reply) {
        midway.util.respondWithMockVariant(this, 'variant', req, reply); // make sure that the variant exist in the same route.
    }
}).variant({
    id: 'variant',
    label: 'Respond With Variant Route',
    handler: function(req, reply) {
        reply({
            'message': 'I am an example of respond_with_mock_variant instead of response of main route '
        });
    }
});

Note the Use of midway.util to access the method respondWithMockVariant



Set Mock Id - setMockId()

This API allows to set mock id for a given test case. If this is set, it overrides all the variants and mocked URLs responses to return mocked data from the given directory as mock-id, where mock-id is the directory name.

midway.setMockId(mockId, midwaySessionId) // with Midway library

or 

browser.setMockId(mocKId, midwaySessionId, callback); // when using Magellan

The following attributes are supported:

Attribute Description Required
mockId Mock id which is the directory name you want to respond data from Yes
midwaySessionId Midway session id, if using parallel sessions No
callback callback function to be invoked after mock id is set (only when using Magellan) No

The file name should be in the format url-methodName-urlCount.extension for the responses stored under file. For example, for the given route below

var midway = require('testarmada-midway');

midway.route({
  id: 'my_route',
  label: 'My Route',
  path: '/api/foo',
  method: 'GET',

  handler: function(req, reply) {
    midway.util.respondWithFile(this, reply);
  }
});

the file name should be api-foo-GET-1.json for the first time the URL is hit. For second time the URL is hit, the file name returned would be api-foo-GET-2.json. If the specific file for the count is not present, Midway will look for default file api-foo-GET.json, which is also helpful if you want to always return the same response irrespective of the number of times the URL is hit.

Example:

var midway = require('testarmada-midway');
midway.setMockId('cart', 'abcdef'); // All responses should be under "cart" directory under your mocked data directory

or 

browser.setMockId('cart', 'abcdef' , callback);

or

curl http://localhost:8000/midway/api/setMockId/cart/abcdef

TIP! For a dynamic url such as /app/{cartid}/getStatus the default file name should be app-cartid-getStatus-GET.json and the count specific file name should be like app-cartid-getStatus-GET-1.json.



Get Mock Id - getMockId()

This API is used to retrieve the currently set mock id.

midway.getMockId(midwaySessionId);

The following attributes are supported:

Attribute Description Required
midwaySessionId Midway session id, if using parallel sessions No

Example:

var midway = require('testarmada-midway');
var mockId = midway.getMockId('abcdef');

or 

curl http://localhost:8000/midway/api/getMockId/cart/abcdef

Note that abcdef is the session id in use for the current test (optional, if not using parallel sessions)



Reset Mock Id - resetMockId()

This API is used to reset currently set mock id.

midway.resetMockId(midwaySessionId) // with Midway library

or 

browser.resetMockId(midwaySessionId, callback); // when using Magellan

The following attributes are supported:

Attribute Description Required
midwaySessionId Midway session id, if using parallel sessions No
callback callback function to be invoked after mock id is set (only when using Magellan) No

Example:

var midway = require('testarmada-midway');
midway.resetMockId('abcdef');

or 

browser.resetMockId('abcdef', callback);

or 

curl http://localhost:8000/midway/api/resetMockId/cart/abcdef

Note that abcdef is the session id in use for the current test (optional, if not using parallel sessions)



Reset url count - resetURLCount()

This API is used to reset URL count to zero. This works in conjunction with setMockId function where you want to restart over for the URL count.

midway.resetURLCount(midwaySessionId)

or 

browser.resetURLCount(midwaySessionId, callback)

The following attributes are supported:

Attribute Description Required
midwaySessionId Midway session id, if using parallel sessions No
callback callback function to be be invoked after the mock id is reset No

Example:

var midway = require('testarmada-midway');
midway.resetURLCount('abcdef');

or 

browser.resetCount('abcdef', callback); // 'abcdef' is a midway session id in use for the test

or

curl http://localhost:8000/midway/api/resetURLCount/abcdef

Note that abcdef is the session id in use for the current test (optional, if not using parallel sessions)



Get url count - getURLCount()

This API is used in conjunction with setMockId function where you want to get the URL count for all mocked calls.

midway.getURLCount(midwaySessionId)

The following attributes are supported:

Attribute Description Required
midwaySessionId Midway session id, if using parallel sessions No

Example:

var midway = require('testarmada-midway');
midway.getURLCount('abcdef');

or 

curl http://localhost:8000/midway/api/getURLCount/abcdef

Note that abcdef is the session id in use for the current test (optional, if not using parallel sessions)



Register session - registerSession()

This API is used to register a session with Midway for a test case when using parallel sessions. Midway needs to be started with sessions.

midway.registerSession(); // with Midway library

or 

browser.registerSession(callback); // with browser tests

The following attributes are supported:

Attribute Description Required
callback callback function to be be invoked after the session is registered. First argument to the callback is an error object, in case of error, null otherwise. The second argument is the registered session id (Only for browser tests) Yes

You can use registerSession() to register a session with Midway and can subsequently use that session id for the current test. Midway returns a unique identifier when registering a session.

If no session is available to use, Midway returns with the message NOT_AVAILABLE.

Example

If midway server is started with sessions, for e.g 3 sessions as shown below,

var midway = require('testarmada-midway');
midway.start({
    host: 'localhost',
    port: 8080,
    mockedDirectory: 'resources/mockedData',
    sessions: 3
});
var midwaySessionId = midway.registerSession();

or


 browser.registerSession(function (err, sessId) {
    if (err) {
      return callback(new Error("Unable to get the sessionId"));
    }
    self.midwaySessionId = sessId;
    client.midwaySessionId = sessId; 
    return callback();
});

or

curl http://localhost:8000/midway/api/registerSession


Close session - closeSession()

This API is used to close a session after running a test so it can be made available for subsequent tests.

midway.closeSession(midwaySessionId);

or 

browser.closeSession(midwaySessionId, callback);

The following attributes are supported:

Attribute Description Required
midwaySesssionId Midway session id to be closed Yes
callback callback function to be be invoked after the session is registered. First argument to the callback is an error object, in case of error, null otherwise (Only when using browser tests) Yes

Example

var midway = require('testarmada-midway');
midway.closeSession('abcdef'); // abcdef is a previously registered session with Midway

or 

client.closeSession('abcdef', function (err) {
  if (err) {
    console.error("Error in closing session:");
  }
});

or

curl http://localhost:8000/midway/api/closeSession/abcdef

Note that abcdef is a previously registered session with Midway.



Check session - checkSession()

This API is used to check status of a session id. It returns one of these states

  • AVAILABLE - If the session is available for use
  • IN_USE - If the session is in use
  • DOES_NOT_EXISTS - If the session id passed is invalid or does not exist

The following attributes are supported:

Attribute Description Required
midwaySessionId Midway session id Yes

Example

var midway = require('testarmada-midway');
var status = midway.checkSession('abcdef');

or 

curl http://localhost:8000/midway/api/checkSession/abcdef


Get sessions - getSessions()

This API is used to get sessions information

midway.getSessions();

Example

var midway = require('testarmada-midway');
var status = midway.getSessions();

or 

curl http://localhost:8000/midway/api/getSessions


Clear sessions - clearSessions()

This API is used to clear the sessions information.

midway.clearSessions();

Example

var midway = require('testarmada-midway');
var status = midway.clearSessions();


Get project name - getProjectName()

This API is used to get the project name passed in Midway options

midway.getProjectName();

Example

var midway = require('testarmada-midway');
var projectName = midway.getProjectName();


Get port information - getPortInfo()

This API is used to get the port information passed in Midway options

midway.getPortInfo();

Example

var midway = require('testarmada-midway');
var portInfo = midway.getPortInfo();


Add state - addState()

This API is used to add a value to the server state.

midway.addState(route, key, value);

The following attributes are supported:

Attribute Description Required
route Route object Yes
key Key for the state variable Yes
value Value of the state variable Yes

Example

var midway = require('testarmada-midway');

midway.route({
  id: 'setState',
  label: 'Add State',
  path: '/login',
  handler: function (req, reply) {
    midway.addState(this, 'loggedIn', true);
    reply().code(204);
  }
});


Get state - getState()

This API is used to read a value from the server state.

midway.getState(route, key);

The following attributes are supported:

Attribute Description Required
route Route object Yes
key Key for the state variable Yes

Example

var midway = require('testarmada-midway');

midway.route({
  id: 'getState',
  label: 'Get State',
  path: '/isLogin',
  handler: function (req, reply) {
    var isLoggedIn = midway.getState(this, 'login');
    reply(isLoggedIn);
  }
});


Clear state - clearState()

This API is used to clear a state for a given session id (Defaults to default session).

midway.clearState(midwaySessionId);

The following attributes are supported:

Attribute Description Required
midwaySessionId Midway session id No

Example

var midway = require('testarmada-midway');

midway.clearState(); // Clears state for default session

midway.clearState('abcdef') // Clears state for session id `abcdef`


Enable Metrics - enableMetrics()

This API is used to enable gathering of usage metrics.

midway.enableMetrics(boolean);

The following attributes are supported:

Attribute Description Required
boolean true to enable, false to disable No

Example

var midway = require('testarmada-midway');

midway.enableMetrics(true); // Enables gathering of usage metrics

midway.enableMetrics(false); // Disables gathering of usage metrics


Check if metrics are enabled - isMetricsEnabled()

This API is used to check if metrics gathering is enabled on Midway. Returns true if metrics gathering is enabled, false otherwise

midway.isMetricsEnabled();

Example

var midway = require('testarmada-midway');

midway.isMetricsEnabled();


Dynamic transposition of JSON data - transposeData()

This API allows to dynamically transpose the JSON data

midway.util.transposeData(dataSet, dataToChange);

The following attributes are supported:

Attribute Description Required
dataSet The data set which needs to change Yes
dataToChange The changes needed in the data set Yes

To change the JSON data on fly (edit existing values or add values).

// Base JSON file
{
  "items":
  {
    "item":
    [
      {
        "id": "0001",
        "type": "donut",
        "name": "Cake",
        "ppu": 0.55,
        "batters":
        {
          "type" : 1,
          "batter":
          [
            { "id": "1001", "type": "Regular" },
          ]
        },
        "topping":
        [
          { "id": "5001", "type": "None" }
        ]
      }
    ]
  }
}

// Code example
var dataToChange = {
 'items.item[0].id': 1234, // substitue id 0001 to 1234
 'items.item[0].val': "value", // Add 'val' to first array element of items.item
 'items.item[1].id': 4567, // Add 'id' to second array element of items.item
 'items.item[0].batters.batter[0].id': 5678 // substitue id 1001 to 5678
}

// when using utils class
var fileLocation = require("path").join(__dirname, './resources/test-data/data-transposition-test.json');
var dataSet = utils.readJsonFile(fileLocation);
substitutedData = midway.util.transposeData(dataSet, dataToChange);

// When using with respondwithFile (This will read the file based on url path and transpose the data)
midway.util.respondWithFile(this, reply, {transpose: dataToChange});

// Resulted JSON
{
  "items": {
    "item": [
      {
        "id": 1234,
        "type": "donut",
        "name": "Cake",
        "ppu": 0.55,
        "val": "value"
        "batters": {
          "type": 1,
          "batter": [
            {"id": 5678, "type": "Regular"}
          ]
        },
        "topping": [
          {"id": "5001", "type": "None"}
        ]
      },
      {
        "id": 4567
      }
    ]
  }
}

Note the Use of midway.util to access the method transposeData



Kill process - killProcess()

This API allows to Kill a process with a pid

midway.util.killProcess(pid, signal, callback);

The following attributes are supported:

Attribute Description Required
pid process id to kill Yes
signal Signal to send (defaults to SIGKILL if passed undefined) Yes
callback Callback function after killprocess completes No

Example

var midway = require('testarmada-midway');

midway.util.killProcess(18222, 'SIGKILL', function () {
  console.log('Process killed);
});

Note the Use of midway.util to access the method killProcess



Read contents of a file - readFile()

This API allows to read contents of a file asynchronously

midway.util.readFile(filePath, callback);

The following attributes are supported:

Attribute Description Required
file path Absolute or relative location of file Yes
callback Callback function after file is read. If file is read successfully, the second argument is the file data. In case of error, the first argument is an error. Returns promise if callback is omitted No

Example

var midway = require('testarmada-midway');

midway.util.readFile('data.json', function (err, fileData) {
  if (err) {
    console.log('Error in reading file ', err);
  } else {
    console.log(fileData);
  }
});

Note the Use of midway.util to access the method readFile



Read contents of a file - readFileSynchronously()

This API allows to read contents of a file synchronously

midway.util.readFileSynchronously(filePath);

The following attributes are supported:

Attribute Description Required
file path Absolute or relative location of file Yes

Example

var midway = require('testarmada-midway');

midway.util.readFileSynchronously('data.json');

Note the Use of midway.util to access the method readFileSynchronously



Read contents of JSON file - readJsonFile()

This API allows to read contents of a JSON file synchronously

midway.util.readJsonFile(filePath);

The following attributes are supported:

Attribute Description Required
file path Absolute or relative location of JSON file Yes

Example

var midway = require('testarmada-midway');

midway.util.readJsonFile('data.json');

Note the Use of midway.util to access the method readJsonFile



Write to file - writeFile()

This API allows to write file contents to a file

midway.util.writeFile(filePath, file data, callback);

The following attributes are supported:

Attribute Description Required
file path Absolute or relative location of file Yes
file data contents to write Yes
callback Callback function after writeFile completes Yes

Example

var midway = require('testarmada-midway');

midway.util.writeFile('hello.txt', 'hello world blah blah', function () {
  console.log('Wrote to file successfully');
});

Note the Use of midway.util to access the method writeFile



Delete file - deleteFile()

This API allows to write file contents to a file

midway.util.deleteFile(filePath, callback);

The following attributes are supported:

Attribute Description Required
file location Absolute or relative location of file to delete Yes
callback Callback function after deleteFile completes Yes

Example

var midway = require('testarmada-midway');

midway.util.deleteFile('filetoDelete.txt', function (err) {
  if (err) {
    console.log('Error in deleting file');
  }
});

Note the Use of midway.util to access the method deleteFile


Check if directory exists - checkDirectoryExists()

This API allows to check if a directory exists. Returns true if directory exists, false otherwise.

midway.util.checkDirectoryExists(directoryPath);

The following attributes are supported:

Attribute Description Required
directory path Location of directory to check Yes

Example

var midway = require('testarmada-midway');

midway.util.checkDirectoryExists('/home/data');

Note the Use of midway.util to access the method checkDirectoryExists


Check if file exists - checkFileExists()

This API allows to check if a file exists. Returns true if file exists, false otherwise.

midway.util.checkFileExists(filePath);

The following attributes are supported:

Attribute Description Required
file path Location of file to check Yes

Example

var midway = require('testarmada-midway');

midway.util.checkFileExists('/home/data');

Note the Use of midway.util to access the method checkFileExists



Set Log level - setLogLevel()

This API allows to set log level on Midway

midway.log.setLogLevel(logLevel); // when using Midway

or

Logger.setLogLevel(logLevel); // when using Midway-Logger

The following attributes are supported:

Attribute Description Required
logLevel Log level you want to set .Valid values (warn/info/debug/error) Yes

Example

var midway = require('testarmada-midway');

midway.log.setLogLevel('debug');

or

curl -X GET http://localhost:8080/midway/api/setloglevel/debug


Get Log level - getLogLevel()

This API allows to get the current log level on Midway

midway.log.getLogLevel(); // when using Midway

or 

Logger.getLogLevel(); // when using Midway-Logger

Example

var midway = require('testarmada-midway');

midway.log.getLogLevel();

or

var Logger = require('testarmada-midway'-logger');
Logger.getLogLevel();

or

curl -X GET http://localhost:8080/midway/api/getloglevel


Reset Log level - resetLogLevel()

This API allows to reset the log level of Midway to info (Default log level)

midway.log.resetLogLevel();

or 

Logger.resetLogLevel();

Example

var midway = require('testarmada-midway');
midway.log.resetLogLevel();

or

var Logger = require('testarmada-midway'-logger');
Logger.resetLogLevel();

or 

curl -X GET http://localhost:8080/midway/api/resetloglevel