测试框架HttpClient
HttpClient是模拟Http协议客户端请求的一种技术,可以发送Get/Post等请求,要使用首先需要在pom文件中引入依赖包。
1.pom文件中引入依赖
1 <dependency> 2 <groupId>org.apache.httpcomponents</groupId> 3 <artifactId>httpclient</artifactId> 4 <version>4.1.2</version> 5 </dependency> 6 <dependency> 7 <groupId>org.testng</groupId> 8 <artifactId>testng</artifactId> 9 <version>6.11</version> 10 </dependency> 11 <dependency> 12 <groupId>org.json</groupId> 13 <artifactId>json</artifactId> 14 <version>20170516</version> 15 </dependency>
2.第一个demo
1 public class Demo1 { 2 @Test 3 public void test1() throws ClientProtocolException, IOException { 4 String result; 5 HttpGet get = new HttpGet("http://www.baidu.com"); 6 HttpClient client = new DefaultHttpClient(); 7 HttpResponse response = client.execute(get); 8 result=EntityUtils.toString(response.getEntity(),"utf-8"); 9 System.out.println(result); 10 } 11 }
3.mock框架mock请求
1 [ 2 { 3 "description":"这是一个会返回cookies信息的get请求", 4 "request":{ 5 "uri":"/getCookies", 6 "method":"get" 7 }, 8 "response":{ 9 "cookies":{ 10 "login":"true" 11 }, 12 "text":"恭喜你获得cookies信息成功" 13 } 14 }, 15 { 16 "description":"这是一个要携带cookies信息才能访问的get请求", 17 "request":{ 18 "uri":"/getWithCookies", 19 "method":"get", 20 "cookies":{ 21 "login":"true" 22 } 23 }, 24 "response":{ 25 "text":"这是一个要携带cookies信息才能访问的get请求" 26 } 27 }, 28 { 29 "description":"模拟带cookies的post请求", 30 "request":{ 31 "uri":"/postWithCookies", 32 "method":"post", 33 "cookies":{ 34 "login":"true" 35 }, 36 "json":{ 37 "name":"huhansan", 38 "age":"18" 39 } 40 }, 41 "response":{ 42 "status":200, 43 "json":{ 44 "huhansan":"success", 45 "status":"1" 46 } 47 } 48 } 49 ]
4.将相关参数写入配置文件application.properties
1 test_url=http://localhost:8888 2 getCookies_uri=http://www.mamicode.com/getCookies 3 getWithCookies_uri=http://www.mamicode.com/getWithCookies 4 postWithCookies_uri=http://www.mamicode.com/postWithCookies
5.需要携带cookie才能访问的get请求
1 public class TestGetCookies { 2 private ResourceBundle bundle; 3 private CookieStore cs; 4 private String url; 5 6 @BeforeTest 7 public void beforeTest() { 8 bundle=ResourceBundle.getBundle("application"); 9 url=bundle.getString("test_url"); 10 } 11 12 @Test 13 public void getCookies() throws ClientProtocolException, IOException { 14 String uri=bundle.getString("getCookies_uri"); 15 HttpGet get = new HttpGet(url+uri); 16 DefaultHttpClient client = new DefaultHttpClient(); 17 HttpResponse response = client.execute(get); 18 String result=EntityUtils.toString(response.getEntity(),"utf-8"); 19 System.out.println(result); 20 //获取cookies信息 21 cs = client.getCookieStore(); 22 List<Cookie> cookies = cs.getCookies(); 23 for(Cookie c:cookies) { 24 String name=c.getName(); 25 String value=c.getValue(); 26 System.out.println(name+":"+value); 27 } 28 } 29 @Test(dependsOnMethods = {"getCookies"}) 30 public void getWithCookies() throws ClientProtocolException, IOException { 31 String uri=bundle.getString("getWithCookies_uri"); 32 HttpGet get = new HttpGet(url+uri); 33 DefaultHttpClient client = new DefaultHttpClient(); 34 //设置cookies信息 35 client.setCookieStore(cs); 36 HttpResponse response = client.execute(get); 37 //获取响应的状态码 38 int codes = response.getStatusLine().getStatusCode(); 39 if (codes==200) { 40 String result = EntityUtils.toString(response.getEntity(),"utf-8"); 41 System.out.println(result); 42 } 43 44 } 45 46 }
6.需要携带cookies才能访问的post请求
温馨提示: 本文由Jm博客推荐,转载请保留链接: https://www.jmwww.net/file/web/39922.html