Forum Discussion

Lucian's avatar
Lucian
Community Hero
7 years ago

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
}
  • Olga_T's avatar
    Olga_T
    SmartBear Alumni (Retired)

    Good job in accomplishing this one, Lucian!

     

    Thanks for sharing the script with us,

     

     

    • Lucian's avatar
      Lucian
      Community Hero

      Thanks Olga_T,

       

      I think it's the first time I work with the Virtual Service from ReadyApi. I was used to the free version before but the pro version looks better to be honest.

    • AlexKaras's avatar
      AlexKaras
      Champion Level 3

      Olga_T:

      Olga,

       

      I would like to ask you (you and the team) to consider the following problem I saw:

      The case is that this thread is about ServiceV and it looks logical for it to be located within the "Service Virtualization/Virtual Server" section in order to be more easily found by the ServiceV users.

      On the other hand, as the initial question was asked within this forum section (SoapUI Pro), it is quite natural that the answer was posted here. But it is not obvious for others that they can search for some nice tips not only within dedicated forum section, but also within other ones.

       

      The simplest solution is just to move this thread (and, maybe, other ones) into the relevant forum section, but I am not sure if this results in some side effects.

       

      Could you consider and advice please?

       

      • TanyaYatskovska's avatar
        TanyaYatskovska
        SmartBear Alumni (Retired)

        Thanks for your feedback, Alex.

        This article was done within the TechCorner resource which persists only in this board at the moment. There are plans to expand this resource to all boards and even to all product communitities.

         

        Meantime, I'm sure that it will be easy to find the article via the search page:

        https://community.smartbear.com/t5/forums/searchpage/tab/message