如何在restassured中设置会话属性?在我的应用程序代码中,我们有如下内容
String userId= request.getSession().getAttribute("userid")
如何在这里(在restassured测试用例中)设置userId作为会话属性?
如何为所有请求(多个后续请求)维护相同的会话?
当我发送多个请求时,它认为每个请求都是新的,并且服务器端的会话正在失效,我希望在后续调用之间保持会话。
我尝试在cookie中设置jsessionid并在第二个请求中发送它,但当我在服务器端调试时,它没有加载创建的会话,而是创建了不同的会话,因此它没有显示我第一次发送请求时在会话中设置的属性。
当我用direct HttpClient尝试同样的方法时,它工作了,而和RestAssured一样,它不工作。
使用HttpClient的代码是这样的
HttpClient httpClient = util.getHttpClient(); //第一个请求
HttpResponse response=httpClient.execute(postRequest); 我从响应中提取了jessionid,并将其设置在第二个请求中
HttpGet getRequest = new HttpGet(Client.endPointUrl);
getRequest.addHeader("content-type", "application/json");
getRequest.addHeader("accept", "application/json");
getRequest.addHeader("Origin", Client.endPointUrl);
getRequest.addHeader("Referer", Client.endPointUrl);
getRequest.addHeader("Auth-Token", authToken);
getRequest.addHeader("Set-Cookie", jsessionId);//设置响应中提取的jessionid后的第二次请求
HttpResponse eventsResponse = httpClient.execute(getRequest); 上面的代码工作得很好,我得到了预期的响应。一个观察结果是,我使用相同的httpClient对象来调用这两个请求。
当我尝试用RestAssured做同样的事情时,它不工作。
RestAssured.baseURI = "http://localhost:8080";
Response response=RestAssured.given().header("Content-Type","application/json").
header("Origin","http://localhost:8080").
header("Referer","http://localhost:8080").
body("{"+
"\"LoginFormUserInput\":{"+
"\"username\":\"test\","+
"\"password\":\"password\""+
"}"+
"}")
.when().post("/sample/services/rest/validateLogin").then().extract().response();
JsonPath js=Util.rawToJson(response);
String sessionId=js.get("sessionID");
System.out.println(sessionId);
for (Header header:response.getHeaders()) {
if ("Set-Cookie".equals(header.getName())) {
id= header.getValue().split(";")[0].trim();
String[] arr=jsessionId.split("=");
jsessionId=arr[0];
break;
}
}
response=RestAssured.given().header("Auth-Token",sessionId).header("Content-Type","application/json").
cookie("JSESSIONID",jsessionId).
header("Origin","http://localhost:8080").
header("Referer","http://localhost:8080").
body("{}").
when().
post("/sample/services/rest/getAllBooks").then().contentType("").extract().response();我尝试使用以下代码对所有请求重用相同的httpclient,但不起作用
RestAssured.config = RestAssured.config().httpClient( new HttpClientConfig().reuseHttpClientInstance());发布于 2019-06-25 04:10:51
您需要在Rest Assured中使用会话过滤器
https://github.com/rest-assured/rest-assured/wiki/Usage#session-support
https://stackoverflow.com/questions/50981245
复制相似问题