Forum Discussion

dithegrey's avatar
dithegrey
Occasional Contributor
10 years ago

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.

 

  • YGathDdu's avatar
    YGathDdu
    8 years ago

    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