The strategy can extract data from the request body, header or URL.
Setting Up Properties
In the virtual service editor, select the Script dispatch strategy.
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
Collapses or expands code regions.
Find / Replace
Displays the Find / Replace dialog.
Displays the Code Completion window with a list of available objects, methods, and properties.
Go to Line
Jumps to a line by its number.
Show Line Numbers
Displays or hides line numbers.
To change the font size of the editor, use Ctrl + mouse wheel.
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
mockRequestand other script objects exist only during the test run. See below.
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.
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.
In your Groovy code, you can use the following objects:
|Use methods and properties of this object to get information on the incoming request: URL, method and protocol, headers, body contents, and other data.
|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.
|Provides scripting interface to the execution context.
|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.
The following code retrieves the user name and password from the query string of a request and creates a session id for these values:
You can see examples in the code edit box when you select the Script dispatch type.