Forum Discussion

mwthoreso's avatar
mwthoreso
New Contributor
4 years ago

Script - Virtual Service Dispatch - JavaScript return value (setting Reponse)

Hi, with the default Groovy scripts you configure your Response Dispatch with a `return` value, the value being the name of the response:

 

if ( yourVariable.equals("somestring"))
{
    return "Success"  // "Success" is the name of the Response you've configured
}

 


Well, in JavaScript you can't just return in the main portion of the script, you can only return from a function/method. 

What specifically do we need to set (in JavaScript) to tell ReadyAPI which Response to set?

 

There are some objects for us to view, but it won't seem to let me manipulate them.  I can find the default Response:

 

log.info(mockOperation.defaultResponse)

 

 

But I can't figure out how to set a Response.  I get errors like this:

 

context.MockResponse.setName("Error")

 

Yields error:

 

TypeError: Cannot call method "setName" of null

 

 Same general error for context.MockResponse.setResponseContent()

  • mwthoreso's avatar
    mwthoreso
    New Contributor

    Super janky work around (this is not the proper way) - I'd really still like to know the official way to do this

    if ( userAri == "text1" ) 
    {
    	mockOperation.setDefaultResponse("Success")
    }
    else if ( requestBodyString.includes('text2') ) 
    {
    	mockOperation.setDefaultResponse("Conflict")
    } else 
    {
    	mockOperation.setDefaultResponse("Error")
    }
    log.info(mockOperation.defaultResponse)