2017-03-22 105 views
1

我想從響應頭中提取值並將它們存儲爲字符串,並最終使用某些值進行斷言。從我想提取的以下Reponse標題* Set-Cookie:id = xxxxxx-xxxxxxx-xxxxxx;並存儲它。我正在使用Rest Assured。謝謝!如何從Response Headers中提取值並使用Rest Assured進行斷言?

響應頭 *緩存控制:無緩存,無店鋪,必重新驗證 *連接:保持活躍 *的Content-Length:108 *內容類型:圖像/ PNG *日期:星期三,2017年3月22日十三時十九分51秒GMT *過期:0 *雜注:無緩存 *服務器:nginx/1.4.6(Ubuntu的) *設置Cookie:AWSELB = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX; PATH = /; DOMAIN = .xxxx.xxxxx.com; MAX-AGE = 3600; VERSION = 1 * Set-Cookie:id = xxxxxx-xxxxxxx-xxxxxx;版本= 1;路徑= /;域= .xxxx.xxxxx.com; Max-Age = 157680000 * Set-Cookie:Session = xxxx-xxxxxx-xxxxxx-xxxxx;版本= 1;路徑= /;域= .xxxxx.xxxxxx.com;最大年齡= 3600 * X供電-通過:XXXXXXXX/1 * X-機器人-標籤:NOINDEX,nofollow的

回答

0

略從DOC適於:https://github.com/rest-assured/rest-assured/wiki/Usage#headers-cookies-status-etc

餅乾

要獲取所需的cookie的所有值,以便首先從Response對象獲取Cookies對象。從Cookies實例中,您可以使用Cookies.getValues()方法獲取所有值,該方法返回帶有所有Cookie值的List。

簡單的值作爲字符串:

import io.restassured.http.Cookie; 
import io.restassured.http.Cookies; 
import io.restassured.response.Response; 

Map<String, String> allCookies = get("https://www.stackoverflow.com").getCookies(); 

List<String> myCookieValues = allCookies.getValues("myCookieName"); 

要想從餅乾各個領域,需要詳細的餅乾:

Cookies allDetailedCookies = get("https://www.stackoverflow.com").getDetailedCookies(); 

Cookie myCookie = allDetailedCookies.get("myCookieName"); 
myCookie.getValue(); 
myCookie.getDomain(); 
myCookie.getExpiryDate(); 
myCookie.getMaxAge(); 
... 

如果多值的cookie:

List<Cookie> myCookies = allDetailedCookies.getList("myCookieNAme"); 

可以斷言在與hamcrest匹配器的餅乾上:

import static io.restassured.RestAssured.when; 
import static org.hamcrest.Matchers.hasValue; 

when() 
    .get("https://www.stackoverflow.com"). 
then() 
    .cookie("myCookieName", hasValue("value")); 

文件建議從進口:

io.restassured.RestAssured.* 
io.restassured.matcher.RestAssuredMatchers.* 
org.hamcrest.Matchers.* 
相關問題