模拟Rest-Service响应(json和xml),使用soapui [英] Mock Rest-Service Response (json and xml), using soapui
问题描述
我使用soapui来模拟休息服务。为此,我需要编写一个脚本(OnRequest脚本)以模拟对我的请求的响应。
因此,我想模拟2种类型的响应: application / json和application / xml。但我不想读任何响应文件。我只想在脚本中编写响应(OnRequest脚本,在soapui中)。我已经找到了一种通过读取响应文件的方法。在我的情况下,我不需要读取任何文件,而是将响应写入脚本(OnRequest脚本,在soapui中)。
有人说我可以使用groovy像这样:
$ b
if(mockRequest.getMethod()==GET&& mediaType ==application / xml ){
mockRunner.returnFile(httpResponse,new File(path +test.xml))
log.infoContent-Type application / xml返回的响应
return new com .eviware.soapui.impl.wsdl.mock.WsdlMockResult(mockRequest)
}
但是该示例确实读取了一个文件,我想使用groovy,但只是在groovy脚本中编写答案。 发现如何解决这个问题:
if(mockRequest.getMethod()==GET&& mediaType ==应用/ XML){
WsdlMockResult mockResult =新WsdlMockResult(mockRequest)
DEF的HttpResponse = mockRequest.httpResponse
httpResponse.setCont entType(application / xml; charset = utf-8)
httpResponse.writer<< <根><使用者> ABC< /使用者>< /根> 中
httpResponse.status = 200
return mockResult
}
享受这个解决方案,如果你需要的话。
I'm getting a confused, because I'm beginner in mocking rest-services.
I'm using soapui to mock a rest-service. To do so, I need to write a script (OnRequest Script) in order to mock the responses to my request.
So, I want to mock 2 types of response : application/json and application/xml. But I don't want to read any response-file. I just want to write the response in the script (OnRequest Script, in soapui).
I've found a way to do it by reading a response file. In my case i don't need to read any file, rather write the response into the script (OnRequest Script, in soapui).
Some say that I can use groovy like this:
if (mockRequest.getMethod() == "GET" && mediaType=="application/xml"){
mockRunner.returnFile(httpResponse,new File(path + "test.xml"))
log.info "Response returning for Content-Type application/xml"
return new com.eviware.soapui.impl.wsdl.mock.WsdlMockResult(mockRequest)
}
But the example does read a file, I want to use groovy but only writing the answer in groovy script.
i've finally found how to fix that :
if (mockRequest.getMethod() == "GET" && mediaType=="application/xml"){
WsdlMockResult mockResult = new WsdlMockResult(mockRequest)
def httpResponse = mockRequest.httpResponse
httpResponse.setContentType("application/xml;charset=utf-8")
httpResponse.writer << "<root><user>ABC</user></root>"
httpResponse.status = 200
return mockResult
}
Enjoy this solution, if you need it.
这篇关于模拟Rest-Service响应(json和xml),使用soapui的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!