如何使用Groovy测试步骤从SoapUI响应中获取Cookie?

时间:2013-12-17 16:58:20

标签: web-services cookies session-cookies soapui

如何使用Groovy测试步骤从SoapUI响应中获取Cookie?

我尝试了这个Groovy代码,但它返回零cookie(或null)。此代码是在标准REST请求返回带有以下标头的结果后立即运行的测试步骤的一部分:

Set-Cookie: JSESSIONID=45C5E845A0C117E22D26DB04A64E5FD8; Path=/tcompany; HttpOnly

我使用的Groovy脚本无法检索Cookie是这样的:

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

我在问题周围找到了一个HACK ,但这似乎不是正常的做法:

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

2 个答案:

答案 0 :(得分:2)

您的代码对我来说很好。唯一的问题是(我认为!)标题应该是“Cookie”,而不是“Set-Cookie”。 这可能会有所帮助:http://siking.wordpress.com/2013/07/25/soapui-cookie-management/

答案 1 :(得分:0)