我如何通过使用放心的所有测试使用全局 header 请求

how I can use global header request through all tests using rest assured

我是新手,放心,我做了一个测试来提取访问令牌和一些其他参数,我想在请求中使用这些访问令牌 headers 进行所有其他测试

是否可以为我拥有的所有测试方法设置全局 header,或者对每个测试执行一次 运行 函数并注入所需的请求 header

 @Test
    public void getAccessToken (){
        Response body =
                given()
                        .params("username", "test@example.com","password","pass!","grant_type","password").post("https://example.test.com/Token")
                        .then()
                        .log().body()
                        .statusCode(200)
                        .extract().response();

        String access_token = body.path("access_token").toString();
        String token_type = body.path("token_type").toString();
        String refresh_token = body.path("refresh_token").toString();
        String Authorization = "bearer " + access_token;    }

}

更新 我在下面添加了以下部分,但现在得到 400 状态代码而不是 200 似乎我遗漏了一些东西,在下面我添加了示例其中一个有效,而另一个使用 RequestSpecification 无效

按预期工作

public class PermissionTests  {

    Response body =
            given()
                    .params("username", "user@example.com","password","pass!","grant_type","password").post("https://test.example.com/Token")
                    .then()
                    .log().body()
                    .statusCode(200)
                    .extract().response();

    String access_token = body.path("access_token").toString();
    String token_type = body.path("token_type").toString();
    String refresh_token = body.path("refresh_token").toString();
    String Authorization = "bearer " + access_token;


@Test
public void addNewGraph(){

    given()
            .header("officeId",1)
            .header("organizationId",1)
            .header("refreshToken",refresh_token)
            .header("Authorization",Authorization)
            .when()
            .get("https://test.example.com/api/cases/recent")
            .then()
            .log().body()
            .statusCode(200);
    }
})

这个示例不起作用 returns 400 知道我使用的是 TestNG 而不是 JUnit

  public class PermissionTests  {

private static RequestSpecification requestSpec;

@BeforeClass
public static void AuthSetup() {

    Response body =
            given()
                    .params("username", "user@example.com","password","pass!","grant_type","password").post("https://test.example.com/Token")
                    .then()
                    .log().body()
                    .statusCode(200)
                    .extract().response();

    String access_token = body.path("access_token").toString();
    String token_type = body.path("token_type").toString();
    String refresh_token = body.path("refresh_token").toString();
    String Authorization = "bearer " + access_token;

    HashMap<String, String> defaultHeader = new HashMap<>();
    defaultHeader.put("officeId","1");
    defaultHeader.put("organizationId","1");
    defaultHeader.put("refresh_token",refresh_token);
    defaultHeader.put("Authorization", Authorization);

        RequestSpecBuilder builder = new RequestSpecBuilder();
        builder.addHeader("officeId", "1");
        builder.addHeader("organizationId", "1");
        builder.addHeader("refresh_token", refresh_token);
        builder.addHeader("Authorization", Authorization);

        requestSpec = builder.build();


 // specification = new RequestSpecBuilder()
////                .addHeaders(defaultHeader)
//                .addHeader("officeId","1")
//                .addHeader("organizationId","1")
//                .addHeader("refresh_token",refresh_token)
//                .addHeader("Authorization",Authorization)
//                .build();

}

@Test
public void addNewGraph(){

    given()
            .spec(requestSpec)
            .log().all()
            .when()
            .get("https://test.example.com/api/cases/recent")
            .then()
            .log().body()
            .statusCode(200);
    }
})

这是上一个方法的日志

