第五节 测试框架HttpClient(三)
2019-11-09 本文已影响0人
我吃小虾米
使用POST方法携带cookie请求
第一步 创建MAVEN依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.51</version>
</dependency>
第二步 创建json文件
[
{
"description": "这是一个响应带有cookie的请求",
"request": {
"uri": "/getcookies",
"method": "get"
},
"response": {
"status": "200",
"text": "获取cookies成功",
"cookies": {
"login": "true"
},
"headers": {
"Content-Type": "text/html;charset=gbk"
}
}
},
{
"description":"这是一个request请求中带cookies、使用json格式传参、返回结果为json格式的post请求",
"request":{
"uri":"/postwithcookiesandjson",
"method":"post",
"cookies":{
"login":"true"
},
"json":{
"name":"lalala",
"age":"28"
}
},
"response":{
"status":200,
"json":{
"code":"0",
"msg":"success",
"p2pdata":{
"name":"moguzhixing",
"address":"beijing"
}
}
}
}
第三步 创建postCookie类
private String urL;
private ResourceBundle bundle;
private CookieStore cookieStore;
@BeforeTest
public void beforeTest(){
bundle=ResourceBundle.getBundle("application", Locale.CHINA);
urL=bundle.getString("test.url");
}
@Test
public void getCookiesTest() throws Exception{
CloseableHttpClient httpClient= HttpClients.createDefault();
// 声明get请求
HttpGet httpget=new HttpGet(this.urL+bundle.getString("test.getCookies.uri"));
// 发送get请求
HttpClientContext content = HttpClientContext.create();
CloseableHttpResponse response=httpClient.execute(httpget,content);
if (response.getStatusLine().getStatusCode()==200){
HttpEntity entity=response.getEntity();
String string= EntityUtils.toString(entity,"utf-8");
// 获取cookies
this.cookieStore=content.getCookieStore();
List<Cookie> cookieList = cookieStore.getCookies();
for (Cookie cookie:cookieList){
String name=cookie.getName();
String value = cookie.getValue();
System.out.println("cookie name:"+name+";cookie value:"+value);
}
}
response.close();
httpClient.close();
}
@Test(dependsOnMethods = {"getCookiesTest"})
public void postRequestCookies() throws IOException {
HttpPost httpPost=new HttpPost(this.urL+bundle.getString("test.postRequestCookies.json.uri"));
CloseableHttpClient client= HttpClients.createDefault();
HttpClientContext context1=HttpClientContext.create();
context1.setCookieStore(this.cookieStore);
// 添加参数
JSONObject param=new JSONObject();
param.put("name","lalala");
param.put("age","28");
System.out.println(param);
//设置请求头信息
httpPost.setHeader("Content-Type","application/json");
// 将参数添加到方法中
StringEntity entity=new StringEntity(param.toString(),"utf-8");
httpPost.setEntity(entity);
//声明一个对象用来进行相应结果的存储
String result;
// 执行post方法
HttpResponse response=client.execute(httpPost,context1);
// 获取响应结果
result= EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result);
// 处理响应结果,判断返回结果是否符合预期
// 将返回结果的相应结果字符串转换为Json对象
JSONObject resultJson=JSONObject.parseObject(result);
System.out.println(resultJson);
// 获取到结果值
String success= (String) resultJson.get("msg");
String code= (String) resultJson.get("code");
// 具体的判断返回结果的值
Assert.assertEquals(success,"success");
Assert.assertEquals(code,"0");
}
第四步 执行json文件
java -jar ./moco-runner-0.11.0-standalone.jar http -p 8809 -c postcookies.json
第五步 执行postCookie.class可以得到如下结果
cookie name:login;cookie value:true
{"name":"lalala","age":"28"}
{"code":"0","msg":"success","p2pdata":{"name":"moguzhixing","address":"beijing"}}
{"msg":"success","code":"0","p2pdata":{"address":"beijing","name":"moguzhixing"}}
===============================================
Default Suite
Total tests run: 2, Passes: 2, Failures: 0, Skips: 0
===============================================
总结一下吧!过程有点坎坷啊,主要在JSON与String类型的转换上。主要原因是使用的json依赖版本不一样,导致写法不一样。只能说百度真的是强大啊!!多问度娘,问题还是都能自己解决的。主要是不放弃,最终还是有回报的。
比心
ε=ε=ε=(#>д<)ノ好想吐槽啊!!!!!
=====================================================================
我能找到一个合适的版本,我真的好羡慕你们,年纪轻轻的就能遇到才华横溢的我⁄(⁄ ⁄•⁄ω⁄•⁄ ⁄)⁄