代码之家  ›  专栏  ›  技术社区  ›  Thorsten79

SoapUI在模拟服务脚本中获取请求参数

  •  25
  • Thorsten79  · 技术社区  · 17 年前

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

    假设传入的请求具有

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

    4 回复  |  直到 17 年前
        1
  •  32
  •   Shonzilla    17 年前

    如果你想访问SOAP请求并执行一些XPath处理,由于soapUI的强大功能,有一种更简单的方法可以在soapUI中完成 GPath XmlSlurper .

    以下是您如何访问客户号码:

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

    从Groovy 1.6.3(在soapUI 2.5及更高版本中使用)开始,XmlSlurper默认在命名空间感知和非验证模式下运行,因此您不需要做任何其他事情。


    Shonzilla

        2
  •  24
  •   Mateusz Mrozewski    17 年前

    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
    

    在这个例子中,我们从请求中获取两个参数并将其转换为double。这样我们就可以对参数进行计算。此示例的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>
    

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

        3
  •  0
  •   djangofan    13 年前

    在纯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
  •  0
  •   nkanani    10 年前

    延伸 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)