Virtual Service Script Editors

About

Groovy scripts can be used in virtual services for many purposes, such as forming and dispatching virtual responses, asserting incoming requests, and many more. For details, see Virtual Service Scripting.

The editors support all the standard features of ReadyAPI scripting editors, such as property expansions, code validation, and code templates.

Script objects

Below is a list of the default objects that are available in the virtual service script editors:

Object

Description

context

Provides a scripting interface to the virtual service, responses, and virtual service runner. Use the context.properties collection to store service-related properties.

log

Includes methods and properties for posting messages to the Script log (to view this log, click Logs in the bottom left corner of the ReadyAPI window and switch to the Script log tab).

You use this for debugging and logging purposes: to post and view data in the log.

message

Used in JMS virtual services. Provides a scripting interface to the JMS message body and headers.

mockOperation

Corresponds to a virtual operation. Provides a scripting interface to the virtual response and request data, assertions, dispatch settings, and other properties.

mockRequest

Provides a scripting interface to the incoming request. Use methods and properties of this object to get the query string, port, headers, body, and other data.

mockResponse

Provides a scripting interface to the virtual response data defined in a virtual service for a response. You can use the responseContent of this property, for example, to change the response data before the virtual service sends the response to a client.

mockResponseLite

A lightweight, high-performance alternative to mockResponse for modifying the virtual response. Use this object instead of mockResponse when high concurrency is required. It exposes four unsynchronized methods: setResponseHeaders, setMediaType, setResponseHttpStatus, and setResponseContent. When any of these methods is called, the virtual service uses the values set on mockResponseLite to build the final response, bypassing mockResponse entirely.

Note

The mockResponseLite is available only in the Edit Response script. It is not available in dispatch scripts or other script contexts.

mockResult

Provides a scripting interface to the entire message exchange procedure: request, response, and assertion results.

mockRunner

Provides a scripting interface to the virtual service runner. You can use this object's methods to stop the current service.

requestContext

Provides a scripting interface to the request context. Use the requestContext.properties collection to store request-related properties.

You can see methods and properties of these objects on the Code Completion list. To invoke it, type the object name and press Ctrl+Space.

Object reference

To learn more about an object, find it in the API docs. API docs describe the internal structure of ReadyAPI; however, the names of objects used in the scripting editors are not necessarily the same as the names in the internal structure of ReadyAPI. To find an object in the documentation:

  1. Use the getClass() method available to every object in ReadyAPI to find the class name.

  2. Post the result to the Script Log using the log.info() method.

For example, for the context object, the script will look like this:

def className = context.getClass();
log.info(className);
editor-get-class-virt.png

Examples

You can find examples of virtual service scripts in the Virtual Service Scripting topic.

See Also

Publication date: