QWERTY
14 years agoOccasional Contributor
Script for adding cookie to request header
Hi
A while ago I came across the problem with adding a cookie to a SoapUI (Pro) request to authenticate against a WSL protected server. I eventually solved the problem by writing a script that does it and now I want to share this with the community. The script comes in two flavours; lite and full, they both works fine although the full version has a lot more finesse. All directories given below is based on the SoapUI Pro 4.0.1 setup, if you have another setup you directory will differ.
Please note that these scripts only works with SoapUI Pro and the full script is only tested using SoapUI Pro 4.0.1
This post will deal with the lite version and the next post will deal with the full version.
Open the demo-listeners.xml in the C:\Program Files\SmartBear\soapUI-Pro-4.0.1\bin\listeners directory and paste the following:
<?xml version="1.0" encoding="UTF-8"?>
<tns:soapui-listeners xmlns:tns="http://eviware.com/soapui/config">
<!-- The below demo-listeners are described in the extensions overview at
http://www.soapui.org/architecture/extensions.html -->
<!-- This demo-listener is not reloadable -->
<!--
<tns:listener id="DemoListener" listenerClass="soapui.demo.DemoListener"
listenerInterface="com.eviware.soapui.model.testsuite.TestRunListener" />
-->
<!-- This demo-listener is reloadable -->
<tns:listener id="DemoListener"
listenerClass="com.eviware.soapui.support.scripting.listeners.ScriptTestRunListener"
groovyClass="soapui.demo.DemoListener"
listenerInterface="com.eviware.soapui.model.testsuite.TestRunListener"/>
<tns:listener id="DemoListener" listenerClass="com.eviware.soapui.support.scripting.listeners.ScriptRequestFilter"
groovyClass="soapui.demo.DemoRequestFilter" listenerInterface="com.eviware.soapui.impl.wsdl.submit.RequestFilter"/>
</tns:soapui-listeners>
This will set up a listener to the DemoRequestFilter class located in the DemoRequestFilter.groovy file in the C:\Program Files\SmartBear\soapUI-Pro-4.0.1\bin\scripts\soapui\demo directory. Now you have to modify this file to add cookies to request headers so open the file and paste the following:
package soapui.demo;
import com.eviware.soapui.impl.wsdl.submit.RequestFilter;
import com.eviware.soapui.model.iface.Request;
import com.eviware.soapui.model.iface.Response;
import com.eviware.soapui.model.iface.SubmitContext;
import com.eviware.soapui.support.types.StringToStringsMap
import com.eviware.soapui.SoapUI;
public class DemoRequestFilter implements RequestFilter {
@Override
public void afterRequest(SubmitContext arg0, Request arg1) {}
public void afterRequest(SubmitContext context, Response response) {}
@Override
public void filterRequest(SubmitContext context, Request wsdlRequest) {
SoapUI.log.info("****running demofilter******")
//The Header field in the SoapUI request header
String header = "Cookie"
//The type of cookie
String type = "type 1"
// "type 2"
//The WSL cookie. Paste the new cookie here and it will replace the old cookie or be added if the header is empty
String cookie = "My cookie"
def headers = new StringToStringsMap()
headers.put(header, type + cookie)
wsdlRequest.requestHeaders = headers
}
}
The indents disappeared but using a somewhat nifty text editor such as Notepad++ you can do it nice and clean. Now, some comments on the script:
*) After updating the listener you must restart SoapUI but the groovy file can be updated without restarting as SoapUI reloads it (check the log)
*) Paste your cookie where it sais "My cookie"
*) Some headers require a type before the value of the cookie, e.g. "WSL-external=", update the type with whatever string you want. If no type is needed just comment it out.
*) The put method takes to arguments: header and value. These corresponds to the two fields in the header tab in SoapUI. Change the header string if needed.
*) The script will create a new StringToString map object each time it's run effectively overwriting the previous header
*) requestHeaders is a property of the wsdlRequest object. It is the actual request being sent and is a StringToStringMap type
Next, the full version, a bit more complex
A while ago I came across the problem with adding a cookie to a SoapUI (Pro) request to authenticate against a WSL protected server. I eventually solved the problem by writing a script that does it and now I want to share this with the community. The script comes in two flavours; lite and full, they both works fine although the full version has a lot more finesse. All directories given below is based on the SoapUI Pro 4.0.1 setup, if you have another setup you directory will differ.
Please note that these scripts only works with SoapUI Pro and the full script is only tested using SoapUI Pro 4.0.1
This post will deal with the lite version and the next post will deal with the full version.
Open the demo-listeners.xml in the C:\Program Files\SmartBear\soapUI-Pro-4.0.1\bin\listeners directory and paste the following:
<?xml version="1.0" encoding="UTF-8"?>
<tns:soapui-listeners xmlns:tns="http://eviware.com/soapui/config">
<!-- The below demo-listeners are described in the extensions overview at
http://www.soapui.org/architecture/extensions.html -->
<!-- This demo-listener is not reloadable -->
<!--
<tns:listener id="DemoListener" listenerClass="soapui.demo.DemoListener"
listenerInterface="com.eviware.soapui.model.testsuite.TestRunListener" />
-->
<!-- This demo-listener is reloadable -->
<tns:listener id="DemoListener"
listenerClass="com.eviware.soapui.support.scripting.listeners.ScriptTestRunListener"
groovyClass="soapui.demo.DemoListener"
listenerInterface="com.eviware.soapui.model.testsuite.TestRunListener"/>
<tns:listener id="DemoListener" listenerClass="com.eviware.soapui.support.scripting.listeners.ScriptRequestFilter"
groovyClass="soapui.demo.DemoRequestFilter" listenerInterface="com.eviware.soapui.impl.wsdl.submit.RequestFilter"/>
</tns:soapui-listeners>
This will set up a listener to the DemoRequestFilter class located in the DemoRequestFilter.groovy file in the C:\Program Files\SmartBear\soapUI-Pro-4.0.1\bin\scripts\soapui\demo directory. Now you have to modify this file to add cookies to request headers so open the file and paste the following:
package soapui.demo;
import com.eviware.soapui.impl.wsdl.submit.RequestFilter;
import com.eviware.soapui.model.iface.Request;
import com.eviware.soapui.model.iface.Response;
import com.eviware.soapui.model.iface.SubmitContext;
import com.eviware.soapui.support.types.StringToStringsMap
import com.eviware.soapui.SoapUI;
public class DemoRequestFilter implements RequestFilter {
@Override
public void afterRequest(SubmitContext arg0, Request arg1) {}
public void afterRequest(SubmitContext context, Response response) {}
@Override
public void filterRequest(SubmitContext context, Request wsdlRequest) {
SoapUI.log.info("****running demofilter******")
//The Header field in the SoapUI request header
String header = "Cookie"
//The type of cookie
String type = "type 1"
// "type 2"
//The WSL cookie. Paste the new cookie here and it will replace the old cookie or be added if the header is empty
String cookie = "My cookie"
def headers = new StringToStringsMap()
headers.put(header, type + cookie)
wsdlRequest.requestHeaders = headers
}
}
The indents disappeared but using a somewhat nifty text editor such as Notepad++ you can do it nice and clean. Now, some comments on the script:
*) After updating the listener you must restart SoapUI but the groovy file can be updated without restarting as SoapUI reloads it (check the log)
*) Paste your cookie where it sais "My cookie"
*) Some headers require a type before the value of the cookie, e.g. "WSL-external=", update the type with whatever string you want. If no type is needed just comment it out.
*) The put method takes to arguments: header and value. These corresponds to the two fields in the header tab in SoapUI. Change the header string if needed.
*) The script will create a new StringToString map object each time it's run effectively overwriting the previous header
*) requestHeaders is a property of the wsdlRequest object. It is the actual request being sent and is a StringToStringMap type
Next, the full version, a bit more complex