测试框架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=/getCookies
3 getWithCookies_uri=/getWithCookies
4 postWithCookies_uri=/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请求

public class TestPostCookies {
    private ResourceBundle bundle;
    private CookieStore cs;
    private String url;
    
    @BeforeTest
    public void beforeTest() {
        bundle=ResourceBundle.getBundle("application");
        url=bundle.getString("test_url");
    }
    @Test
    public void getCookies() throws ClientProtocolException, IOException {
        String uri=bundle.getString("getCookies_uri");
        HttpGet get = new HttpGet(url+uri);
        DefaultHttpClient client = new DefaultHttpClient();
        HttpResponse response = client.execute(get);
        String result=EntityUtils.toString(response.getEntity(),"utf-8");
        System.out.println(result);
        //获取cookies信息
        cs = client.getCookieStore();
        List<Cookie> cookies = cs.getCookies();
        for(Cookie c:cookies) {
            String name=c.getName();
            String value=c.getValue();
            System.out.println(name+":"+value);
        }
    }
    @Test(dependsOnMethods = {"getCookies"})
    public void postWithCookies() throws ClientProtocolException, IOException {
        String uri=bundle.getString("postWithCookies_uri");
        HttpPost post = new HttpPost(url+uri);
        DefaultHttpClient client = new DefaultHttpClient();
        //添加post请求参数,用json传值,pom文件中引入json依赖
        JSONObject json = new JSONObject();
        json.put("name", "huhansan");
        json.put("age", "18");
        //设置请求头信息
        post.setHeader("content-type", "application/json");
        //将参数信息添加到方法中
        StringEntity entity = new StringEntity(json.toString(), "utf-8");
        post.setEntity(entity);
        //设置cookies信息
        client.setCookieStore(cs);
        //发送请求
        HttpResponse response=client.execute(post);
        //接收响应信息
        String result=EntityUtils.toString(response.getEntity(),"utf-8");
        //将返回的响应结果字符串转化成为json对象
        JSONObject resultJson = new JSONObject(result);
        //获取响应结果值
        String success=resultJson.getString("huhansan");
        String status=resultJson.getString("status");
        Assert.assertEquals("success", success);
        Assert.assertEquals("1", status);
        
    }
}

7.启动mock应用,然后运行test方法,就可以正常返回信息

原文地址:https://www.cnblogs.com/heyuling/p/12071713.html