|
SoapUI每次请求之后都会把Cookie清理掉,保持一个干净的运行环境,这导致测试有登陆的场景变得麻烦了.
处理SoapUI的Cookie,有好种方式
1,用脚本发送http+cookie
2,HTTP中手动加hearder
3,开发一个soapUI插件
4,替换soapUI源码,支持保持cookie
一,跟踪
找原因真的好麻烦,打不了断点嘛.
1, jvisualvm 安装btrace插件,
2,监控http执行栈,
- <p>[java] view plain copy</p><p>@OnMethod(clazz = "org.apache.http.impl.client.CloseableHttpClient", method = "execute" ) </p><p>public static void Trace_exit(){ </p><p> println( "jstack() is :" ); </p><p> println( "[" ); </p><p> jstack(); </p><p> println( "]" ); </p><p>} </p><p>
- </p><p>
- </p><p>
- </p><p>
- </p><p>
- </p><p>
- </p><p>[java] view plain copy</p><p>org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java) </p><p>org.apache.http.impl.client.CloseableHttpClient.execute(CloseableHttpClient.java:57) </p><p>com.eviware.soapui.impl.wsdl.support.http.HttpClientSupport$Helper.execute(HttpClientSupport.java:233) </p><p>com.eviware.soapui.impl.wsdl.support.http.HttpClientSupport.execute(HttpClientSupport.java:323) </p><p>com.eviware.soapui.impl.wsdl.submit.transports.http.HttpClientRequestTransport.submitRequest(HttpClientRequestTransport.java:295) </p><p>com.eviware.soapui.impl.wsdl.submit.transports.http.HttpClientRequestTransport.sendRequest(HttpClientRequestTransport.java:225) </p><p>com.eviware.soapui.impl.wsdl.WsdlSubmit.run(WsdlSubmit.java:119) </p><p>java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) </p>
复制代码
说起来简单,这可搞了几个小时
看来关键的还是以下这个类:
com.eviware.soapui.impl.wsdl.support.http.HttpClientSupport
反编译以后找到一段神奇的代码:
- <p>
- </p><p>public static BasicHttpContext createEmptyContext() {</p><p> BasicHttpContext httpContext = new BasicHttpContext();</p><p> // BasicCookieStore cookieStore = new BasicCookieStore();</p><p> // httpContext.setAttribute("http.cookie-store", cookieStore);</p><p> return httpContext;</p><p>}</p>
复制代码
果然这个方法被它调用了:
com.eviware.soapui.impl.wsdl.submit.transports.http.BaseHttpRequestTransport.sendRequest()
SoapUI原来是设计是对REST和SOAP优化的,不需要cookie,而且理论上每个http请求是独立的,所以清理cookie
现在用反编译出的class替换它就以了.
以下groovy脚本,可以用来验证cookie还存在,也可以用来清理cookie
- <p>
- </p><p>[java] view plain copy</p><p>import com.eviware.soapui.impl.wsdl.support.http.*; </p><p>import org.apache.http.client.methods.HttpGet; </p><p>import org.apache.http.client.CookieStore; </p><p> // ht.execute(); </p><p> HttpGet get=new HttpGet("http://www.baidu.com"); </p><p> HttpGet get2=new HttpGet("http://www.baidu.com"); </p><p> // CookieStore ck; </p><p> </p><p> HttpClientSupport ht=new HttpClientSupport(); </p><p> </p><p> HttpClientSupport.getHttpClient().execute(get); </p><p> CookieStore ck= HttpClientSupport.getHttpClient().getCookieStore(); </p><p> </p><p> // HttpClientSupport.getHttpClient().execute(get2); </p><p> CookieStore ck2= HttpClientSupport.getHttpClient().getCookieStore(); </p>
复制代码
清理脚本(没验证):
[java] view plain copy
HttpClientSupport.getHttpClient().setCookieStore(<span style="color: rgb(128, 128, 128); font-family:
Menlo; font-size: 12pt; background-color: rgb(43, 43, 43);">new BasicCookieStore());</span>
|
|