【接口测试】使用httpClient获取cookies+携带获取的cookies访问get接口

数据准备

在本机或者远端机器安装部署moco-runner(参考:https://blog.csdn.net/qq_32706349/article/details/80472445

这里我们只需要准备Json文件:

[
{
    "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请求"
    }
  }
]

  代码实现

import org.apache.http.HttpEntity;
import org.apache.http.client.CookieStore;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.cookie.Cookie;
import org.apache.http.impl.client.BasicCookieStore;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.cookie.BasicClientCookie;
import org.apache.http.util.EntityUtils;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.*;

public class CookieTest {

    //用来存储Cookies信息的变量
    private CookieStore store;
//调用获取cookie信息的get接口 @Test public void testGetCookies() throws IOException { //获取body String result=null; store = new BasicCookieStore(); CloseableHttpClient client = HttpClients.custom().setDefaultCookieStore(store).build(); HttpGet get=new HttpGet("http://localhost:30080/getcookies"); CloseableHttpResponse response=client.execute(get); HttpEntity entity = response.getEntity(); result= EntityUtils.toString(entity,"utf-8"); System.out.println(result); List<Cookie> cookieList=store.getCookies(); for (Cookie cookie:cookieList){ String name =cookie.getName(); String value = cookie.getValue(); System.out.println("访问/getcookies接口成功,cookie name = "+name+", cookie value = "+value); } response.close(); client.close(); }
//调用带cookie信息的get接口
//方式一:
//通过创建cookieStore存放cookie,以此传递到httpclient @Test public void testGetWithCookies1() throws IOException { //创建cookieStore存储cookie CookieStore cookieStore=new BasicCookieStore(); //创建cookie对象 BasicClientCookie cookie=new BasicClientCookie("login","true"); cookie.setDomain("localhost"); cookie.setPath("/"); System.out.println(cookie); cookieStore.addCookie(cookie); CloseableHttpClient client=HttpClients.custom().setDefaultCookieStore(cookieStore).build(); HttpGet get=new HttpGet("http://localhost:30080/get/with/cookies"); CloseableHttpResponse response=client.execute(get); HttpEntity entity=response.getEntity(); String result=EntityUtils.toString(entity,"utf-8"); System.out.println(result); response.close(); client.close(); }
//方式二:
//依赖获取cookie接口,store传递cookie
   @Test(dependsOnMethods = {"testGetCookies"})
    public void testGetWithCookies2() throws IOException {
        CloseableHttpClient client=HttpClients.custom().setDefaultCookieStore(this.store).build();
        HttpGet get=new HttpGet("http://localhost:30080/get/with/cookies");
        CloseableHttpResponse response=client.execute(get);
        HttpEntity entity=response.getEntity();
        String result=EntityUtils.toString(entity,"utf-8");
        System.out.println(result);

        response.close();
        client.close();
    }
}

  

总结一下遇到的问题:

1、网上教程大多是使用旧的DefaultHttpClient实现获取cookie,但是httpclient新版本已经不推荐,这里使用的是CloseableHttpClient通过setDefaultCookieStore()方式传递cookie

旧的实现方式参考:https://blog.csdn.net/lt326030434/article/details/80449856

新的实现方式参考:https://blog.csdn.net/wsrfljygracie/article/details/89181318

2、在cookie传递的使用方面,找了一些资料,不停调试测试,靠着IDEA的提示和资料调通。

使用CookieStore保持会话的使用方法参考:https://www.cnblogs.com/ssgao/p/8829056.html

需要注意:cookie需要设置name、value、domain(这个没有设置会一直调不通)

3、注意moco-runner部署的服务器地址,如果是测试服务器本机就是localhost,如果是远端机器,url需要填写具体的url地址

4、代码实现是比较简单的远端调用接口的形式,没有使用本地resources目录配置下application.properties、foo.json,后续会学习。

另外,个人感觉json放在本地资源文件调用的必要性不是很大,看工具的使用范围,自用部署在服务器即可,也不需要每次修改后提交代码。

转发请说明出处,谢谢。

如果还有其他问题,欢迎交流。

猜你喜欢

转载自www.cnblogs.com/fatCat1/p/12160607.html