如何使用放心的请求设置cookie?

kar*_*oos 6 cookies http-get httpclient http-post rest-assured

我需要自动化其余的 API。API 受 Spring 安全保护。

以下是验证代码:

Response response = given().auth()
                    .form(userName, password,   FormAuthConfig.springSecurity().withLoggingEnabled(new LogConfig(captor, true)))
                    .post("/home/xyz.html");

            Assert.assertTrue("Error occurs", response.statusCode() == 302);

            if (response.statusCode() == 302) {
                Cookie cookie = response.getDetailedCookie("JSESSIONID");
                result.actualFieldValue = "User Authenticated: Session ID ->" + cookie.getValue();
                System.out.println("Cookie set : "+cookie.getValue());
                apiTestSessionID = cookie.getValue();
            }
Run Code Online (Sandbox Code Playgroud)

用户登录并返回 302 状态,表示重定向。我找到了 cookie 并设置了一些全局变量。

现在,我使用请求设置 cookie:

RequestSpecification reqSpecification = new RequestSpecBuilder().addCookie("JSESSIONID", AbstractBaseClass.apiTestSessionID).build();

            Map<String, String> parameters = new HashMap<String, String>();
            parameters.put("cstmrID", "000N0961");
            parameters.put("pageNumber", "1");
            parameters.put("pageSize", "10");
            parameters.put("sortColumnName", "FIELD_NM");
            parameters.put("sortDir", "asc");
            parameters.put("filterColumnName1", "");
            parameters.put("filterColumnName2", "USER_UPDT_EMAIL_ID");
            parameters.put("filterValue2", "");

            reqSpecification.queryParams(parameters);

            Response response = given().spec(reqSpecification).when().get("/service/customerConfig").thenReturn();
            System.out.println(response.asString());
Run Code Online (Sandbox Code Playgroud)

但作为回应,我得到了登录页面 HTML。我无法理解我哪里做错了。

假设:

  1. 由于使用 post 请求,返回 302,我是否需要重定向到下一个 url,然后使用 cookie 执行 get 请求。
  2. 这是使用请求设置 cookie 的正确方法吗?
  3. 我是否需要使用请求设置标头。如果是,则以下是标题信息。我需要设置所有这些吗?

GET /example.com/abc.html HTTP/1.1 主机:example.com 连接:keep-alive Cache-Control:max-age=0 Upgrade-Insecure-Requests:1 User-Agent:Mozilla/5.0(Windows NT 6.1; WOW64)为AppleWebKit / 537.36(KHTML,例如Gecko)铬/ 55.0.2883.87 Safari浏览器/ 537.36接受:text / html的,应用/ XHTML + xml的,应用/ XML; q = 0.9,图像/ WEBP,/ q = 0.8接受-编码:gzip、deflate、sdch 接受语言:en-US,en;q=0.8 Cookie:JSESSIONID=C70A69F1C60D93DC3F8AC564BDE3F4DE.lon2mcaqaapp002;__utma=185291189.2055499590.1460104969.1460104969.1460618428.2

小智 5

import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.http.Cookies;

private Cookie cookie;

@BeforeClass
public void exampleOfLogin() {
    String body = String.format("//json");
    cookies = RestAssured.given()
            .contentType(ContentType.JSON)
            .when()
            .body(body)
            .post("www.test_test.com")
            .then()
            .statusCode(200)
            .extract()
            .response()
            .getDetailedCookies();
}

@Test
public void performActionsBasedOnCookies() {
//set cookies before making a post request and check the returned status code
    RestAssured.given()
            .cookies(cookies)
            .contentType(ContentType.JSON)
            .when()
            .post("www.test_url.com")
            .then()
            .statusCode(200);
}
Run Code Online (Sandbox Code Playgroud)

  • 谢谢,这节省了我很多时间,您需要将变量重命名为“static private Cookies coockies” (3认同)

Kat*_*ina 2

我也是放心的新手,但我刚刚编写了类似的测试。

我建议你写一个私有authenticate()方法:

private static String authenticate() {
    given()
        .auth()
        .form(userName, password,FormAuthConfig.springSecurity().withLoggingEnabled(new LogConfig(captor, true)))
    when()
        .post("/home/xyz.html").
    thenReturn()
        .getDetailedCookie("JSESSIONID");
}
Run Code Online (Sandbox Code Playgroud)

然后在请求中使用cookie:

given()
    .cookie(authenticate())
when()
    .get("/service/customerConfig").
thenReturn();
Run Code Online (Sandbox Code Playgroud)

但我不知道你如何检查这里statusCode

.log().all()用于查看日志也是一个好主意。