SoapUI

Rest OnRequest sample Groovy

import com.eviware.soapui.impl.rest.mock.RestMockResult

RestMockResult res = new RestMockResult( mockRequest )
String val ="{test:1234}"
mockRequest.httpResponse.writer << val
mockRequest.httpResponse.status = 200 
mockRequest.httpResponse.header = "Content-type: application/json"
mockRequest 
return res

WAR generation from SoapUI project

SoapUI is able to generate a WAR to create mock servers to run on Tomcat. The SoapUI project is stored inside the WAR in WEB-INF/soapui/soapuiprj.xml .

Sample onRequest script to call groovy scripts inside WEB-INF/soapui/. They can be changed on runtime after Tomcat has exploded the WAR file.

Inside the onRequestScript

import groovy.util.GroovyScriptEngine
import com.eviware.soapui.support.GroovyUtils
def gru = new GroovyUtils(context)
GroovyScriptEngine ge = new GroovyScriptEngine(gru.projectPath)
Binding b = new Binding()
b.setVariable("mockRequest", mockRequest);
ge.run( "scriptx.groovy" , b )

scriptx.groovy

import com.eviware.soapui.impl.rest.mock.RestMockResult

RestMockResult res = new RestMockResult( mockRequest )
println "Message in tomcat console"
def servletContext = mockRequest.httpRequest.getServletContext()
println servletContext.getServerInfo()
println servletContext.getServletContextName()
servletContext.log("servlet context log") //INFO: in file logs\localhost.XXXX.log in apache

String paramx = mockRequest.httpRequest.getParameter("paramx")
String rendered = String.format("<html><body><p>Page with %s</p></body></html>",paramx)
mockRequest.httpResponse.writer << rendered
mockRequest.httpResponse.setStatus(200)
mockRequest.httpResponse.setContentType("text/html")
mockRequest.httpResponse.setContentLength( rendered.length() )

SoapUI (last edited 2015-12-09 18:26:58 by static-wan-bl2-240-198-rev)