使用放心的CAS认证
CAS authentication using rest assured
我们在应用程序中使用 CAS(中央身份验证服务)进行身份验证。
我们有很多 REST API,我们需要测试其余调用。我们正计划建立一个测试套件来测试所有 REST api。因此,我们考虑为您的套件使用 Rest-assured。
身份验证问题。由于我们有 CAS,每次调用都会重定向到登录页面,要求输入用户名和密码。是否有直接一次性传递用户名和密码并通过身份验证并获取 cookie 的方法?
任何帮助将不胜感激!!
您可以使用 auth 方法通过 Rest-Assured 进行身份验证。
例如:
RestAssured.given ().auth ().basic (username, password);
编辑:
你也可以试试这个:
RestAssured.given ().auth ().form (username, password);
试试这个,如果有效请告诉我们。
尝试以下操作:
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>)
;
我们在应用程序中使用 CAS(中央身份验证服务)进行身份验证。
我们有很多 REST API,我们需要测试其余调用。我们正计划建立一个测试套件来测试所有 REST api。因此,我们考虑为您的套件使用 Rest-assured。
身份验证问题。由于我们有 CAS,每次调用都会重定向到登录页面,要求输入用户名和密码。是否有直接一次性传递用户名和密码并通过身份验证并获取 cookie 的方法?
任何帮助将不胜感激!!
您可以使用 auth 方法通过 Rest-Assured 进行身份验证。
例如:
RestAssured.given ().auth ().basic (username, password);
编辑:
你也可以试试这个:
RestAssured.given ().auth ().form (username, password);
试试这个,如果有效请告诉我们。
尝试以下操作:
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>)
;