We use CAS (Central Authentication Service) for authentication in our application.
We have a lot of REST APIs and we need to test the rest calls. We are planning to set up a test suite which tests all the REST api's. So, we thought of using Rest-assured for your suite.
The problem is the authentication. Since we have CAS in place, for each call it redirects to the login page asking for username and password. Is there a straight to pass the username and password in one shot and get authenticated and get the cookies?
Any help will be appreciated!!
Try Following:
Response response = given().contentType(ContentType.JSON)
.relaxedHTTPSValidation().when().get("<URL>");
String html = response.asString();
String sessionid = response.getCookie("JSESSIONID");
org.jsoup.nodes.Document loginDoc = Jsoup.parse(html);
String ltTagValue = loginDoc.select("input[name=lt]").val();
String executionTagValue = loginDoc.select("input[name=execution]").val();
HashMap<String, Object> head = new HashMap();
head.put("User-Agent","Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/59.0.3071.115 Safari/537.36");
head.put("Accept","text/html");
head.put("Accept-Language" ,"en-US,en;q=0.9");
Response response2= given().headers(head).
contentType(ContentType.URLENC).cookie("CASTGC","").cookie("JSESSIONID", sessionid).
cookie("CASPRIVACY","")
.cookie("Path","/em-cas").
relaxedHTTPSValidation().
formParam("username", "<abc>").
formParam("password", "<xyz>").
formParam("lt", ltTagValue).
formParam("execution", executionTagValue).
formParam("_eventId", "submit").
formParam("submit", "LOGIN").
when().
post(<URL>)
;