MoinMoin Logo
  • Comments
  • Immutable Page
  • Menu
    • Navigation
    • RecentChanges
    • FindPage
    • Local Site Map
    • Help
    • HelpContents
    • HelpOnMoinWikiSyntax
    • Display
    • Attachments
    • Info
    • Raw Text
    • Print View
    • Edit
    • Load
    • Save
  • Login

Navigation

  • Start
  • Sitemap

Upload page content

You can upload content for the page named below. If you change the page name, you can also upload content for another page. If the page name is empty, we derive the page name from the file name.

File to load page content from
Page name
Comment

Revision 18 as of 2020-05-03 17:21:20
  • SoapUI

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("<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() )

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
  • 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("<html><body><p>Page with %s</p></body></html>",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

  • MoinMoin Powered
  • Python Powered
  • GPL licensed
  • Valid HTML 4.01