Reputation: 73
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.
Upvotes: 1
Views: 6453
Reputation: 73
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.
Upvotes: 4