如何使用Groovy测试步骤从SoapUI响应中获取Cookie? [英] How do I get a Cookie from a SoapUI response using a Groovy test step?

查看:2183
本文介绍了如何使用Groovy测试步骤从SoapUI响应中获取Cookie?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How do I get a Cookie from a SoapUI response using a Groovy test step?

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

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

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

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

我发现一个HACK 的问题,但这似乎不是正常的方式do it:

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


推荐答案

您的代码对我来说看起来不错。唯一的问题是(我想!)标题应该是Cookie,而不是Set-Cookie。
这可能有助于: http://siking.wordpress。 com / 2013/07/25 / soapui-cookie-management /

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/

这篇关于如何使用Groovy测试步骤从SoapUI响应中获取Cookie?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