Conditional Routing

Applies to ReadyAPI 3.56, last modified on November 21, 2024

About

You use the conditional routing mode to enable or disable routing for specific requests.

You write Groovy script code that specifies if the virtual service should route a request or not. The service executes this code for every incoming request that matches the virtual operation, for which you enabled conditional routing.

In a Groovy script, you use the return keyword to return results. If the script does not contain the return keyword, ReadyAPI will use the value of the last script line.

If the script returns true, the service routes the request. If the script returns false, the service does not route the request and processes it according to the Dispatch settings. If the result value is not boolean, or the script does not return anything, the service will not route the request. See more on writing script code below.

Setting Up Properties

To enable conditional routing for an operation:

  1. Enable routing for your virtual service and then select Conditional in the Routing Mode combo box for the desired operation.

  2. After you selected the Conditional value, ReadyAPI will display the editor on the right. In the editor, select Conditional Routing and write the Groovy script code:

    Service virtualization and API testing: Setting conditional request routing

    Click the image to enlarge it.

Writing Script Code

  • In a Groovy script, you use the return keyword to return the result. If the script does not contain the return keyword, ReadyAPI will use the value of the last script line.

    Here is the simplest example: The code below commands the virtual service to route a request if the p1 URL parameter of the request equals 123:

    Groovy

    return mockRequest.queryString.contains('p1=123')

    // or

    mockRequest.queryString.contains('p1=123')

    You can find more examples below.

  • In a Groovy script, you can use the following objects:

    • mockRequest – Contains methods and properties that provide access to the incoming request header, body and parameters. Below are some frequently-used properties:

      Property Description Example
      mockRequest.path Returns the path part of the request URL. http://localhost:8088/test?p1=value1
      mockRequest.queryString Returns the substring after the ? character in the URL. http://localhost:8088/test?p1=value1
      mockRequest.requestContent Returns a string that contains data of the request body. See below.
      mockRequest.requestHeaders Returns a collection of request headers. See below.
    • log – Contains methods and properties for posting messages to the Script log. To view it, click Logs at the bottom of the ReadyAPI window and switch to the Script log tab.

    You can see the complete list of available methods and properties in the Code Completion window. To view it, type the object name and a period after it. The editor will invoke the Code Completion list automatically. Alternatively, you can select Code Completion from the context menu, or press Ctrl+Space.

  • To evaluate the entered expression, click . The virtual service will execute the Groovy code you entered and will report whether the request will be routed or more.

    To use this functionality, you need to provide a sample request to the virtual service. To do this, run the virtual service and then simulate the request from your client mobile application, ReadyAPI Test or another tool.

  • If you need a larger editor window, click . This will pop out the script editor.

    The editor also supports the following commands. You can find them in the Edit menu or in the context menu of the editor:

    Command Description Keyboard Shortcut
    Code Completion Displays the Code Completion window with a list of available objects, methods and properties. Ctrl+Space
    Find / Replace Displays the Find Replace dialog. Ctrl+F
    Go to Line Lets you quickly jump to a line by its number. Ctrl+G
    Show Line Numbers Displays or hides line numbers. Alt+L

Examples

  • The following code commands the virtual service to route a request if the p1 URL parameter equals to the My value string:

    Groovy

    return mockRequest.queryString.contains('p1=My%20value')

    // or

    mockRequest.queryString.contains('p1=My%20value')

    Note: We replace the space character with its code when checking for the value.
  • The following code commands the virtual service to route a request if that request has the /client substring in the URL path:

    Groovy

    return mockRequest.path.contains('/client')

  • To check the request against multiple conditions, make them a single expression connected with logical operators:

    Groovy

    (mockRequest.path == '/client') || mockRequest.queryString.contains('p1=My%20value')

    // or

    return (mockRequest.path == '/client') || mockRequest.queryString.contains('p1=My%20value')

  • The following code commands the virtual service to route requests that include specific data in the body:

    Groovy

    mockRequest.requestContent.contains('my-data:"value",')

    // or

    return mockRequest.requestContent.contains('my-data:"value",')

  • The following code demonstrates how you can check the value of a custom header:

    Groovy

    mockRequest.requestHeaders.get('Custom-header', '') == 'custom-value'

    // or

    return mockRequest.requestHeaders.get('Custom-header', '') == 'custom-value'

    If you need complex processing, you can iterate through headers using code like this:

    Groovy

    for (hdr in mockRequest.requestHeaders) {

    // Process a header
    // For example, output it to the log
    // log.info(hdr.toString())

    }

See Also

Request Routing and Recording
About Routing

Highlight search results