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

发布于 2024-07-23 06:30:00 字数 363 浏览 7 评论 0原文

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

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

假设传入请求有

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

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

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

This is probably a very easy one for all SoapUI regulars.

In a SoapUI mock service response script, how do I extract the value inside the request I'm replying to?

Let's say the incoming request has

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

How do I get the "1234" into a Groovy variable? I tried with an xmlHolder but I seem to have the wrong XPath.

(I know how to set a property and integrate its value into the response already.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

耳钉梦 2024-07-30 06:30:00

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

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

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

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

干杯!
尚齐拉

If you want to access SOAP request and do some XPath processing, there's an easier way to do it in soapUI thanks to the power of GPath and XmlSlurper.

Here's how you would access the customer number:

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

As of Groovy 1.6.3 (which is used in soapUI 2.5 and beyond), XmlSlurper runs in namespace-aware and non-validating mode by default so there's nothing else you need to do.

Cheers!
Shonzilla

人间☆小暴躁 2024-07-30 06:30:00

再举一个例子:

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>

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

One more example:

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

In this example we get two parameters from the request and convert them to doubles. This way we can perform calculations on the parameters. The sample SoapUI response for this example is:

<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>

You can see how the calculations result is passed back to the response.

时光清浅 2024-07-30 06:30:00

在纯 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  ); 
}

In a pure Java (not using SoapUI) you would just create a custom Naming Context like this one:

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

Then, parse it like so:

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  ); 
}
赤濁 2024-07-30 06:30:00

扩展 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)

Extending http://www.soapui.org/soap-mocking/creating-dynamic-mockservices.html and based on http://www.soapui.org/apidocs/com/eviware/soapui/support/xmlholder.html I came up with this:

// 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)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文