{
    "access_token": "eyJhbGciOiJodHRwOi8vd3d3LnczLm9yZy8yMDAxLzA0L3htbGRzaWctbW9yZSNobWFjLXNoYTI1NiIsInR5cCI6IkpXVCJ9.eyJodHRwOi8vc2NoZW1hcy54bWxzb2FwLm9yZy93cy8yMDA1LzA1L2lkZW50aXR5L2NsYWltcy9uYW1laWRlbnRpZmllciI6IjE2MSIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL25hbWUiOiJha2FtZWxAdHJhY2tlcnByb2R1Y3RzLmNvbSIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vYWNjZXNzY29udHJvbHNlcnZpY2UvMjAxMC8wNy9jbGFpbXMvaWRlbnRpdHlwcm92aWRlciI6IkFTUC5ORVQgSWRlbnRpdHkiLCJBc3BOZXQuSWRlbnRpdHkuU2VjdXJpdHlTdGFtcCI6ImJmODQ1MTEwLTk0ZDEtNGE0Yi05YzkxLThlNWQ1NDI2YTYxMyIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL2FkbWluIjoiVHJ1ZSIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL3NpZCI6IjE2MSIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL2VtYWlsYWRkcmVzcyI6ImFrYW1lbEB0cmFja2VycHJvZHVjdHMuY29tIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvb3JnYW5pemF0aW9uSWQiOiIxIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvb2ZmaWNlSWQiOiIxIiwicmVxdWlyZU1mYSI6IkZhbHNlIiwibmJmIjoxNTg4MTQ3MzMwLCJleHAiOjE1ODgxOTA1MzAsImlzcyI6Imh0dHBzOi8vdHJhY2tlcnByb2R1Y3RzLmNvbSIsImF1ZCI6ImM3MzJhY2U4MzRjZDQ4NTE5MGEzZTNhMjM2YTZhYzFkIn0.6pbDhYmyAXX9z46By4HxrCg_4HKRCSGq42FdhFoyA6s",
    "token_type": "bearer",
    "expires_in": 43199,
    "refresh_token": "d64dde50sd4be16ef209dcc5ss",
    "userName": "user@example.com",
    "userId": "sds",
    "deviceId": "eesdsde20d93e",
    "maxStringFieldLength": "10000",
    "opfs": "null",
    ".issued": "Wed, 29 Apr 2020 08:02:10 GMT",
    ".expires": "Wed, 29 Apr 2020 20:02:10 GMT"
}
Request method: GET
Request URI:    https://example.test.com/api/cases/recent
Proxy:          <none>
Request params: <none>
Query params:   <none>
Form params:    <none>
Path params:    <none>
Headers:        officeId=1
                organizationId=1
                refresh_token=d64dde50sd4be16ef209dcc5ss
                Authorization=bearer eyA1L2lkZW50aXR5L2NsYWltcy9uYW1laWRlbnRpZmllciI6IjE2MSIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzsdvY2xhaW1zL25hbWUiOiJha2FtZWxAdHJhY2tlcnByb2R1Y3RzLmNvbSIsImh0dHA6Ly9zY2hlbWFzLm1pY3Jvc29mdC5jb20vYWNjZXNzY29udHJvbHNlcnZpY2UvMjAxMC8wNy9jbGFpbXMvaWRlbnRpdHlwcm92aWRlciI6IkFTUC5ORVQgSWRlbnRpdHkiLCJBc3BOZXQuSWRlbnRpdHkuU2VjdXJpdHlTdGFtcCI6ImJmODQ1MTEwLTk0ZDEtNGE0Yi05YzkxLThlNWQ1NDI2YTYxMyIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL2FkbWluIjoiVHJ1ZSIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL3NpZCI6IjE2MSIsImh0dHA6Ly9zY2hlbWFzLnhtbHNvYXAub3JnL3dzLzIwMDUvMDUvaWRlbnRpdHkvY2xhaW1zL2VtYWlsYWRkcmVzcyI6ImFrYW1lbEB0cmFja2VycHJvZHVjdHMuY29tIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvb3JnYW5pemF0aW9uSWQiOiIxIiwiaHR0cDovL3NjaGVtYXMueG1sc29hcC5vcmcvd3MvMjAwNS8wNS9pZGVudGl0eS9jbGFpbXMvb2ZmaWNlSWQiOiIxIiwicmVxdWlyZU1mYSI6IkZhbHNlIiwibmJmIjoxNTg4MTQ3MzMwLCJleHAiOjE1ODgxOTA1MzAsImlzcyI6Imh0dHBzOi8vdHJhY2tlcnByb2RdZCI6ImM3MzJhY2U4MzRjZDQ4NTE5MGEzZTNhMjM2YTZhYzFkIn0.6pbDhYmyAXX9z46By4HxrCg_4HKRCSGq42FdhFoyA6s
                accept=application/json, text/plain, */*
Cookies:        <none>
Multiparts:     <none>
Body:           <none>

{
    "message": "GENERAL.ERROR",
    "errorId": "637237441331863542"
}

来自浏览器的原始请求header

您可以使用 Rest Assured 的 Specification Re Use,尤其是 RequestSpecBuilder(),因为您需要在不同的测试中重复使用请求数据

public class PermissionTests {

private static RequestSpecification requestSpec;

@BeforeClass
public static void AuthSetup() {

    Response body = given().log().all()
            .params("username", "user@example.com", "password", "pass!", "grant_type", "password")
            .post("https://test.example.com/Token").then().log().body().statusCode(200).extract().response();

    String access_token = body.path("access_token").toString();
    String token_type = body.path("token_type").toString();
    String refresh_token = body.path("refresh_token").toString();
    String Authorization = "bearer " + access_token;

    RequestSpecBuilder builder = new RequestSpecBuilder();
    builder.addHeader("officeId", "1");
    builder.addHeader("organizationId", "1");
    builder.addHeader("refresh_token", refresh_token);
    builder.addHeader("Authorization", Authorization);

    requestSpec = builder.build();

}

@Test
public void addNewGraph() {

    given().spec(requestSpec).log().all().when().get("https://test.example.com/api/cases/recent").then().log()
            .body().statusCode(200);
}
}

您可以使用 RestAssured.requestSpecification 设置将随每个请求一起发送的默认请求规范,例如

RestAssured.requestSpecification = new RequestSpecBuilder()
     .build().header("Authorization", "Bearer " + token);