Forum Discussion

h_ixon's avatar
h_ixon
Occasional Contributor
15 years ago

[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:

// 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}
// ....

2 Replies

  • h_ixon's avatar
    h_ixon
    Occasional Contributor
    Up,

    I search on the web but i've still not find the response to my issue.

    I try "mockRunner.dispatchPostRequest(mockRequest).setMockResponse(result)" where 'result' is the name of the response to send but it's not work.

    Any idea ? I search on api but without success.
  • h_ixon's avatar
    h_ixon
    Occasional Contributor
    I have found an alternative solution. I write it here.

    On the mockService Onrequest script we create a context variable: context.ResultResponse = "myresponsename"

    On each operation we have a simple script with "return context.ResultResponse"

    The response is define on mockService level.