Parameter Type Description
lookup An object An object specifying the key (copied from a request field), the data source, and the response token
lookup.key An object The information on how to select the key from the request.
lookup.key.from A string or an object The name of the request field to select from, or, if the request field is an object, then an object specifying the path to the request field. For example,
{ "from": "body" }
and
{ "from": { "query": "q" } }
are both valid.
lookup.key.using An object The configuration needed to select the key from the response
lookup.key.using.method A string The method used to select the key from the request. Allowed values are regex, xpath, and jsonpath.
lookup.key.using.selector A string The selector used to select the key from the request. For a regex, this would be the pattern, and the replacement value will be the entire match by default. xpath and jsonpath selectors work on XML and JSON documents. If the request value does not match the selector (including through XML or JSON parsing errors), nothing is replaced.
lookup.key.using.ns An object For xpath selectors, the ns object maps namespace aliases to URLs
lookup.key.using.options An object For regex selectors, the options object describes the regular expression options
lookup.key.using.options.ignoreCase A boolean Uses a case-insensitive regular expression
lookup.key.using.options.multiline A boolean Uses a multiline regular expression
lookup.key.index An int (defaults to 0) Each of the selection options returns an array: regex returns an array of parenthesized gropus (with the entire match in the 0th index), and jsonpath and xpath return an array of matches. This field selects the appropriate value from the array to use as the lookup key into the data source.
lookup.fromDataSource An object Configuration for the external data source to lookup data based on the key. Each lookup configuration may only specify one data source.
lookup.fromDataSource.csv An object Configuration for using a CSV file as the data source
lookup.fromDataSource.csv.path A string The path to the CSV file, which must be readable by the mb process
lookup.fromDataSource.csv.keyColumn A string The header of the column to scan for a match against the key. If a match is found, the entire row will be returned.
lookup.fromDataSource.csv.delimiter A string(default to comma ,) The delimiter separated colums in CSV file.
lookup.into A string The token to replace in the response with the selected request value. There is no need to specify which field in the response the token will be in; all response tokens will be replaced in all response fields. A successful match will return a hashmap or dictionary type object, with named indexes. For example, if you specify
{ "into": "${NAME}" }
as your token configuration, and the data source returned a row containing "first" and "last" fields, then ${NAME}["first"] and ${NAME}["last"] will be replaced by the appropriate data. You can quote the field name with double quotes, single quotes, or no quotes at all.

The lookup behavior supports dynamically replacing values in the response with something that comes from an external data source. Looking up the values from the data source requires first selecting the key value from the request. The key selection and replacement behavior in the response mirrors the functionality for the copy behavior. We'll look at the following examples:

Look at the copy examples to see how to do advanced key selection using jsonpath and xpath.

We'll use the following CSV file for these examples, saved as "values.csv" in the current working directory of the mb process (you can always use an absolute path):

State_ID,code,Name,price,tree,jobs
1111111,400,liquid,235651,mango,farmer
9856543,404,solid,54564564,orange,miner
2222222,500,water,12564,pine,shepherd
1234564,200,plasma,2656,guava,lumberjack
9999999,200,lovers,9999,dogwood,steel worker

Lookup up from a CSV file based on a key selected with a regular expression

The following example shows selecting the key using a regular expression to match against the path. We're using the index field to select the first parenthesized group in the regular expression.

POST /imposters HTTP/1.1
Host: localhost:<%= port %>
Accept: application/json
Content-Type: application/json

{
  "port": 9595,
  "protocol": "http",
  "stubs": [
    {
      "responses": [
        {
          "is": {
            "statusCode": "${row}['code']",
            "headers": {
              "X-Tree": "${row}['tree']"
            },
            "body": "Hello ${row}['Name'], have you done your ${row}['jobs'] today?"
          },
          "behaviors": [
            {
              "lookup": {
                "key": {
                  "from": "path",
                  "using": { "method": "regex", "selector": "/(.*)$" },
                  "index": 1
                },
                "fromDataSource": {
                  "csv": {
                    "path": "<%= process.cwd() %>/values.csv",
                    "keyColumn": "Name",
                    "delimiter": ","
                  }
                },
                "into": "${row}"
              }
            }
          ]
        }
      ]
    }
  ]
}

As with the copy behavior, we can plug tokens into any of the response fields (including the statusCode). Let's see what happens when we craft a request to match all of those selectors:

GET /liquid HTTP/1.1
Host: localhost:9595
HTTP/1.1 400 Bad Request
X-Tree: mango
Connection: close
Date: Thu, 28 Dec 2016 11:37:31 GMT
Transfer-Encoding: chunked

Hello liquid, have you done your farmer today?