Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SoapUI - Automatically add custom SOAP headers to outgoing request

Tags:

soap

wsdl

soapui

So what I want to do is to automatically add SOAP header to every request that is generated in SoapUI as I've got hundreds of them and doing this manually is annoying.

Lets say that this is my example request generated from the WSDL which looks like that:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pol="http://something">
   <soapenv:Header>
   </soapenv:Header>
   <soapenv:Body>
      <pol:GetSomething>
         <tag1>3504</tag1>
         <tag2>ALL</tag2>
      </pol:GetSomething>
   </soapenv:Body>
</soapenv:Envelope>

and when I make the request I want SoapUI to modify it to look like that:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:pol="http://something">
   <soapenv:Header>
      <token xmlns="ns1">${TOKEN}</token>
      <user xmlns="ns2">user</user>
      <system xmlns="ns3">system</system>
   </soapenv:Header>
   <soapenv:Body>
      <pol:GetSomething>
         <tag1>3504</tag1>
         <tag2>ALL</tag2>
      </pol:GetSomething>
   </soapenv:Body>
</soapenv:Envelope>

Is it possible in SoapUI?

like image 665
Kyle Avatar asked Oct 01 '22 19:10

Kyle


1 Answers

In your testCase you can add a first step of type Groovy Script, in this script you can manipulate each request to add necessary elements on <soap:Header>, I give you an example that works for me:

def groovyUtils = new com.eviware.soapui.support.GroovyUtils( context );
def tcase = testRunner.testCase ;
// get total number of testSteps
def countTestSteps = tcase.getTestStepList().size();
// start with 1 to avoid groovy script testStep
for(i=1;i<countTestSteps;i++){

// get testStep
def testStep = tcase.getTestStepAt(i);
// get request
def request = testStep.getProperty('Request').getValue();
// get XML
def xmlReq = groovyUtils.getXmlHolder(request);
// get SOAPHEADER
def soapHeader = xmlReq.getDomNode("declare namespace soap='http://schemas.xmlsoap.org/soap/envelope/'; //soap:Header")
// document to create new elements
def requestDoc = soapHeader.getOwnerDocument()
// create new element
def newElem = requestDoc.createElementNS(null, "element");
// insert in header
soapHeader.insertBefore(newElem, soapHeader.getFirstChild());
// now put your new request in testStep
log.info xmlReq.getXml();
testStep.setPropertyValue('Request', xmlReq.getXml());
}

This sample code only add one new element on the <soap:header>, but you can modify it to add attributes, text content and more nodes. You can also take a look at:

dynamically create elements in a SoapUI request | SiKing

like image 105
albciff Avatar answered Oct 13 '22 09:10

albciff