extensionTool - Groovy example to modify the response payload

Can you point me to a couple examples of how to use Groovy code in extensionTools to modify things like response headers, payloads, etc.
Comments
-
Hi,
Would it make sense to add the header using the Transport HTTP Headers table and then script the value? That would be easier since the script could just handle returning the value in that case.
For modifying the payload the extension tool can be chained to the outgoing response and then whatever the tool returns as output from the script would override the original body. Is the goal to modify xml or json? Groovy has different libraries for processing xml and json that help make them easier to modify/wrok with.
0 -
Hi William. Just looking for a basic example of a script updating the response. I've done a good bit of that in the past when building VAs in the Parasoft client. I'm generating an extensionTool via the API and I am not seeing any changes to the response as I would expect based on the things I have done in the past. I have chained it to the Outgoing Response and I am returning text.
0 -
Hi,
A trick I use to is to build the asset using the desktop and then call the relevant REST API GET calls to see how the tool was configured for a working case. That response should then be very close to the payload that would need to be sent in a POST to configure a new tool in the exact same way.
1 -
excellent idea!
0 -
Still struggling a bit here. My process generates an extensionTool in the VA and I can see it in CTP. However the code doesn't seem to have any effect on the response. Can you post a simple Groovy script that I can apply to the Incoming Request to change the request using a replace("this", "that");
0 -
To clarify, do you want to replace items in an incoming request payload or the outgoing response payload? The groovy scripts below should work for either one, but just make sure you're attaching the Extension Tool to the desired output.
Here's a groovy script to replace a string in the outgoing response payload:
import com.parasoft.api.Application def replaceString(input,context) { modified = input.replace("test","REPLACED STRING") Application.showMessage("Original payload: \n" + input + "\n"); Application.showMessage("Modified payload: \n" + modified + "\n"); return modified }
Here's a groovy script to replace the entire payload in the outgoing response:
import com.parasoft.api.Application def replacePayload(input,context) { modified = input.toString().replaceAll("[\n\r]","").replaceAll("[^\n]+","REPLACED ENTIRE PAYLOAD") Application.showMessage("Original payload: \n" + input + "\n"); Application.showMessage("Modified payload: \n" + modified + "\n"); return modified }
1 -
Awesome. Thank you. I may also want to modify the request, but I would imagine I could use this approach on the Incoming Request. Thanks again. This is very helpful to something I am working on for my employer. JT
0 -
Is scripting via extensionTool something that can be disabled in the Virtualize configuration? I am able to generate the tool locally and it correctly updates the response payload. However when it is hosted on our enterprise instance of Virtualize the update does not happen. Similar to how there was a setting to disable autodeploy, I am wondering if tools are disabled. Is this possible in the config?
Thanks,
JT
0 -
Hi,
I am not aware of any options to disable scripting. Are the tool settings the same? I believe for it to work the option "exitCodeIndicatesSuccess" is supposed to be false and it must be attached to the outgoing response.
Perhaps comparing a tool created by the REST API with one created using the Desktop in the UI might help to spot if there are any differences in settings.
Can you use event monitoring on the enterprise instance to make sure the expected responder is the one providing the response?
0 -
message just says error during script execution. view details for more information not sure how to view details in CTP
0 -
Hi,
I think the details would be in an event. Not exactly sure how to find it in CTP, if you have the server added to your desktop you could start monitoring the asset. In CTP I think there is the "Events" tab under Service Virtualization that you could probably use to filter down to events for that asset.
0 -
I was able to locate the problem The "method" value in my request payload didn't match the method name I had set up in my script.
0 -
Glad you were able to find the issue!
0