= SoapUI = SoapUI is a free and open source cross-platform Functional Testing solution. http://www.soapui.org/ == Rest OnRequest sample Groovy == * http://www.soapui.org/apidocs/com/eviware/soapui/impl/rest/mock/RestMockRequest.html * http://www.soapui.org/apidocs/com/eviware/soapui/impl/rest/mock/RestMockResult.html * https://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletResponse.html * https://docs.oracle.com/javaee/6/api/javax/servlet/http/HttpServletRequest.html {{{ 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 }}} * http://www.soapui.org/scripting---properties/the-soapui-object-model.html == 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("
Page with %s
",paramx) mockRequest.httpResponse.writer << rendered mockRequest.httpResponse.setStatus(200) mockRequest.httpResponse.setContentType("text/html") mockRequest.httpResponse.setContentLength( rendered.length() ) }}} == Install SOAP UI == * wget https://s3.amazonaws.com/downloads.eviware/soapuios/5.5.0/SoapUI-x64-5.5.0.sh * sh SoapUI-x64-5.5.0.sh == Mock REST service == * File, Create empty project * New REST Mock service * right mouse button, Add new mock action * add new mock response tothe action * define response {"key":"hello world"} * define status 200 and media type application/json * show rest mock service, click play green triangle * access http://localhost:8080/action1 * https://www.soapui.org/soap-mocking/creating-dynamic-mockservices/ Dispatch script for action that has a Default response , in the MockResponse script area add the stuff below {{{ import com.eviware.soapui.impl.rest.mock.RestMockResult def getNumberArgument(){ def requestPath = mockRequest.getPath() def split = requestPath.tokenize('/') def index =0 for(item in split){ if(item=="number"){ break } index++ } def nr = split[index+1].toInteger() return nr } def buildResponse(nr){ def val ="" if(nr % 2 ==0){ val = '{"number":"evenxy"}' } else{ val = '{"number":"oddxy"}' } log.info("called build resp") return val } def createMockResponse(status,contentType,payload){ RestMockResult res = new RestMockResult( mockRequest ) // HttpServletResponse // ServletResponse response = mockRequest.httpResponse response.setStatus(status) response.setContentType(contentType) response.writer.print(payload) response.writer.flush() return res } def nr = getNumberArgument() def val = buildResponse(nr) def res = createMockResponse(200,"application/json",val) return res }}} == test endpoint == * create new mock action test inside rest mock service * create new mock response Default inside action * in default in the script tab add the following {{{ import com.eviware.soapui.impl.rest.mock.RestMockResult RestMockResult res = new RestMockResult( mockRequest ) String paramx = mockRequest.httpRequest.getParameter("paramx") String rendered = String.format("Page with %s
",paramx) mockRequest.httpResponse.writer.print(rendered) mockRequest.httpResponse.writer.flush() mockRequest.httpResponse.setStatus(200) mockRequest.httpResponse.setContentType("text/html") mockRequest.httpResponse.setContentLength( rendered.length() ) }}} * run the mock rest service * access using http://localhost:8080/test?paramx=asdf == get json endpoint == * create new mock action test inside rest mock service * create new mock response Default inside action * access http://localhost:8080/getjson?paramx=ggg * access http://localhost:8080/getjson * in default in the script tab add the following {{{ import com.eviware.soapui.impl.rest.mock.RestMockResult import groovy.json.JsonOutput /* comment*/ // comment httpRes = mockRequest.httpResponse class Out{ int id String name } class Other{ Out[] stuff } String paramx = mockRequest.httpRequest.getParameter("paramx") def elements = [ new Out(id:1,name:"aaaak1") ] elements << new Out(id:2,name: paramx ) elements.add( new Out(id:3,name:"aaaak3") ) def data = new Other(stuff : elements) log.info( data.stuff.size() ) log.info( data.stuff.getClass() ) String payload = JsonOutput.toJson(data) httpRes.setStatus(404) httpRes.setContentType("application/json") httpRes.setContentLength( payload.length() ) httpRes.writer.print(payload) httpRes.writer.flush() }}} == multipart form data == * curl -v -X POST -F 'paramx=hello' -F 'aaaa=bbbkkk' -F 'UploadedFiles=@/home/user/ldap.txt' http://localhost:8080/formdatatest {{{ import com.eviware.soapui.impl.rest.mock.RestMockResult def values="" for(def attach in mockRequest.getRequestAttachments() ){ log.info( attach.getAttachmentType().toString() ) log.info("name " + attach.getName() ) log.info("part " + attach.getPart() ) log.info("content type " + attach.getContentType() ) def r = new BufferedReader( new InputStreamReader(attach.getInputStream() ) ) def line="" while(line != null){ line = r.readLine() if(line!=null){ values += " " + line } } } String payload = " <<<<<<<< " + values + ">>>>>>>>> \n" log.info(payload) httpRes = mockRequest.httpResponse httpRes.setStatus(200) httpRes.setContentType("text/plain") httpRes.setContentLength( payload.length() ) httpRes.writer.print(payload) httpRes.writer.flush() }}}