h_ixon
15 years agoOccasional Contributor
[Groovy] Choose response from Onrequest script
Hello,
I have a problem with my groovy script. I try to make an OnRequest script on my mockservices.
This script must check some parameters on the receive mockRequest and afterthat select the operation and the response to send. I want to write this script to prevent from code repeating in each operation.
For the moment with my onRequest script, i know the operation name and the response name (the one i want to choose) but i don't know how to set the response that the mockService will send finally.
Can you help me to dispatch or set the response ?
Thank's
Below on example of my script:
I have a problem with my groovy script. I try to make an OnRequest script on my mockservices.
This script must check some parameters on the receive mockRequest and afterthat select the operation and the response to send. I want to write this script to prevent from code repeating in each operation.
For the moment with my onRequest script, i know the operation name and the response name (the one i want to choose) but i don't know how to set the response that the mockService will send finally.
Can you help me to dispatch or set the response ?
Thank's
Below on example of my script:
// Def variable
def opeList = mockRunner.mockService.getMockOperationList()
def request = mockRequest.getRequestContent()
def operation = ""
// Set a variable with the operation name i want
opeList.eachWithIndex() {
key, value ->
if ( request =~ /\b${key.name}\b/ ){
operation = "${key.name}"
//log.info "operation = " + operation
}
}
switch ( operation ) {
case "operation1" : if(param1='john doe') {return response1} //I want to send response1 if a variable on the request is john doe but i don't know how to say "choose response1"
if(param1='jerry') {return response2}
break
case "operation2": if (param1='john doe') { return response1}
// ....