本文主要包含一下无点。
1、如何get请求如何获取cookie信息。 getCookieStore()
2、如何发送带cookie信息的post 请求。 setCookieStore(); HttpPost();
3、testng依耐测试,带cookie信息的post请求需要依耐于获取cookie信息的get请求。 @Test(dependsOnMethods = {"getTestCookie"})
4、如何使用moco框架构建带返回cookie信息的get请求和一个带cookie和json参数的post请求且响应结果也是json格式。
5、如何获取请求返回为json格式的参数。
6、如何对响应结果进行断言。
java代码
1 package com.course.httpclient.cookies; 2 3 import org.apache.http.HttpResponse; 4 import org.apache.http.client.CookieStore; 5 import org.apache.http.client.HttpClient; 6 import org.apache.http.client.methods.HttpGet; 7 import org.apache.http.client.methods.HttpPost; 8 import org.apache.http.cookie.Cookie; 9 import org.apache.http.entity.StringEntity; 10 import org.apache.http.impl.client.DefaultHttpClient; 11 import org.apache.http.util.EntityUtils; 12 import org.json.JSONObject; 13 import org.testng.Assert; 14 import org.testng.annotations.BeforeTest; 15 import org.testng.annotations.Test; 16 import java.io.IOException; 17 import java.util.List; 18 import java.util.Locale; 19 import java.util.ResourceBundle; 20 21 public class MyCookieForPost { 22 private String url; 23 private ResourceBundle bundle;//用于读取配置文件 24 private CookieStore store;//用于存储cookies信息 25 26 @BeforeTest 27 public void beforeTest() { 28 29 bundle = ResourceBundle.getBundle("application", Locale.CHINA); 30 //上行代码用于读取配置文件,baseName和类在同一目录的resource文件中 31 url = bundle.getString("test.url"); 32 //上行代码是获取配置文件中的域名 33 } 34 35 @Test 36 public void getTestCookie() throws IOException { 37 38 String result; 39 String uri = bundle.getString("getCookies.uri"); 40 //以上代码是获取配置文件中的getCookies.uri对应的路径 41 String testurl = this.url + uri; 42 HttpGet get = new HttpGet(testurl); 43 System.out.println("这是testurl的地址" + testurl); 44 // HttpClient client = new DefaultHttpClient(); HttpClient无法获取cookie信息 45 DefaultHttpClient client = new DefaultHttpClient(); 46 //创建HttpClient对象,用于执行get请求 47 HttpResponse response = client.execute(get); 48 System.out.println("这是response的值" + response); 49 result = EntityUtils.toString(response.getEntity(), "utf-8"); 50 System.out.println(result); 51 //以下代码是获取cookie信息 52 this.store = client.getCookieStore(); 53 List<Cookie> cookkielist = store.getCookies(); 54 for (Cookie cookie : cookkielist) { 55 String name = cookie.getName(); 56 String value = cookie.getValue(); 57 System.out.println("cookie name=" + name + " cookie value=" + value); 58 } 59 60 61 } 62 63 @Test(dependsOnMethods = {"getTestCookie"}) 64 public void postTestMethods() throws IOException { 65 66 String uri = bundle.getString("testPostWithCookies.uri"); 67 //以上代码是获取配置文件中的getCookies.uri对应的路径 68 String testurl = this.url + uri; 69 DefaultHttpClient client = new DefaultHttpClient();//声明一个client对象用来进行方法的执行 70 HttpPost post = new HttpPost(testurl);//什么一个post方法 71 JSONObject param = new JSONObject();//添加参数 接口要求post参数的格式是json格式 72 param.put("name", "zhangshan"); 73 param.put("age", "18"); 74 post.setHeader("Accept-Encoding", "gzip, deflate"); //设置请求信息,设置header 75 post.setHeader("Content-Type", "application/json"); //设置请求信息,设置header 76 StringEntity entity = new StringEntity(param.toString(), "utf-8");//将参数信息添加到方法中 77 post.setEntity(entity);//将post方法和参数绑定在一起 78 String result; //声明一个对象,进行响应结果的存储 79 client.setCookieStore(this.store);//设置cookies信息 80 HttpResponse response = client.execute(post);//执行post方法 81 result = EntityUtils.toString(response.getEntity());//获取响应结果 82 JSONObject resultJson = new JSONObject(result); //将返回的响应结果字符串转换成json对象 83 84 String success = resultJson.getString("zhangshan");//获取响应的参数值 85 int status = resultJson.getInt("status"); 86 // String status = (String) resultJson.get("status");//获取响应的参数值 87 Assert.assertEquals("成功", success); //处理结果,判断期望结果和实际结果 88 89 Assert.assertEquals(1, status); 90 91 } 92 93 }
接口信息配置文件application.properties如下
1 test.url=http://127.0.0.1:8888 2 getCookies.uri=/getCookies 3 testPostWithCookies.uri=/post/with/cookies
moco模拟接口信息如下
[ { "description": "这是一个会返回cookies信息的get请求", "request": { "uri": "/getCookies", "method": "get" }, "response": { "headers": { "Content-Type": "text/html;charset=gbk" }, "cookies": { "login": "true" }, "text": "恭喜你获得cookies信息成功" } }, { "description": "这是一个带cookies的post请求", "request": { "uri": "/post/with/cookies", "method": "post", "cookies": { "login": "true" }, "json": { "name": "zhangshan", "age": "18" } }, "response": { "headers": { "Content-Type": "text/html;charset=gbk" }, "status": 200, "json": { "zhangshan": "成功", "status": 1 } } } ]