XPath (Dispatch Strategy)

Applies to ReadyAPI 3.51, last modified on March 21, 2024

About

The XPath dispatch strategy of a virtual service uses XQuery code to extract data from an incoming request, processes this data and returns the name of the response that the virtual service will send to the client.

The strategy extracts data from the request body. The body should contain data in the XML format.

ReadyAPI cannot parse XML documents that contain the byte order mark (BOM) character.

How It Works

  1. In the virtual service editor, select the XPath dispatch strategy.

  2. Enter the XQuery code that will retrieve data from the incoming request and return the name of the desired response.

    For example, the following code checks if the value of the data/login/@username attribute in the incoming request starts with john:

    XQuery

    declare namespace sam='http://www.my-company.com/sample/';
    if (starts-with(//sam:data[1]/login[1]/@username, 'john')) then
      'OK-Response-Name'
    else
      'Another-Response-Name'

    To specify the desired XML node in the request body, you can either type the appropriate XPath expression or click and select the desired XML element or attribute in the subsequent dialog. To use this feature, the virtual service should have a sample request data. See below.

    To check if the entered XQuery code is correct, click . If the evaluation is successful, ReadyAPI will display a message box. Otherwise, no message will be shown.

  3. In the Default Response box, select the response that the virtual service will return in case the XQuery code does not return any response name or returns the name of a response that is not defined for the request.

    In JMS virtual services, you can set the Default Response property to <None> to simulate absence of responses.

Service virtualization and API testing: Properties of the XPath dispatch strategy

Click the image to enlarge it.

There is no need to save the changes, ReadyAPI does this automatically. If the service is running on your computer, ReadyAPI applies changes on-the-fly.

Tip

To specify the XPath expression, you can click and select the desired XML element or attribute in the subsequent dialog. The dialog will form the XPath expression and insert it into the code edit box.

To use this feature, you need to send a sample request to the virtual service before configuring dispatch properties. To do this, start the service and run the request, for which you define the dispatch strategy. You can run it in any way: from a functional test case, from the APIs node, or from an external application like cURL.

See Also

Response Dispatching

Highlight search results