如何在Rest assured中通过头部传递授权令牌?

14

尝试使用Rest assured自动化进行API测试

@Test
public void Login() {
    Response resp = given().
            body("{\"phone_number\":\"2222222222\",\"\r\n" + 
                    "               + \" \"country_code\": \"+91\",\"\r\n" + 
                    "               + \" \"login_type\": 0}").
            when().
            contentType(ContentType.JSON).
            post("http://url/api/v1/login");

    System.out.println(resp.asString());
}
2个回答

24

添加授权标头。

Response resp = given().header("Authorization", "Bearer "+token).body(...

欲了解更多信息,请参见此处


0

首先创建名为httpHeaderManager()的方法

为头部创建一个Header类的对象,并将其存储到ArrayList中,例如:

public static Headers httpHeaderManager(){
    Header contentType = new Header("Content-Type","application/json");
    Header authorization = new Header("Authorization", "your token");
    List<Header> headerList = new ArrayList<Header>();
    headerList.add(contentType);
    headerList.add(authorization);
    Headers header = new Headers(headerList);

}

调用httpHeaderManager()方法的第二次

@Test
public void create(){
  Response response = 
            given()
            .headers(httpHeaderManager())

}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接