【问题标题】:SoapUI REST mock service give errors when mocking to handle dynamic responses depending on request values (But the same method works for SOAP mocks)SoapUI REST 模拟服务在模拟以根据请求值处理动态响应时给出错误(但相同的方法适用于 SOAP 模拟)
【发布时间】:2016-07-11 18:04:41
【问题描述】:

我试图使用 soapUI 模拟以下 REST Web 服务

样品请求:

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

样本响应1

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

样本响应2

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

通常我使用这种 groovy 脚本来评估请求并输出动态响应。 (使用soapUI模拟SOAP网络服务时)

常规脚本:

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";
}

不幸的是,当我尝试向此 REST 模拟服务端点发送请求时,它会返回错误。 错误:

com.eviware.soapui.impl.wsdl.mock.DispatchException: 失败 使用脚本调度; java.lang.NullPointerException:无法调用 空对象上的方法 getRequestContent()

我了解错误消息称 getRequestContent() 已返回空值,因此我收到此异常。但同样适用于 SOAP 模拟服务,不会返回空值或导致异常。感谢任何解决此问题的解决方法。

【问题讨论】:

  • 你说它适用于soapui。那么,当你得到错误?即,导致错误的服务部署在哪里?
  • 嗨 Rao,是的,groovy 脚本适用于soapUI 中的 SOAP Web 服务模拟。但是当我为 REST Web 服务模拟编写相同类型的 groovy 脚本时,情况并非如此。 (使用 http PUT 方法)。 WebService 尚未部署。我只是通过按下soapUI模拟服务窗口中的绿色播放按钮来启动模拟服务。

标签: web-services rest soap mocking soapui


【解决方案1】:

我刚刚发现 mockRequest.getRequestContent() 对于发送到使用 soapUI(5.2.1 版)模拟的 REST Web 服务的所有 POST、PUT、DELETE 请求返回 null

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

由于上面的(RequestContent)返回null,soap UI不能评估下面标签的值。

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

soapUI(5.2.1版)soapUI中这个bug的官方参考:

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

【讨论】:

    【解决方案2】:

    我刚刚找到了解决soapUI(版本5.2.1)中上述错误的解决方法。

    到目前为止,我很清楚我可以使用 mockRequest.getRequest().getReader().readLine() 脚本来读取请求正文中的单行。 (虽然 mockRequest.getRequestContent() 返回了一个空对象)

    我只需使用以下 groovy 脚本访问请求正文。

    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");
    

    根据上面的脚本,它必须逐行读取请求正文,然后合并所有行以获得整个请求正文

    由于请求正文不再为空,以下脚本可以评估 reference 标记内的值。

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

    最终我能够根据以下条件使用动态响应来模拟我的 REST 模拟服务。

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

    希望您可以通过遵循此方法节省大量时间。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-20
      • 1970-01-01
      相关资源
      最近更新 更多