How do I get a Cookie from a SoapUI response using

2019-08-06 02:44发布

问题:

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 -------")

回答1:

Your code looks fine to me. The only problem is that (I think!) the header should be "Cookie", as opposed to "Set-Cookie". This might help: http://siking.wordpress.com/2013/07/25/soapui-cookie-management/



回答2:

For SoapUi test, I add header : http://mariemjabloun.blogspot.com/2014/11/set-webservice-authentication-and-set.html