Yasas De Silva
Yasas De Silva

Reputation: 207

SoapUI REST mock service give errors when mocking to handle dynamic responses depending on request values (But the same method works for SOAP mocks)

I was trying to mock the following REST web-service using soapUI

Sample Request:

   <Request>
            <HistoricTxn>
                <reference>E1</reference>
                <method>txn</method>
            </HistoricTxn>
    </Request>

SampleResponse1

<Response>
    <reason>ACCEPTED</reason>
    <status>1</status>
    <time>10:12</time>
</Response>

SampleResponse2

<Response>
    <information>Failure on invalid request details</information>
    <reason>fails Luhn check</reason>
    <status>3</status>
    <time>10:15</time>
</Response>

Usually I use this kind of a groovy script to evaluate the request and out put a dynamic response. (When using soapUI to mock SOAP web services)

groovy Script:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context);
def holder = groovyUtils.getXmlHolder(mockRequest.getRequestContent());

def reference = holder.getNodeValue("//reference");

if(reference == "Success"){
    return "SampleResponse1";
} else {
    return "SampleResponse2";
}

Unfortunately when I try to hit a request to this REST mock service end point it returns an error. Error:

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

I understand that the error message says getRequestContent() has returned a null value and because of that I am getting this exception. But the same works with the SOAP mock services, with out returning null values or causing exceptions. Appreciate any workarounds to overcome this issue.

Upvotes: 3

Views: 12513

Answers (2)

Yasas De Silva
Yasas De Silva

Reputation: 207

I just found a workaround to overcome the aforesaid bug in soapUI(version 5.2.1).

By this point it is clear to me that I can use mockRequest.getRequest().getReader().readLine() script to read a single line in the request body. (though mockRequest.getRequestContent() returned a null object)

I would simply access the request body using the following groovy script.

def requestBody="";
  try {
       while ((line = mockRequest.getRequest().getReader().readLine()) != null){
                  requestBody = requestBody+line;
       }
  } catch (Exception e) { 
   log.error("exception:"+e)
  }

def groovyUtils = new com.eviware.soapui.support.GroovyUtils(context);
def holder = groovyUtils.getXmlHolder(requestBody); 

def reference = holder.getNodeValue("//reference");

According to the above script, it will have to read the request body line by line and then merge all the lines to have the whole request body.

Since the request body is not null anymore following script can evaluate the value inside reference tag.

def reference = holder.getNodeValue("//reference");

eventually i was able to mock my REST mock service with dynamic responses according to following conditions.

if(reference == "Success"){
    return "SampleResponse1";
} else {
    return "SampleResponse2";
}

Hope you could save a lot of time by following this method.

Upvotes: 4

Yasas De Silva
Yasas De Silva

Reputation: 207

I just figured that mockRequest.getRequestContent() returns null for all the POST, PUT, DELETE requests sent to REST web-services mocked using soapUI (version 5.2.1)

def holder = groovyUtils.getXmlHolder(mockRequest.getRequestContent());

Since the above (RequestContent) returns null, soap UI can not evaluate the value of the following tag.

def reference = holder.getNodeValue("//reference");

SoapUIOfficial References for this bug in soapUI(version 5.2.1):

https://community.smartbear.com/t5/SoapUI-NG/SoapUI-5-0-requestContent-is-null-for-Rest-Mock-service/td-p/40458

https://community.smartbear.com/t5/SoapUI-NG/Mock-Service-mockRequest-requestContent-is-NULL-HTTP-PUT/td-p/42138

Upvotes: 4

Related Questions