Ask a Question

Listeners - Modifying Requests before posting to a Service

cdunne
Contributor

Listeners - Modifying Requests before posting to a Service

Hi,

We have a large number of optional elements in our Service requests. One of the issues I am coming accross regularly is a number of them cannot be passed as empty tags due to the type declaration.

We have build a framework driven from excel datasheets but we don't want to have to build large numbers of template requests based on different business rules.

Is there a way of only stripping the empty optional elements before a request is sent to the service? Does SoapUI Pro have this functionality or do I need to script something to handle it?

One way I thought would be to have a listener that calls a script to remove any empty elements from the request by default unless specified in the datasheet. Is this possible? I can't find any examples of using a listener on a particular test step or any examples of where I would start.

Thanks

Darren
16 REPLIES 16
omatzura
Super Contributor

Hi Darren,

before we dig into listeners; have you tried the "Remove Empty Content" option on the Request level (bottom left properties..)?

regards!

/Ole
eviware.com
cdunne
Contributor

Hi Ole,

Ah yes I just come across the feature, this morning while debugging a listener to do the same thing 😞 Hopefully that should fix most of the issues.

There may still be a time where I need to pass an empty element in the Request. The idea is to have predefined global Keyword that maps to a function, for example if the listener sees the value $#BLANK#$ in a request it will strip the value from the tags. Is there a way the listener can read a request after the "Remove Empty Content" has occured but before the Request is sent to the Service?

Another quick question too, is there a quick way of checking a response is a soap fault in a script assertion? for example

if (response == soapfault)
{
fail assertion
}

pass assertion


Cheers

Darren
omatzura
Super Contributor

Hi Darren,

here comes the code for a DemoRequestFilter that removes all elements containing "--remove--" from the outgoing request:

package soapui.demo 

import com.eviware.soapui.SoapUI
import com.eviware.soapui.impl.wsdl.submit.transports.http.BaseHttpRequestTransport
import com.eviware.soapui.impl.wsdl.submit.RequestFilter
import com.eviware.soapui.impl.wsdl.WsdlRequest
import com.eviware.soapui.model.iface.Response
import com.eviware.soapui.model.iface.SubmitContext

public class DemoRequestFilter implements RequestFilter
{
public void afterRequest( SubmitContext context, Response response )
{}

public void filterRequest( SubmitContext context, WsdlRequest wsdlRequest )

String content = (String) context.getProperty( BaseHttpRequestTransport.REQUEST_CONTENT ) 
if( content == null )
{
System.err.println( "Missing request content in context, skipping demofilter" )
}
else
{
System.out.println( "running demofilter" )
content = content.replaceAll( "<(.+)>--remove--</(\\1)>", "" )
context.setProperty( BaseHttpRequestTransport.REQUEST_CONTENT, content )
}
}
}


Put this in the soapuipro\bin\scripts folder and add a demo-listeners.xml file in the soapuipro\bin\listeners folder containing:

<?xml version="1.0" encoding="UTF-8"?>
<tns:soapui-listeners xmlns:tns="http://eviware.com/soapui/config">
    <tns:listener id="DemoListener" listenerClass="soapui.demo.DemoListener"
listenerInterface="com.eviware.soapui.model.testsuite.TestRunListener" />
</tns:soapui-listeners>


The listener uses the standard regex-based String.replaceAll to remove these tags, you should be able to tweak it as required.

Regarding the SOAP-Fault, you could try using the same code as the NotSoapFaultAssertion:

def responseContent = messageExchange.responseContent
def soapVersion = messageExchange.operation.getInterface().soapVersion
assert !com.eviware.soapui.impl.wsdl.support.soap.SoapUtils.isSoapFault( responseContent, soapVersion )


ok.. hope I got all that right 🙂

regards!

/Ole
eviware.com
cdunne
Contributor

Good Stuff Ole.

That's exactly what I wanted, couldn't figure out exactly where I need to apply the filter before it gets sent to the service. This will allow us to do quite a bit of manipulation of the requests based on predefined "keywords” supplied in the datasheets which map to specific functions.

Thanks
cdunne
Contributor

Hi Ole,

Been trying to get this to work but I think there is something wrong with the entry in the demo-listeners.xml file. Any chance you could confirm if this is correct?

Darren
omatzura
Super Contributor

Hi Darren,

hmm.. are you getting any errors at startup in any of the logs?

regards,

/Ole
eviware.com
cdunne
Contributor

Hi Ole,

Thats the thing, I'm getting no errors at all so I'm a bit lost at the moment to what is happening.

Cheers

Darren
omatzura
Super Contributor

Hi,

ok.. do you get a "Adding listeners from [..]" message in the soapui log at startup?

regards,

/Ole
eviware.com
cdunne
Contributor

Hi Ole,

these are the 2 outputs from the logs at startup and the SoapUI log.

08:53:23,034 INFO  [DefaultSoapUICore] initialized soapui-settings from [C:\Prog
ram Files\eviware\soapUI-Pro-2.0.3\bin\soapui-settings.xml]
08:53:23,112 INFO  [SoapUIProGroovyScriptEngineFactory] Initializing scripts fol
der [C:\Program Files\eviware\soapUI-Pro-2.0.3\bin\scripts]
08:53:23,987 INFO  [SoapUIProGroovyScriptEngineFactory] 16 classes loaded
08:53:23,987 INFO  [DefaultSoapUICore] Adding listeners from [C:\Program Files\e
viware\soapUI-Pro-2.0.3\bin\listeners\demo-listeners.xml]
08:53:24,190 INFO  [DefaultSoapUICore] Adding listener [class soapui.demo.DemoLi
stener]

============================================================================================


Tue May 27 08:53:23 BST 2008:INFO:initialized soapui-settings from [C:\Program Files\eviware\soapUI-Pro-2.0.3\bin\soapui-settings.xml]
Tue May 27 08:53:23 BST 2008:INFO:Initializing scripts folder [C:\Program Files\eviware\soapUI-Pro-2.0.3\bin\scripts]
Tue May 27 08:53:23 BST 2008:INFO:16 classes loaded
Tue May 27 08:53:23 BST 2008:INFO:Adding listeners from [C:\Program Files\eviware\soapUI-Pro-2.0.3\bin\listeners\demo-listeners.xml]
Tue May 27 08:53:24 BST 2008:INFO:Adding listener [class soapui.demo.DemoListener]
Tue May 27 08:53:25 BST 2008:INFO:Loading workspace from [C:\SoapUI_Sandbox\MeteorSOA_MW_Proxy_Phase2_soapui-projects_v0.3\Proof_of_Concept-workspace.xml]


Only thing I changedwas the class name to match the the one referenced in the Listener config file.


Cheers


Darren
cancel
Showing results for 
Search instead for 
Did you mean: