带cookie的HTTPPose请求
1、测试类:
package httpclient; import org.apache.http.HttpResponse; import org.apache.http.client.CookieStore; import org.apache.http.client.HttpClient; 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.JSONML; 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"); } //没有cookie @Test public void testGetCookies() throws IOException { String result; //从配置文件中拼接测试url String uri = bundle.getString("getCookees.uri"); String testUrl = this.url+uri; HttpGet get = new HttpGet(testUrl); HttpClient client = new DefaultHttpClient(); HttpResponse httpResponse= client.execute(get); result = EntityUtils.toString(httpResponse.getEntity()); System.out.println(result); } //获取cookie @Test public void testGetCookies1() throws IOException { String result; //从配置文件中拼接测试url String uri = bundle.getString("getCookees.uri"); String testUrl = this.url+uri; HttpGet get = new HttpGet(testUrl); DefaultHttpClient client = new DefaultHttpClient(); HttpResponse httpResponse= client.execute(get); result = EntityUtils.toString(httpResponse.getEntity()); System.out.println(result); //获取cookie信息 this.store = client.getCookieStore(); List<Cookie> cookieList = store.getCookies(); for (Cookie cookie:cookieList ) { String name = cookie.getName(); String value = cookie.getValue(); System.out.println("cookie name"+name+"cookie value"+value); } } @Test(dependsOnMethods = "testGetCookies1") public void testPostMethod() throws IOException { String uri = bundle.getString("test.post.with.cookies"); String testUrl = this.url + uri; //声明一个client对象,用来进行方法的执行 DefaultHttpClient client = new DefaultHttpClient(); //声明一个方法,这个方法就是post方法 HttpPost post = new HttpPost(testUrl); //添加参数 JSONObject param = new JSONObject(); param.put("name","wangkc"); 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(this.store); //执行post方法 HttpResponse response = client.execute(post); //获取响应结果 result = EntityUtils.toString(response.getEntity()); System.out.println(result); //处理结果,就是判断返回结果是否符合预期 //将返回的响应结果字符串转换成为json对象 JSONObject resultJson = new JSONObject(result); //具体的判断返回结果的值 String sucess = (String) resultJson.get("huhansan"); String status = (String) resultJson.get("status"); //具体的判断返回结果的值 Assert.assertEquals("success",sucess); Assert.assertEquals("1",status); } }
2、配置文件:
test.url=http://127.0.0.1:8888 dev.url=http://127.0.0.1:8888 getCookees.uri=/getCookies test.get.with.cookies=/get/with/cookies test.post.with.cookies=/post/with/cookies login=/login
3、mock文件:
[ { "description":"这是一个会返回cookies信息的get请求", "request":{ "uri":"/getCookies", "method":"get" }, "response":{ "cookies":{ "login":"true" }, "text":"恭喜你获得cookies信息成功" } }, { "description":"这是一个带cookies信息的get请求", "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":"wangkc", "age":"18" } }, "response":{ "status":200, "json":{ "huhansan":"success", "status":"1" } } } ]