Virtual Service Events

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

Using Virtual Service Events

To perform custom actions when the virtual service is functioning, you can use script properties of your service and its virtual requests. An alternative way to do this is to create event handlers in the Events manager:

  1. Click Events on the toolbar.

  2. In the subsequent dialog, click to create a new event handler.

  3. Select the event type, click OK and write the script code:

    Service virtualization and API testing: Create an event handler for a virtual service

    Click the image to enlarge it.

The event list in the dialog is project-based. That is, in this dialog, you can create event handlers that occur for virtual services and other objects of your current project.

Service-Related Events

Event Description
MockRunListener.onMockRunnerStart Occurs right after a virtual service starts. Same as the Start Script of a service. Write an event handler for this event to open files, allocate resources or create objects shared by multiple event handlers or scripts.
MockRunListener.onMockRunnerStop Occurs when a virtual service stops. Same as the Stop Script of a service. Use the onMockRunnerStop event handler to release allocated resources.
MockRunListener.onMockRequest Occurs when a virtual service receives a request. Same as the OnRequest Script of a service. Write an event handler for this event to perform specific actions with the incoming request data.
MockRunListener.onMockResult Occurs after a virtual service sends a response to the client. Same as the After Request Script of a service. You can use the onMockResult event handler, for example, to log information on the processed request or to release the resources.

The MockRunListener.onMockRequest, MockRunListener.onMockRunnerStart, MockRunListener.onMockResult, and MockRunListener.onMockRunnerStop events are not triggered for JMS virtual services. Use the JMS-specific JmsMockRunListener events instead.

JmsMockRunListener.onMockRunnerStart Occurs right after a JMS virtual service starts. Same as the Start Script of a service. Write an event handler for this event to open files, allocate resources or create objects shared by multiple event handlers or scripts.
JmsMockRunListener.onMockRunnerStop Occurs when a JMS virtual service stops. Same as the Stop Script of a service. Use the onMockRunnerStop event handler to release allocated resources.
JmsMockRunListener.onMockRequest Occurs when a JMS virtual service receives a request. Same as the OnRequest Script of a service. Write an event handler for this event to perform specific actions with the incoming request data.
JmsMockRunListener.onMockResult Occurs after a JMS virtual service sends a response to the client. Same as the After Request Script of a service. You can use the onMockResult event handler, for example, to log information on the processed request or to release the resources.
MockRunListener.beforeRoute Occurs before a virtual service routes an incoming request to a real API. You can use the beforeRoute event handler, for example, to modify HTTP requests that will be routed to an API.
MockRunListener.afterRoute Occurs after a virtual service receives a response to a routed request. You can use the afterRoute event handler, for example, to modify routed HTTP responses received from a real API.

Target Property

All the events have the Target property. It specifies the name of the virtual service, to which the event will apply. If this property is empty, an event will trigger for any virtual service in your project.

You can create the same event handler for multiple virtual services. You can determine the virtual service in script code by checking its name. To get this name, use the runner.mockContext.modelItem.name property.

Script Objects

Object Description
log Contains methods and properties for posting messages to the Script log (to view it, click Logs in the bottom left corner of the ReadyAPI window and switch to the Script log tab). Use this object for logging and debugging purposes.
request Provides a scripting interface to the incoming request. Use methods and properties of this object to get request headers, URL, contents and other data.
response Provides a scripting interface to the response a virtual service is going to send to the client. Use methods and properties of this object to change the response data.
runner Provides a scripting interface to the virtual service runner. Use methods of this object to stop the service or get scripting access to the execution context.

Notes

  • Unlike the script properties, these event handlers are not saved as part of the virtual service data and are not executed when you run your service on a remote computer.

  • By default, the event handlers are shared among all the virtual services in your project. To determine for which service the event handler was called, you use the Target property of your event handlers, or call the runner.mockContext.modelItem.name property to distinguish the virtual services in script code.

  • ReadyAPI ignores result values of the event handlers.

  • ReadyAPI does not inform you on exceptions that occur in the code of these event handlers. If an exception occurs, ReadyAPI simply exits the event handler call.

  • The script debugger does not work for event handlers.

See Also

Virtual Service Scripting
Configuring Virtual Services

Highlight search results