SoapUI getting request parameters in mock service script

做~自己de王妃 提交于 2019-12-17 15:37:45

问题


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


回答1:


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




回答2:


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.




回答3:


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



回答4:


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)


来源:https://stackoverflow.com/questions/945403/soapui-getting-request-parameters-in-mock-service-script

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!