Listeners - Modifying Requests before posting to a Service
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-24-2008
09:11 AM
04-24-2008
09:11 AM
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
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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2008
01:19 AM
04-25-2008
01:19 AM
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
before we dig into listeners; have you tried the "Remove Empty Content" option on the Request level (bottom left properties..)?
regards!
/Ole
eviware.com
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2008
02:30 AM
04-25-2008
02:30 AM
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
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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2008
04:46 AM
04-25-2008
04:46 AM
Hi Darren,
here comes the code for a DemoRequestFilter that removes all elements containing "--remove--" from the outgoing request:
Put this in the soapuipro\bin\scripts folder and add a demo-listeners.xml file in the soapuipro\bin\listeners folder containing:
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:
ok.. hope I got all that right 🙂
regards!
/Ole
eviware.com
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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
04-25-2008
07:30 AM
04-25-2008
07:30 AM
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
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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2008
10:09 AM
05-26-2008
10:09 AM
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
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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2008
10:14 AM
05-26-2008
10:14 AM
Hi Darren,
hmm.. are you getting any errors at startup in any of the logs?
regards,
/Ole
eviware.com
hmm.. are you getting any errors at startup in any of the logs?
regards,
/Ole
eviware.com
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2008
10:34 AM
05-26-2008
10:34 AM
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
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
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-26-2008
02:57 PM
05-26-2008
02:57 PM
Hi,
ok.. do you get a "Adding listeners from [..]" message in the soapui log at startup?
regards,
/Ole
eviware.com
ok.. do you get a "Adding listeners from [..]" message in the soapui log at startup?
regards,
/Ole
eviware.com
- Bookmark
- Subscribe
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
05-27-2008
12:59 AM
05-27-2008
12:59 AM
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
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
