Applies to ReadyAPI 2.6, last modified on January 15, 2019

About

The Script dispatch strategy of a virtual service uses Groovy script code to parse data of an incoming request and return the name of the response that the virtual service will send to the client.

The strategy can extract data from the request body, header or URL.

Setting Up Properties

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

  2. Enter the Groovy script code that will retrieve data from the incoming request and will return the name of the desired response. You can see examples in the code edit box when you select the Script dispatch type.

    For more convenient editing, you can use a larger editor window. Click to open the script editor.

    Both, the edit box and the editor support the following context menu commands:

    Command Description Keyboard Shortcut

    Folding

    Collapses or expands code regions.

    None

    Find / Replace

    Displays the Find / Replace dialog.

    CTRL+F

    Code Completion

    Displays the Code Completion window with a list of available objects, methods, and properties.

    CTRL+SPACE

    Go to Line

    Jumps to a line by its number.

    CTRL+G

    Show Line Numbers

    Displays or hides line numbers.

    ALT+L

    To change the font size of the editor, use Ctrl + mouse wheel.

  3. To check the script returns no syntax errors, click . If ReadyAPI encounters an error, it will report this error. Else, it will execute the code and report the result. Note that, in many cases, the script will return [null] as mockRequest and other script objects exist only during the test run. See below.

  4. In the Default Response box, select the response that the virtual service will return in case the script 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 Script dispatch strategy

Click the image to enlarge it.

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

Script Objects

In your Groovy code, you can use the following objects:

Object Description
mockRequest Use methods and properties of this object to get information on the incoming request: URL, method and protocol, headers, body contents, and other data.
mockOperation Provides scripting interface to the virtual operation and its components: assertions, responses and dispatch settings. You can use it, for example, to get response names in script code.
context and
requestContext
Provides scripting interface to the execution context.
log Use methods and properties of this object to post messages to the Script log. To view it, click Logs at the bottom of the ReadyAPI window and switch to the Script log tab.

All objects mentioned above, except for log, are available only when the virtual service processes an incoming request. They are not available when you are evaluating your script with the button.

Examples

The following code retrieves the user name and password from the query string of a request and creates a session id for these values:

Show Example

You can see examples in the code edit box when you select the Script dispatch type.

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

See Also

Response Dispatching

Highlight search results