cancel
Showing results for 
Search instead for 
Did you mean: 

Dispatching responses from a Virtual Service

Highlighted
Community Hero

Dispatching responses from a Virtual Service

As suggested by @Olga_T I created a script that dispatches responses as presented in this topic.

 


2018-08-23_11h54_32.png

 

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
}


If my answer helped please click on the 'Accept as Solution' button.
Tags (1)
4 REPLIES 4
Highlighted
Moderator

Re: Dispatching responses from a Virtual Service

Good job in accomplishing this one, @Lucian!

 

Thanks for sharing the script with us,

 

 


Olga Terentieva
SmartBear Assistant Community Manager

Highlighted
Community Hero

Re: Dispatching responses from a Virtual Service

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.



If my answer helped please click on the 'Accept as Solution' button.
Highlighted
Community Hero

Re: Dispatching responses from a Virtual Service

@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?

 

Regards,
  /Alex [Community Hero]
____
[Community Heroes] are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Posts made by [Community Heroes]
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
The [Community Hero] signature is used with permission by SmartBear Software.
https://community.smartbear.com/t5/custom/page/page-id/hall-of-fame
================================
Highlighted
Community Manager

Re: Dispatching responses from a Virtual Service

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

 

 

---------
Tanya Gorbunova
SmartBear Community Manager

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️
New Here?
Join us and watch the welcome video:
Announcements
Top Kudoed Authors