【问题标题】:maintaining sessions with RestAssured与 RestAssured 保持会话
【发布时间】:2018-06-22 05:32:35
【问题描述】:

如何在放心中设置会话属性?在我的应用程序代码中,我们有这样的东西

String userId= request.getSession().getAttribute("userid")

这里如何设置 userId 为 session 属性(在放心的测试用例中)?

如何为所有请求(多个后续请求)保持相同的会话?

当我发送多个请求时,它会将每个请求视为新请求,并且会话从服务器端失效,我想在后续调用之间保持会话。

我尝试在 cookie 中设置 jsessionid 并在第二个请求中发送它,但是当我在服务器端调试时,它没有加载创建的会话,而是创建不同的会话,因此它不显示我第一次发送请求时在会话中设置的属性。

当我尝试使用直接 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());

【问题讨论】:

  • 有人能回答这个问题吗?即使我也面临同样的问题。

标签: rest-assured rest-assured-jsonpath


【解决方案1】:

您需要在 Rest Assured 中使用 Session 过滤器

https://github.com/rest-assured/rest-assured/wiki/Usage#session-support

【讨论】:

    猜你喜欢
    • 2015-01-16
    • 2013-07-17
    • 1970-01-01
    • 2011-04-10
    • 2016-12-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-11-05
    相关资源
    最近更新 更多