【问题标题】:SoapUI getting request parameters in mock service scriptSoapUI 在模拟服务脚本中获取请求参数
【发布时间】:2009-06-03 15:27:32
【问题描述】:

对于所有 SoapUI 常客来说,这可能是一件非常容易的事情。

在 SoapUI 模拟服务响应脚本中,如何提取我正在回复的请求中的值?

假设传入的请求有

<ns1:foo>
  <ns3:data>
    <ns3:CustomerNumber>1234</ns3:CustomerNumber>
  </ns3:data>
</ns1:foo>

如何将“1234”放入 Groovy 变量中?我尝试使用 xmlHolder,但我似乎有错误的 XPath。

(我已经知道如何设置属性并将其值集成到响应中。)

【问题讨论】:

    标签: xml web-services groovy soapui


    【解决方案1】:

    如果您想访问 SOAP 请求并进行一些 XPath 处理,由于 GPathXmlSlurper 的强大功能,有一种更简单的方法可以在 soapUI 中进行。

    以下是访问客户编号的方法:

    def req = new XmlSlurper().parseText(mockRequest.requestContent)
    log.info "Customer #${req.foo.data.CustomerNumber}"
    

    从 Groovy 1.6.3(用于 soapUI 2.5 及更高版本)开始,XmlSlurper 默认在命名空间感知和非验证模式下运行,因此您无需执行任何其他操作。

    干杯!
    山西拉

    【讨论】:

    • 这里有大量的 XML-Q/A,但是如果你有一个 JSON 请求,它使用 def requestJson = new groovy.json.JsonSlurper().parseText(mockRequest.requestContent) 和例如类似地工作。 log.info "${requestJson.'CustomerNumber'}"
    【解决方案2】:

    再举一个例子:

    def request = new XmlSlurper().parseText(mockRequest.requestContent)
    def a = request.Body.Add.x.toDouble()
    def b = request.Body.Add.y.toDouble()
    context.result = a + b
    

    在本例中,我们从请求中获取两个参数并将它们转换为双精度值。这样我们就可以对参数进行计算。此示例的示例 SoapUI 响应是:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:typ="http://example.org/math/types/">
       <soapenv:Header/>
       <soapenv:Body>
          <typ:AddResponse>
             <result>${result}</result>
          </typ:AddResponse>
       </soapenv:Body>
    </soapenv:Envelope>
    

    您可以看到计算结果是如何传回响应的。

    【讨论】:

    • 感谢您的示例!我喜欢 Shonzilla 和你的回复!
    【解决方案3】:

    在纯 Java(不使用 SoapUI)中,您只需创建一个自定义命名上下文,如下所示:

    import java.util.Iterator;
    import java.util.List;
    
    import javax.xml.XMLConstants;
    import javax.xml.namespace.NamespaceContext;
    
    class WSNamespaceContext implements NamespaceContext
    {
        public String getNamespaceURI(String prefix)
        {
            if ( prefix.equals("ns3") )
                return "http://www.mysite.com/services/taxservice";
           else if (prefix.equals("soapenv"))
                return "http://schemas.xmlsoap.org/soap/envelope/";
            else
                return XMLConstants.NULL_NS_URI;
        }
    
        public String getPrefix(String namespace)
        {
            if ( namespace.equals("http://www.mysite.com/services/taxservice") )
                return "ns3";
            else if (namespace.equals("http://schemas.xmlsoap.org/soap/envelope/"))
                return "soapenv";
            else
                return null;
        }
    
        public Iterator<List<String>> getPrefixes(String namespace)
        {
            return null;
        }
    }
    

    然后,像这样解析它:

    XPathFactory factory = XPathFactory.newInstance(); 
    XPath xp = factory.newXPath(); 
    xp.setNamespaceContext( nsc ); 
    XPathExpression xpexpr = xp.compile("//ns3:CustomerNumber/text()");
    NodeList nodes = (NodeList)xpexpr.evaluate(doc, XPathConstants.NODESET); 
    for ( int i = 0; i < nodes.getLength(); i++ )  { 
        String val = nodes.item(i).getNodeValue();
        System.out.println( "Value: " + val  ); 
    }
    

    【讨论】:

      【解决方案4】:

      扩展http://www.soapui.org/soap-mocking/creating-dynamic-mockservices.html 并基于http://www.soapui.org/apidocs/com/eviware/soapui/support/xmlholder.html 我想出了这个:

      // Create XmlHolder for request content
      def holder = new com.eviware.soapui.support.XmlHolder( mockRequest.requestContent )
      holder.namespaces["ns3"] = "ns3"
      
      // Get arguments
      def custNo = holder.getNodeValue("//ns3:CustomerNumber")
      context.setProperty("custNo", custNo)
      

      【讨论】:

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