How do I get a Cookie from a SoapUI response using a Groovy test step?
I tried this Groovy code, but it's returning zero cookies (or null). This code is part of a test step that runs immediately after a standard REST request returns a result with the following header:
Set-Cookie: JSESSIONID=45C5E845A0C117E22D26DB04A64E5FD8; Path=/tcompany; HttpOnly
And the Groovy script I am using that fails to retrieve the Cookie is this:
import com.eviware.soapui.impl.wsdl.support.http.HttpClientSupport
def myCookieStore = HttpClientSupport.getHttpClient().getCookieStore()
def myCookies = myCookieStore.getCookies()
def sessionCookie
//log.info("Test:" + myCookies.get(0).getValue() )
if ( myCookies.size() > 0 ) {
myCookies.each {
log.info( "Cookie: " + it.name )
if( it.name == "JSESSIONID" ) {
sessionCookie = it
log.info("Found JSESSIONID cookie: " + it.value )
}
}
} else {
log.info("No cookies found in cookie store.")
}
//assert myCookies[0].value().contains("JSESSIONID")
return sessionCookie
I found a HACK around the problem but this seems like its not the normal way to do it:
def val = testRunner.testCase.testSteps['REST Test Request 1'].testRequest.response.getResponseHeaders()
log.info("---- all headers -------")
val.each() { hdrs ->
log.info hdrs
}
log.info("---- cookie jar contents -------")
def cjar = val.get("Set-Cookie")[0]
log.info ( "Cookie Jar: " + cjar )
def cookies = cjar.tokenize("\\;")
log.info("---- cookies -------")
cookies.each() { cookie ->
log.info "Cookie: " + cookie
}
log.info("---- separated -------")
cookies.each() { cookie ->
def pair = cookie.tokenize("\\=")
log.info( "[- Key: " + pair[0] + ", Val: " + pair[1] + "-]" )
}
log.info("---- end -------")