Ask a Question

REST Mocking with Query Parametr

SOLVED
dithegrey
Occasional Contributor

REST Mocking with Query Parametr

I have several mocks with responses. But for some of them I want to create response based on additional query parameter. For example: I have mock for REST request such as "GET /order/item" and it works fine. But I tried to create mock for "GET /order/item?status=queued" and created response for it, but I get the same response as for "GET /order/item" when I test it out.

Is it possible to create REST Mock with Query in SoapUI Pro (5.1.2)?

Thanks for answers in advance.

 

6 REPLIES 6
TanyaYatskovska
SmartBear Alumni (Retired)

Hi,

 

It looks like you didn’t set up MockOperation properly. To check if your query has an additional parameter and send the appropriate response, you need to specify MockOperation Dispatching. The detailed steps are listed here: http://www.soapui.org/soap-mocking/mockoperations-and-responses.html

 

 

---------
Tanya Yatskovskaya
SmartBear Community and Education Manager



dithegrey
Occasional Contributor

As I mentioned - I have REST Mockings, not SOAP mocking.

Thus - I cannot use MockOperation

TanyaYatskovska
SmartBear Alumni (Retired)

I still see the possibility to specify a Dispatch when working with Rest Mocking: http://www.soapui.org/rest-testing-mocking/mock-service-scripting.html

---------
Tanya Yatskovskaya
SmartBear Community and Education Manager



Open the request action.

Change the Dispatch dropdown to SCRIPT

You should then be able to script something like this: (actual working example from one of my mock services)

 

// Match based on query parameter
def queryString = mockRequest.getRequest().getQueryString()
log.info "QueryString: " + queryString

def response

if (queryString !=null) {
	String[] fields = queryString.split('/&')
	fields.each
	{
		// Split each query string field into its key and value
		String[] kvp = it.split('=');
		def key = kvp[0];
		def value = kvp[1] ;

		// Change the response depending on the value of nrd:AreaCode
		if( key == "nrd:AreaCode" )
		{
			if( value == "7A1" )
			{
				// return list of BCU GP Practices
				response = "Response - GP Practices - BCU"
			}
			else if( value == "7A2" )
			{
				// return list of Hywel Dda GP Practices
				response = "Response - GP Practices - Hywel Dda"
			}
		}
	}
}

if (response != null) 
{
	log.info( "response: " + response)
	return response
}

 

Apologies in advance regarding coding conventions / style. I am not a Groovy expert but I thought a working example would be helpful

HKosova
SmartBear Alumni (Retired)

I'd like to add that in Ready! API / ServiceV Pro 1.5+ we added the parameter dispatching mode (response selection based on query/path/header parameters) that lets you configure the rules visually instead of using scripts.


Helen Kosova
SmartBear Documentation Team Lead
________________________
Did my reply answer your question? Give Kudos or Accept it as a Solution to help others. ⬇️⬇️⬇️

Hi, I am facing similar issues with with Path containing special characters - /abc/xyz/city/India?stateName=AP i tried solution provide above..it's not working. not sure, i missing anything. Thanks

 

getting error as below :

com.eviware.soapui.impl.wsdl.mock.DispatchException: Failed to dispatch using script; java.lang.NullPointerException: Cannot invoke method getRequest() on null object

cancel
Showing results for 
Search instead for 
Did you mean: