Forum Discussion
QWERTY
14 years agoOccasional Contributor
The full version of the script is a bit more complex. First, you have to add a new listener to the listeners folder: cookie-listeners.xml:
<?xml version="1.0" encoding="UTF-8"?>
<tns:soapui-listeners xmlns:tns="http://eviware.com/soapui/config">
<tns:listener id="CookieListener"
listenerClass="com.eviware.soapui.support.scripting.listeners.ScriptRequestFilter"
groovyClass="soapui.cookie.Cookie"
listenerInterface="com.eviware.soapui.impl.wsdl.submit.RequestFilter"/>
</tns:soapui-listeners>
I never got this to work in conjunction with the demo-listeners.xml so remove that one. Now you have to create a new folder called cookie like this: C:\Program Files\SmartBear\soapUI-Pro-4.0.1\bin\scripts\soapui\cookie
As you can see it's on the same level as the demo folder.
Next, in that folder, create Cookie.groovy and paste the following:
package soapui.cookie;
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.SoapUI;
import com.eviware.soapui.support.types.StringToStringMap
public class Cookie implements RequestFilter {
//The different types
private String TYPE1 = "type 1"
private String TYPE2 = "type 2"
private String NOWSL = "noWSL"
//The Header field in the SoapUI test request header
private String HEADERFIELD = "Cookie"
//The WSL cookie. Paste the new cookies here and they will replace the old cookies or be added if the header is empty
private String type 1 cookie = "type 1 cookie"
private String type 2 cookie = "type 2 cookie"
//The SoapUI test request header
private def soapReqHeader
@Override
public void afterRequest(SubmitContext arg0, Request arg1) {}
public void afterRequest(SubmitContext context, Response response) {}
@Override
public void filterRequest(SubmitContext context, Request wsdlRequest) {
String wslCookie
String wslType
//The header taken from the request is a StringToStringMap property of the wsdlRequest object
soapReqHeader = wsdlRequest.requestHeaders
String endpoint = getEndpointType(wsdlRequest)
switch (endpoint) {
case TYPE1 :
SoapUI.log.info("**** type 1 cookie ****")
wslType = "type 1"
wslCookie = type 1 cookie
populateHeader(soapReqHeader, HEADERFIELD, wslType, wslCookie)
break
case TYPE2 :
SoapUI.log.info("**** type 2 cookie ****")
wslType = "type 2"
wslCookie = type 2 cookie
populateHeader(soapReqHeader, HEADERFIELD, wslType, wslCookie)
break
default:
SoapUI.log.info("***** No cookie needed ****")
soapReqHeader = new StringToStringMap()
break
}
wsdlRequest.requestHeaders = soapReqHeader
}
/* Populates the header by either replacing an existing cookie
or adding one if no header can he found.
The header consists of a key (supposed to be "cookie" or
"Cookie") and a value.
The toString() method invoked on the header returns a hidden
"\n" that must be removed for matching purposes.
The substring(9) method call is to remove the key ("Cookie : ")
from the value to be replaced.
*/
private void populateHeader(def reqHeader, String headerName, String prefix, String cookie) {
if ((reqHeader.toString().length() && cookie.trim().length() > 1) && reqHeader.getKeys()[0] ==~ /(?i)cookie/) {
SoapUI.log.info("**** Header has cookie ****")
if (reqHeader.toString().replaceAll("(\\r|\\n)", "").endsWith(cookie)) {
SoapUI.log.info("**** Cookies match, do nothing ****")
} else {
SoapUI.log.info("**** Updating cookie ****")
reqHeader.replace(reqHeader.getKeys()[0], reqHeader.toString().substring(9).replaceAll("(\\r|\\n)", ""), prefix + cookie)
}
} else if (cookie.trim().length() > 1) {
SoapUI.log.info("**** Header is empty, adding cookie ****")
reqHeader.put(headerName, prefix + cookie)
} else {
SoapUI.log.info("**** FYI: The cookie is empty ****")
}
}
/* getEndpoint() returns the entire path including the service name.
The regexp filters out the essential information and braces
potential "https" additions
*/
private String getEndpointType(Request wsdlRequest) {
String endpointType
if (wsdlRequest.getEndpoint() =~ "type 1 endpoint") { endpointType = TYPE1 }
else if (wsdlRequest.getEndpoint() =~ "type 2 endpoint") { endpointType = TYPE2 }
else { endpointType = NOWSL }
return endpointType
}
}
To set this up you should put in the cookies, cookie types and the endpoint urls. The endpoint urls you add in the getEndpointTypes method and they should not include "http://" nor the services. The idea is that a certain cookie type is used for a certain url regardless if it's http or https and regardless of the specific service. This is why a regexp is used. For example,
http://myservice.com/wsdl/service_1
and
https://myservice.com/wsdl/service_2
both use the same cookie type but you don't want two cases for this. The regexp pattern =~ "myservice.com" makes sure that the same type is used in both cases. When set up, the script will automatically change type and cookie based on the used endpoint so you can use it with automated scrips running against different urls authenticated by different cookies. Of course, you have to add cookies, types, cases and endpoint types yourself.
<?xml version="1.0" encoding="UTF-8"?>
<tns:soapui-listeners xmlns:tns="http://eviware.com/soapui/config">
<tns:listener id="CookieListener"
listenerClass="com.eviware.soapui.support.scripting.listeners.ScriptRequestFilter"
groovyClass="soapui.cookie.Cookie"
listenerInterface="com.eviware.soapui.impl.wsdl.submit.RequestFilter"/>
</tns:soapui-listeners>
I never got this to work in conjunction with the demo-listeners.xml so remove that one. Now you have to create a new folder called cookie like this: C:\Program Files\SmartBear\soapUI-Pro-4.0.1\bin\scripts\soapui\cookie
As you can see it's on the same level as the demo folder.
Next, in that folder, create Cookie.groovy and paste the following:
package soapui.cookie;
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.SoapUI;
import com.eviware.soapui.support.types.StringToStringMap
public class Cookie implements RequestFilter {
//The different types
private String TYPE1 = "type 1"
private String TYPE2 = "type 2"
private String NOWSL = "noWSL"
//The Header field in the SoapUI test request header
private String HEADERFIELD = "Cookie"
//The WSL cookie. Paste the new cookies here and they will replace the old cookies or be added if the header is empty
private String type 1 cookie = "type 1 cookie"
private String type 2 cookie = "type 2 cookie"
//The SoapUI test request header
private def soapReqHeader
@Override
public void afterRequest(SubmitContext arg0, Request arg1) {}
public void afterRequest(SubmitContext context, Response response) {}
@Override
public void filterRequest(SubmitContext context, Request wsdlRequest) {
String wslCookie
String wslType
//The header taken from the request is a StringToStringMap property of the wsdlRequest object
soapReqHeader = wsdlRequest.requestHeaders
String endpoint = getEndpointType(wsdlRequest)
switch (endpoint) {
case TYPE1 :
SoapUI.log.info("**** type 1 cookie ****")
wslType = "type 1"
wslCookie = type 1 cookie
populateHeader(soapReqHeader, HEADERFIELD, wslType, wslCookie)
break
case TYPE2 :
SoapUI.log.info("**** type 2 cookie ****")
wslType = "type 2"
wslCookie = type 2 cookie
populateHeader(soapReqHeader, HEADERFIELD, wslType, wslCookie)
break
default:
SoapUI.log.info("***** No cookie needed ****")
soapReqHeader = new StringToStringMap()
break
}
wsdlRequest.requestHeaders = soapReqHeader
}
/* Populates the header by either replacing an existing cookie
or adding one if no header can he found.
The header consists of a key (supposed to be "cookie" or
"Cookie") and a value.
The toString() method invoked on the header returns a hidden
"\n" that must be removed for matching purposes.
The substring(9) method call is to remove the key ("Cookie : ")
from the value to be replaced.
*/
private void populateHeader(def reqHeader, String headerName, String prefix, String cookie) {
if ((reqHeader.toString().length() && cookie.trim().length() > 1) && reqHeader.getKeys()[0] ==~ /(?i)cookie/) {
SoapUI.log.info("**** Header has cookie ****")
if (reqHeader.toString().replaceAll("(\\r|\\n)", "").endsWith(cookie)) {
SoapUI.log.info("**** Cookies match, do nothing ****")
} else {
SoapUI.log.info("**** Updating cookie ****")
reqHeader.replace(reqHeader.getKeys()[0], reqHeader.toString().substring(9).replaceAll("(\\r|\\n)", ""), prefix + cookie)
}
} else if (cookie.trim().length() > 1) {
SoapUI.log.info("**** Header is empty, adding cookie ****")
reqHeader.put(headerName, prefix + cookie)
} else {
SoapUI.log.info("**** FYI: The cookie is empty ****")
}
}
/* getEndpoint() returns the entire path including the service name.
The regexp filters out the essential information and braces
potential "https" additions
*/
private String getEndpointType(Request wsdlRequest) {
String endpointType
if (wsdlRequest.getEndpoint() =~ "type 1 endpoint") { endpointType = TYPE1 }
else if (wsdlRequest.getEndpoint() =~ "type 2 endpoint") { endpointType = TYPE2 }
else { endpointType = NOWSL }
return endpointType
}
}
To set this up you should put in the cookies, cookie types and the endpoint urls. The endpoint urls you add in the getEndpointTypes method and they should not include "http://" nor the services. The idea is that a certain cookie type is used for a certain url regardless if it's http or https and regardless of the specific service. This is why a regexp is used. For example,
http://myservice.com/wsdl/service_1
and
https://myservice.com/wsdl/service_2
both use the same cookie type but you don't want two cases for this. The regexp pattern =~ "myservice.com" makes sure that the same type is used in both cases. When set up, the script will automatically change type and cookie based on the used endpoint so you can use it with automated scrips running against different urls authenticated by different cookies. Of course, you have to add cookies, types, cases and endpoint types yourself.