maintaining sessions with RestAssured
Asked Answered
T

1

6

How to set session attributes in restassured? In my application code we have something like this

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

How to set userId as session attribute here(in restassured test case)?

How to maintain the same session for all the requests(multiple subsequent requests)?

When i send multiple requests, it's considering every request as new and session is getting invalidated from server side, i want to maintain session between subsequent calls.

I tried setting jsessionid in the cookie and sent it in the second request, but when i debugged in the server side, it's not loading the session which was created, instead it's creating different session and because of this its doesn't show the attribute which i have set in the session when i first sent the request.

When i tried the same with direct HttpClient, it working, where as the same with RestAssured it's not working.

Code which was working with HttpClient is this

HttpClient httpClient = util.getHttpClient(); 

//1st request

HttpResponse response=httpClient.execute(postRequest); 

from response i have extracted the jessionid and set this in the second request

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);

//2nd request after setting the jessionid which i have extracted from the response

HttpResponse eventsResponse = httpClient.execute(getRequest); 

Above code is working perfectly fine and i am getting expected response. One observation is i am using the same httpClient Object for invoking both the requests.

Where as i if i try the same using RestAssured, it's not working.

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();

I tried reusing the same httpclient for all the requests using the following, but it didn't work

RestAssured.config = RestAssured.config().httpClient( new HttpClientConfig().reuseHttpClientInstance());
Titanism answered 22/6, 2018 at 5:32 Comment(1)
Can somebody answer this question? Even I am facing the same issue.Northeast
C
2

You need to use Session filter in Rest Assured

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

Concerned answered 24/6, 2019 at 20:10 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.