[英]addCookie method doesn't adds the cookie to an httpServletResponse
[英]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);
}
}
Cookie
header 中發送 go, Set-Cookie
是服務器用於創建 cookies 的內容
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.