测试框架httpclent 4.HttpClient Post方法实现

时间:2021-09-01 19:20:23

startupWithCookies.json

[
{
"description":"这是一个会返回cookies信息的get请求",
"request":{
"uri":"/getCookies",
"method":"get" },
"response":{
"cookies":{
"login":"true"
},
"text":"恭喜获得cookies信息成功"
} }, {
"description":"这是一个带cookies的请求",
"request":{
"uri":"/get/with/cookies",
"method":"get",
"cookies":{
"login":"true"
}
},
"response":{
"text":"这是一个需要携带cookies信息才能访问的get请求"
}
}, {
"description":"这是一个带cookies的post请求",
"request":{
"uri":"/post/with/cookies",
"method":"post",
"cookies":{
"login":"true"
},
"json":{
"name":"huhanshan",
"age":"18"
}
},
"response":{
"status":200,
"json":{
"huhanshan":"success",
"status":"1"
}
}
} ]

新建一个 MyCookiesForPost.java 类

package com.course.httpclient.cookies;

import org.apache.http.HttpResponse;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.cookie.Cookie;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import org.testng.Assert;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test; import java.io.IOException;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle; public class MyCookiesForPost { private String url;
private ResourceBundle bundle; //用来存储cookies信息的变量
private CookieStore store; @BeforeTest
public void beforeTest(){
bundle = ResourceBundle.getBundle("application",Locale.CHINA);
url = bundle.getString("test.url"); } @Test
public void testGetGookies() throws IOException {
String result;
String uri = bundle.getString("getCookies.uri");
HttpGet get = new HttpGet(this.url + uri);
DefaultHttpClient client = new DefaultHttpClient();
HttpResponse response = client.execute(get); result = EntityUtils.toString(response.getEntity(),"utf-8");
System.out.println(result); //获取cookies的信息,因为cookie里面不只是一个,他是一个cookie类型的list
store = client.getCookieStore();
List<Cookie> cookieList = store.getCookies(); for(Cookie cookie : cookieList){
String name = cookie.getName();
String value = cookie.getValue();
System.out.println("name = "+name+",value = "+value);
}
} @Test(dependsOnMethods = "testGetGookies")
public void testPostMethod() throws IOException {
String uri = bundle.getString("test.post.with.cookies");
//拼接最终的地址
String testUrl = url+uri; //声明一个方法post
HttpPost post = new HttpPost(testUrl); //声明一个Client对象
DefaultHttpClient client = new DefaultHttpClient(); //添加参数
JSONObject param = new JSONObject();
param.put("name","huhanshan");
param.put("age","18"); //设置请求头信息 header
post.setHeader("content-type","application/json"); //将参数信息添加到方法中
StringEntity entity = new StringEntity(param.toString(),"utf-8");
post.setEntity(entity); //声明一个对象来进行响应结果的存储
String result; //设置cookies信息
client.setCookieStore(store); //执行post方法
HttpResponse response = client.execute(post); //获取响应状态码
int statusCode = response.getStatusLine().getStatusCode(); if(statusCode==200){
//获取响应结果
result = EntityUtils.toString(response.getEntity(),"uft-8"); //将返回的响应结果字符串转化为json对象
JSONObject resultJson = new JSONObject(result); //判断返回结果的值
String status = resultJson.getString("status");
String success = resultJson.getString("huhanshan");
Assert.assertEquals("1",status,"status的类型是"+status.getClass());
Assert.assertEquals("success",success);
System.out.println(result);
}
} }

首先在terminal运行命令:  java -jar ./moco-runner-0.12.0-standalone.jar http -p 8888 -c startupWithCookies.json