Mock Service 스크립트에서 요청 매개 변수를 얻는 SOAPUI
-
09-09-2019 - |
문제
이것은 아마도 모든 SOAPUI 규칙에 매우 쉬운 일일 것입니다.
Soapui Mock Service Response Script에서 답장중인 요청 내부의 값을 어떻게 추출합니까?
들어오는 요청이 있다고 가정 해 봅시다
<ns1:foo>
<ns3:data>
<ns3:CustomerNumber>1234</ns3:CustomerNumber>
</ns3:data>
</ns1:foo>
"1234"를 그루비 변수로 어떻게 얻습니까? 나는 XMLHolder와 함께 시도했지만 잘못된 XPath가있는 것 같습니다.
(저는 속성을 설정하고 그 가치를 이미 응답에 통합하는 방법을 알고 있습니다.)
해결책
SOAP 요청에 액세스하고 XPath 처리를 수행하려면 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
다른 팁
한 가지 예 :
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
이 예에서는 요청에서 두 개의 매개 변수를 얻고 복식으로 변환합니다. 이렇게하면 매개 변수에 대한 계산을 수행 할 수 있습니다. 이 예제의 샘플 비누 큐이 응답은 다음과 같습니다.
<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>
계산 결과가 응답으로 다시 전달되는 방법을 알 수 있습니다.
순수한 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 );
}
확장 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)