Modifying Messages

This is another cool possibility that you can use to streamline your tests, for example you might want to ensure that your service is always returning certain values to a client during a test-scenario. Here the Event Handler functionality in ReadyAPI fits the bill nicely. Open the Project Window and select the Event tab, add a handler for MonitorListener.afterProxy and set its script as follows:

//Event Handler:

// get the raw response as a string
def str = new String( messageExchange.getResponseContent()  )

// replace ConversionRateResult value with '1.0'
str = str.replaceAll("Result>.*</ConversionRateResult", "Result>1.0</ConversionRateResult");

// save the response back
messageExchange.rawResponseBody = str 

Now all responses will have a conversion rate of "1.0"

(request with altered response at the top, event handler at bottom left, and SOAP Monitor bottom right)

The beforeProxy event can be used to modify the proxied request instead:

//Event Handler:

// retrieve the request
def out = new
method.getRequestEntity().writeTo( out )

// create string from request and replace FromCurrency value with 'AUD'
def str = out.toString().replaceAll("<web:FromCurrency>.*</web:FromCurrency>", "<web:FromCurrency>AUD</web:FromCurrency>");

// replace the request
method.entity = new org.apache.http.entity.StringEntity ( str ) 

// test the modification
def outAfter = new
method.getRequestEntity().writeTo( outAfter ) outAfter.toString()


Now all requests will convert from the AUD currency no matter their input.