我正在尝试根据请求进行模拟响应。 有这样的要求:
<soapenv:Body>
<con:person>
<person>
<name>John</name>
<age>18</age>
</person>
</con:person>
</soapenv:Body>
和那样的反应
<soapenv:Body>
<con:result>
<person>
<name>?</name>
<age>?</age>
<country>?</country>
<city>?</city>
</person>
</con:result>
</soapenv:Body>
我可以使用请求中的元素来获取我想要的数据库并创建响应。 但是,当我向许多人提出这样的请求时
<soapenv:Body>
<con:person>
<person>
<name>John</name>
<age>18</age>
</person>
<person>
<name>Doe</name>
<age>50</age>
</person>
</con:person>
</soapenv:Body>
我不知道如何从请求中获取所有数据以及如何使用它们来创建这样的响应:
<soapenv:Body>
<con:result>
<person>
<name>John</name>
<age>18</age>
<country>France</country>
<city>Paris</city>
</person>
<person>
<name>Doe</name>
<age>50</age>
<country>Spain</country>
<city>Madrid</city>
</person>
</con:result>
</soapenv:Body>
请求和响应中的人数相同。
我希望我很清楚,我感谢你的答案。
答案 0 :(得分:2)
我设法做了类似的事情。首先,我将模拟响应对象定义为:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:stac="stackOverflow.question">
<soapenv:Header/>
<soapenv:Body>
<stac:result>
${personElements}
</stac:result>
</soapenv:Body>
</soapenv:Envelope>
然后我使用这个Groovy脚本为${personElements}
创建了内容:
import groovy.xml.MarkupBuilder
// An array from which county and city will be drawn randomly
def countryArray = [["Australia", "Perth"],
["Spain", "Madrid"],
["England","London"],
["Brazil", "Rio"]]
def random = new Random()
// create XmlHolder for request content
def holder = new com.eviware.soapui.support.XmlHolder( mockRequest.requestContent )
// Get the name and age values from the request
def requestItems = holder.getNodeValues( "//*:person/*:person/descendant::*")
def writer = new StringWriter()
def personElements = new MarkupBuilder(writer)
// Build the response elements
for (int index = 0; index < requestItems.size() - 1; index += 2) {
personElements.'stac:person'() {
'stac:name'(requestItems[index])
'stac:age'(requestItems[index+1])
// Choose a random county and city from the array
def randomIndex = random.nextInt(countryArray.size())
'stac:country'(countryArray[randomIndex][0])
'stac:city'(countryArray[randomIndex][1])
}
}
// Add the newly created elements to the response
context.personElements = writer.toString()
这给了我一个回复:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:stac="stackOverflow.question">
<soapenv:Header/>
<soapenv:Body>
<stac:result>
<stac:person>
<stac:name>Dolly Bonkers</stac:name>
<stac:age>42</stac:age>
<stac:country>Brazil</stac:country>
<stac:city>Rio</stac:city>
</stac:person>
<stac:person>
<stac:name>Mary Poppins</stac:name>
<stac:age>82</stac:age>
<stac:country>England</stac:country>
<stac:city>London</stac:city>
</stac:person>
<stac:person>
<stac:name>Bilbo Baggins</stac:name>
<stac:age>102</stac:age>
<stac:country>Australia</stac:country>
<stac:city>Perth</stac:city>
</stac:person>
<stac:person>
<stac:name>Johnny Hardcase</stac:name>
<stac:age>22</stac:age>
<stac:country>Spain</stac:country>
<stac:city>Madrid</stac:city>
</stac:person>
</stac:result>
</soapenv:Body>
</soapenv:Envelope>
该脚本只是从一个小数组中获取随机的城市和国家/地区值,但如果您想要对某些名称进行一致的响应,则可以提出更好的结果。