Http自动跳转Https的接口测试实践

日前,需要做一个接口测试,对象是一个web系统,登录的时候需要用SSL连接,登录进入系统后自动转成普通的Http连接。
这种类型的web网站很常见,既保证了登录时的安全,也避免了整个系统使用SSL带来的系统性能下降的问题。
首先,我们把整个访问过程分解成3个过程:
  1. 访问http接口:http://***.com/?;
  2. 跳转到Https,登录用ssl连接:https://login.***.com;
  3. 再返回到http:http://***.com/common/getInfo.do?***;
那进行接口测试应该怎么做呢?
  • 初步尝试
一般一个http接口拿来,基本上直接用Httpclient模拟Http请求,通过get或post发送请求,进行接口测试。
inputurl="http://***.com/common/getEmployeeByDept.do?dept1=D032&dept2=D032002&dept3=D032010"
//创建HttpClientBuilder  
HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();  
//HttpClient  
CloseableHttpClient httpClient = httpClientBuilder.build();
HttpGet httpGet = new HttpGet(inputurl);
返回信息如下:
{"code":"400","data":null,"msg":"请重新登录。"}
通过尝试说明此种方法不可行。需要先跳转到Https进行登录操作才行。
  • 遇到问题,想解决方法
先去了解了下跳转机制,从Http>Https>Http的过程中,session是不会丢失的。就是说用户登录时候通过http访问了首页,然后点击登录按钮,跳转到https协议下,输入用户名密码等信息,登录成功,在服务端session放入当前登录用户信息. 这种跳转方式不会出现session丢失情况。而session的保持是通过cookie来维持的,就是说服务端跟客户端的会话是通过浏览器每次提交的一个cookie来表示,这个cookie的key是JSESSIONID,value是session的id,所以需要获取登录时的cookie。
  • 第二次尝试
尝试通过Httpclient获取cookie, 但是因为http跳转过去的登录页链接(https://login.***.com/accounts/login/?uid=523f70387b3d42439905170931f34a65&next=/openid/%3Fopenid.ns%3Dhttp%253A%252F%252Fspecs.openid.net%252Fauth%252F2.0%26openid.claimed_id%.........,是动态变化的,放弃此种方法。
看下面代码:
HttpHost httpHost = new HttpHost("localhost");  
HttpGet httpGet = new HttpGet("/https/");  
HttpResponse response = httpClient.execute(httpHost,httpGet);  
if(HttpStatus.SC_OK==response.getStatusLine().getStatusCode()){  
    HttpEntity entity = response.getEntity();  
}  
httpGet = new HttpGet("/https/index.jsp?cookie=write");  
response = httpClient.execute(httpHost,httpGet);  
FileWriter fw = new FileWriter("C:/cookie.txt");   
//读取cookie并保存文件  
List cookies = ((AbstractHttpClient) httpClient).getCookieStore().getCookies(); 
  • 再度尝试
接着就想尝试不通过https接口,通过直接访问web页的形式,获取cookie,首先当然想到了很熟悉的selenium,可是selenium需要打开浏览器,不是很方便。后来就想到了htmlunit ,这是一款开源的java 页面分析工具,读取页面后,可以有效的使用htmlunit分析页面上的内容。项目可以模拟浏览器运行,是个没有界面的浏览器,运行速度也是非常迅速。
使用Htmlunit非常简单:
1.首先在Maven项目中添加配置项:
<dependency>
<groupId>net.sourceforge.htmlunit</groupId>
<artifactId>htmlunit</artifactId>
<version>2.15</version>
</dependency>
2.使用Htmlunit提取cookie:
final WebClient webClient = new WebClient(BrowserVersion.CHROME);
webClient.getOptions().setUseInsecureSSL(true);
webClient.getOptions().setJavaScriptEnabled(false);
webClient.getOptions().setCssEnabled(false);
HtmlPage page = null;
page = webClient.getPage(loginUrl);
HtmlElement corpid = (HtmlElement) page.getElementById("id_corpid"); 
HtmlElement corppw = (HtmlElement) page.getElementById("id_corppw");
corpid.click();
String username = ph.readValue("userName");
corpid.type(username);
corppw.click();
String password = new String(dec.decodeBuffer(ph.readValue("password")));
corppw.type(password);
List loginBtn = (List) page.getByXPath("//div[@id='corp']/form/div[@]/button");
Page resultPage = loginBtn.get(0).click();
String EHRCookie = HtmlUnitUtil.getCookieHeader(webClient);
logger.info("获得openidpage cookie值: "+cookie);
return cookie;
执行会发现已经将cookie提取出来:
[INFO ]13:29:15,882,main,[Class]LoginUtil, [Method]getEHRCookie, 获得openidpage cookie值: JSESSIONID=8A1979AC21C24DD8622E41D89ABFF6F3.classa-***.org-8010; sessionid=23d2b3e737c34cb8d8898bbec94c6a11
3.接着在Httpclient中加载提取的cookie访问http接口:
//加载cookie
httpGet.addHeader(new BasicHeader("Cookie",cookie));
接着就可以不用管登录的过程用通常的方法,用Httpclient继续进行接口测试了。
  • 总结
在Https验证跳转到http的web系统的接口测试中,如果将Htmlunit和httpclient结合起来会简化登录验证过程,方便进行测试代码编写。
posted @ 2015-11-13 15:43  臭臭爸爸  阅读(5526)  评论(0编辑  收藏  举报