Wednesday, 6 November 2019

soapui MockService script to get value from request and set in response

REST MockService with Property msisdn, value empty (not sure if this is needed)

REST MockService with OnRequest Script containing:
log.info("request:\n")
log.info(mockRequest)
log.info(mockRequest.getRequestContent())
def holder = new com.eviware.soapui.support.XmlHolder( mockRequest.requestContent )
log.info(holder)
// no need to mess with urn or namespace
//holder.namespaces["ns"] = "http://mbleh"
//holder.namespaces["urn"] = "urn"
msisdn = holder.getNodeValue("//urn:msisdn")
log.info(msisdn)
log.info("set context.msisdn="+msisdn)
context.msisdn = msisdn
// and get/set several other parameters
for ( item in ["a", "b", "c"]) {
    log.info "Item : [$item]"
    v = holder.getNodeValue("//urn:" + item)
    log.info("set context." + item + "="+v)
    context.setProperty(item, v)
}

https://support.smartbear.com/readyapi/apidocs/soapui/com/eviware/soapui/support/XmlHolder.html

https://www.soapui.org/scripting-properties/property-expansion.html


Example of incoming request:
POST /path HTTP/1.1
soapaction: XXXX
content-type: text/xml;charset=UTF-8
Host: hostname:8081
Content-Length: 525
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:va.a.a">
   <soapenv:Header/>
   <soapenv:Body>
      <urn:REQUEST>
         <urn:msisdn>11144444483</urn:msisdn>
         <urn:a>A</urn:a>
      </urn:REQUEST>
   </soapenv:Body>
</soapenv:Envelope>


Part of Mock Action, Response:  see ${msisdn}  var set in Mock Service context
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <RESPONSE version="1.0" xmlns="urn:va.a.a" xmlns:ns2="urn:vb.b.b" xmlns:ns3="urn:vc.c.c" xmlns:ns4="urn:vd.d.d" xmlns:ns5="urn:ve.e.e">
         <a>A</a>
         <msisdn>${msisdn}</msisdn>
      </RESPONSE>
   </soapenv:Body>
</soapenv:Envelope>


And the actual response sent: (msisdn filled in via script and context.msisdn)
HTTP/1.1 200 OK
Content-Type: application/xml
Content-Length: 888
Server: Jetty(6.1.26)
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <RESPONSE version="1.0" xmlns="urn:va.a.a" xmlns:ns2="urn:vb.b.b" xmlns:ns3="urn:vc.c.c" xmlns:ns4="urn:vd.d.d" xmlns:ns5="urn:ve.e.e">
         <a>A</a>
         <msisdn>11144444483</msisdn>
      </RESPONSE>
   </soapenv:Body>
</soapenv:Envelope>



No comments: