测试框架HttpClient

似水流年梦 2019-12-20

HttpClient是模拟Http协议客户端请求的一种技术,可以发送Get/Post等请求,要使用首先需要在pom文件中引入依赖包。

1.pom文件中引入依赖

<dependency>
        <groupId>org.apache.httpcomponents</groupId>
        <artifactId>httpclient</artifactId>
        <version>4.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.testng</groupId>
        <artifactId>testng</artifactId>
        <version>6.11</version>
    </dependency>
    <dependency>
       <groupId>org.json</groupId>
       <artifactId>json</artifactId>
       <version>20170516</version>
    </dependency>

2.第一个demo

public class Demo1 {
    @Test
    public void test1() throws ClientProtocolException, IOException {
        String result;
        HttpGet get = new HttpGet("http://www.baidu.com");
        HttpClient client = new DefaultHttpClient();
        HttpResponse response = client.execute(get);
        result=EntityUtils.toString(response.getEntity(),"utf-8");
        System.out.println(result);
    }
}

3.mock框架mock请求

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

4.将相关参数写入配置文件application.properties

test_url=http://localhost:8888
getCookies_uri=/getCookies
getWithCookies_uri=/getWithCookies
postWithCookies_uri=/postWithCookies

5.需要携带cookie才能访问的get请求

public class TestGetCookies {
    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 getWithCookies() throws ClientProtocolException, IOException {
        String uri=bundle.getString("getWithCookies_uri");
        HttpGet get = new HttpGet(url+uri);
        DefaultHttpClient client = new DefaultHttpClient();
        //设置cookies信息
        client.setCookieStore(cs);
        HttpResponse response = client.execute(get);
        //获取响应的状态码
        int codes = response.getStatusLine().getStatusCode();
        if (codes==200) {
            String result = EntityUtils.toString(response.getEntity(),"utf-8");
            System.out.println(result);
        }
       
    }

}

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方法,就可以正常返回信息

相关推荐

nalanrumeng / 0评论 2020-04-09