[英]How to set cookie with the request using rest assured?
我需要自動化其余的API。 API通過Spring安全保護。
以下是驗證的代碼:
Response response = given().auth()
.form(userName, password, FormAuthConfig.springSecurity().withLoggingEnabled(new LogConfig(captor, true)))
.post("/home/xyz.html");
Assert.assertTrue("Error occurs", response.statusCode() == 302);
if (response.statusCode() == 302) {
Cookie cookie = response.getDetailedCookie("JSESSIONID");
result.actualFieldValue = "User Authenticated: Session ID ->" + cookie.getValue();
System.out.println("Cookie set : "+cookie.getValue());
apiTestSessionID = cookie.getValue();
}
用戶登錄並返回302狀態,表示重定向。我找到了cookie並設置了一些全局變量。
現在,我使用請求設置cookie:
RequestSpecification reqSpecification = new RequestSpecBuilder().addCookie("JSESSIONID", AbstractBaseClass.apiTestSessionID).build();
Map<String, String> parameters = new HashMap<String, String>();
parameters.put("cstmrID", "000N0961");
parameters.put("pageNumber", "1");
parameters.put("pageSize", "10");
parameters.put("sortColumnName", "FIELD_NM");
parameters.put("sortDir", "asc");
parameters.put("filterColumnName1", "");
parameters.put("filterColumnName2", "USER_UPDT_EMAIL_ID");
parameters.put("filterValue2", "");
reqSpecification.queryParams(parameters);
Response response = given().spec(reqSpecification).when().get("/service/customerConfig").thenReturn();
System.out.println(response.asString());
但作為回應我得到登錄頁面HTML 。 我無法理解我做錯了什么。
假設:
GET /example.com/abc.html HTTP / 1.1主機:example.com連接:keep-alive Cache-Control:max-age = 0升級 - 不安全請求:1用戶代理:Mozilla / 5.0(Windows NT 6.1; WOW64)AppleWebKit / 537.36(KHTML,與Gecko一樣)Chrome / 55.0.2883.87 Safari / 537.36接受:text / html,application / xhtml + xml,application / xml; q = 0.9,image / webp, / ; q = 0.8接受 -編碼:gzip,deflate,sdch Accept-Language:en-US,en; q = 0.8 Cookie:JSESSIONID = C70A69F1C60D93DC3F8AC564BDE3F4DE.lon2mcaqaapp002; __utma = 185291189.2055499590.1460104969.1460104969.1460618428.2
我也是Rest Assured的新手,但我剛剛寫了類似的測試。
我建議你寫一個私有的authenticate()
方法:
private static String authenticate() {
given()
.auth()
.form(userName, password,FormAuthConfig.springSecurity().withLoggingEnabled(new LogConfig(captor, true)))
when()
.post("/home/xyz.html").
thenReturn()
.getDetailedCookie("JSESSIONID");
}
然后在請求中使用cookie:
given()
.cookie(authenticate())
when()
.get("/service/customerConfig").
thenReturn();
但我不知道你怎么在這里查看statusCode
。
使用.log().all()
來查看日志也是個好主意。
import io.restassured.RestAssured;
import io.restassured.http.ContentType;
import io.restassured.http.Cookies;
private Cookie cookie;
@BeforeClass
public void exampleOfLogin() {
String body = String.format("//json");
cookies = RestAssured.given()
.contentType(ContentType.JSON)
.when()
.body(body)
.post("www.test_test.com")
.then()
.statusCode(200)
.extract()
.response()
.getDetailedCookies();
}
@Test
public void performActionsBasedOnCookies() {
//set cookies before making a post request and check the returned status code
RestAssured.given()
.cookies(cookies)
.contentType(ContentType.JSON)
.when()
.post("www.test_url.com")
.then()
.statusCode(200);
}
我曾嘗試使用getDetailedCookies()來檢索身份驗證/授權cookie,並將其設置為given()。cookies(cookies).when()。post(url)。
但我無法檢索授權所需的所有cookie。
我就是這樣做的。
我創建了一個實例變量
import io.restassured.filter.cookie.CookieFilter
CookieFilter filter = new CookieFilter();
在身份驗證/授權調用中使用cookieFilter
。RestAssured.given()過濾器(過濾器)。體(主體).POST(URL).andReturn();
在需要身份驗證/授權cookie的請求中使用相同的過濾器。
。RestAssured.given()過濾器(過濾器)。體(主體).POST(URL);
過濾器中填充了身份驗證調用中的所有Cookie。
這是一個基本代碼來說明這個想法。 您可以將其擴展為步驟定義
import io.restassured.RestAssured;
import io.restassured.filter.cookie.CookieFilter;
import io.restassured.response.Response;
import org.junit.Test;
public class RestAssuredRunner {
CookieFilter filter = new CookieFilter();
@Test
public void testAuthenticatedRequest(String[] args) {
String url = "http://mywebsitelogin.com";
String body = "userId=1212&&password=232323";
//Authentication request
Response response = RestAssured.given().filter(filter).body(body).post(url).andReturn();
//Request that needs authentication
RestAssured.given().filter(filter).body(body).post(url);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.