簡體   English   中英

無法在 Gatling 中使用 addCookie 設置正確的身份驗證 cookie

[英]Can't set correct auth cookie by using addCookie in Gatling

我正在嘗試將我的登錄名移動到我在 Gatling 中進行的性能測試之外的應用程序。

登錄認證是通過存儲的Cookie,所以我想登錄app在另一個Class中獲取一個auth cookie,然后直接解析成gatling中的“addCookie”方法(所以我猜應該存儲在下次gatling性能的session中測試)

我在 class CookieSaver 中有方法 getAuthCookie

public String getAuthCookie() throws Exception {

    Properties prop = new Properties();

    prop.load(new FileInputStream("data.properties"));
    String user = prop.getProperty("username");
    String pass = prop.getProperty("password");
    String url = prop.getProperty("url");

    Map<Object, Object> data = new HashMap<>();
    data.put("username", user);
    data.put("password", pass);

    HttpRequest request2 = HttpRequest.newBuilder()
            .POST(buildFormDataFromMap(data))
            .uri(URI.create(url))
            .setHeader("User-Agent", "Java 11 HttpClient Bot")
            .header("Content-Type", "application/x-www-form-urlencoded")
            .build();
    HttpResponse<String> response = httpClient.send(request2, HttpResponse.BodyHandlers.ofString());
    String cookie = String.valueOf(response.headers().firstValue("set-cookie"));

    System.out.println(response.statusCode());

    System.out.println(response.body());
    String fixedCookie = cookie.replace("Optional", "");
    return fixedCookie;
}

我正在嘗試使用它 in.exec(addCookie........

ChainBuilder search;

{

    try {
        search = exec(flushCookieJar())
                .exec(flushHttpCache())

                .exec(addCookie(Cookie("set-cookie", authCookie.getAuthCookie()).withDomain("localhost")))
                
                .pause(1)

                .exec(
                        http("Contacts")
                                .get("/transactions/contacts")
                                .check(
                                        status().is(200).saveAs("Response")
                                ))
                .exec(session -> {
                    System.out.println("session1: " +session);
                    return session;
                })

                .exec(flushSessionCookies())


                .pause(1);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

}

我獲得了 Cookie 並將其正確設置為 session(運行后在日志中驗證),但是在嘗試運行下一步性能測試時我獲得了 401,這很簡單

我已經嘗試在性能測試中登錄到應用程序並且它工作正常,但我需要將登錄移到性能測試之外

這有效:

ChainBuilder search;

{

    try {
        search = exec(http("Home").get("/"))

                .exec(
                        http("Login")
                                .post("/login")
                                .formParam("username", "user")
                                .formParam("password", "pw")
                                .check(
                                        status().is(200)
                                )
                )
                .pause(1)

                .exec(
                        http("Contacts")
                                .get("/transactions/contacts")
                                .check(
                                        status().is(200).saveAs("Response")
                                ))
                .exec(session -> {
                    System.out.println("sesja1: " +session);
                    return session;
                })

                .exec(flushSessionCookies())


                .pause(1);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

}
  1. 當您發送 cookies 時,他們需要在Cookie header 中發送 go, Set-Cookie服務器用於創建 cookies 的內容
  2. 我不認為你真的把你的登錄“移出”你的性能測試,你只是在使用你的自定義代碼,它仍然會出現在指標中並影響響應時間和吞吐量(並保持與服務器的連接打開順便說一句),也許值得在掛鈎之前的某個地方模擬登錄並在真正的模擬開始時使用饋線
  3. 讓 Load Generator 和被測系統在同一台機器上運行並不是最好的主意,因為它們都會為操作系統資源而苦苦掙扎,即使是一個全面的APM 工具也無法告訴您瓶頸在哪里

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM