Lucian
8 years agoCommunity Hero
Dispatching responses from a Virtual Service
As suggested by Olga_T I created a script that dispatches responses as presented in this topic.
The idea behind it is pretty simple namely creating one operation, 4 responses and setting the dispatch style to script. The code I used is attached below:
// Get the full query string
def queryString = mockRequest.getRequest().getQueryString()
// Get the password index in the query string
def passwordIndex = queryString.indexOf("password=") + 9
// Cut out the password starting with the previously obtained index
def pass = queryString.substring(passwordIndex)
// If there are more parameters after the password rule them out
if (pass.contains("&"))
pass = pass.substring(0, pass.indexOf("&"))
/**
* Start dispatching responses
*/
if( !queryString.contains("grant_type=") || !queryString.contains("username=") || !queryString.contains("password=") )
{
// Return a bad request
return "BadRequest"
}
else if ( !queryString.contains("grant_type=password"))
{
// Return an unsupported grant type response
return "UnsupportedGrantType"
} else if ( !queryString.contains("username=test") || !checkPasswordComplexity(pass))
{
// Return invalid credentials
return "InvalidCredentials"
} else
{
return "Success"
}
// Methods definition
def checkPasswordComplexity(String password)
{
if (password ==~ /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[$@$!%*?&])[A-Za-z\d$@$!%*?&]{8,}/)
return true
else
return false
}