如何从 Response Headers 中提取值并使用 Rest Assured 进行断言?
How to extract values from Response Headers and make assertations using Rest Assured?
我想从响应 Header 中提取值并将它们存储为字符串,并最终使用某些值进行断言。
从下面的响应Header我想提取* Set-Cookie:id=xxxxxx-xxxxxxx-xxxxxx;
并存储它。
我正在使用 Rest Assured。谢谢!
Response Headers
* Cache-Control:no-cache, no-store, must-revalidate
* Connection:keep-alive
* Content-Length:108
* Content-Type:image/png
* Date:Wed, 22 Mar 2017 13:19:51 GMT
* Expires:0
* Pragma:no-cache
* Server:nginx/1.4.6 (Ubuntu)
* Set-Cookie: AWSELB=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX;PATH=/;DOMAIN=.xxxx.xxxxx.com;MAX-AGE=3600;VERSION=1
* Set-Cookie:id=xxxxxx-xxxxxxx-xxxxxx; Version=1; Path=/; Domain=.xxxx.xxxxx.com; Max-Age=157680000
* Set-Cookie:Session=xxxx-xxxxxx-xxxxxx-xxxxx; Version=1; Path=/; Domain=.xxxxx.xxxxxx.com; Max-Age=3600
* X-Powered-By:Xxxxxxxx/1
* X-Robots-Tag:noindex, nofollow
稍微改编自文档:https://github.com/rest-assured/rest-assured/wiki/Usage#headers-cookies-status-etc
Cookies
要获取 cookie 的所有值,您需要先从 Response 对象获取 Cookies 对象。从 Cookies 实例中,您可以使用 Cookies.getValues() 方法获取所有值,该方法 returns 包含所有 cookie 值的列表。
字符串形式的简单值:
import io.restassured.http.Cookie;
import io.restassured.http.Cookies;
import io.restassured.response.Response;
Map<String, String> allCookies = get("https://www.whosebug.com").getCookies();
List<String> myCookieValues = allCookies.getValues("myCookieName");
要从 cookie 中获取所有字段,您需要详细的 cookie :
Cookies allDetailedCookies = get("https://www.whosebug.com").getDetailedCookies();
Cookie myCookie = allDetailedCookies.get("myCookieName");
myCookie.getValue();
myCookie.getDomain();
myCookie.getExpiryDate();
myCookie.getMaxAge();
...
如果是多值 cookie:
List<Cookie> myCookies = allDetailedCookies.getList("myCookieNAme");
您可以使用 hamcrest 匹配器对 cookie 进行断言:
import static io.restassured.RestAssured.when;
import static org.hamcrest.Matchers.hasValue;
when()
.get("https://www.whosebug.com").
then()
.cookie("myCookieName", hasValue("value"));
文档推荐从 :
导入
io.restassured.RestAssured.*
io.restassured.matcher.RestAssuredMatchers.*
org.hamcrest.Matchers.*
我想从响应 Header 中提取值并将它们存储为字符串,并最终使用某些值进行断言。
从下面的响应Header我想提取* Set-Cookie:id=xxxxxx-xxxxxxx-xxxxxx;
并存储它。
我正在使用 Rest Assured。谢谢!
Response Headers
* Cache-Control:no-cache, no-store, must-revalidate
* Connection:keep-alive
* Content-Length:108
* Content-Type:image/png
* Date:Wed, 22 Mar 2017 13:19:51 GMT
* Expires:0
* Pragma:no-cache
* Server:nginx/1.4.6 (Ubuntu)
* Set-Cookie: AWSELB=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX;PATH=/;DOMAIN=.xxxx.xxxxx.com;MAX-AGE=3600;VERSION=1
* Set-Cookie:id=xxxxxx-xxxxxxx-xxxxxx; Version=1; Path=/; Domain=.xxxx.xxxxx.com; Max-Age=157680000
* Set-Cookie:Session=xxxx-xxxxxx-xxxxxx-xxxxx; Version=1; Path=/; Domain=.xxxxx.xxxxxx.com; Max-Age=3600
* X-Powered-By:Xxxxxxxx/1
* X-Robots-Tag:noindex, nofollow
稍微改编自文档:https://github.com/rest-assured/rest-assured/wiki/Usage#headers-cookies-status-etc
Cookies
要获取 cookie 的所有值,您需要先从 Response 对象获取 Cookies 对象。从 Cookies 实例中,您可以使用 Cookies.getValues() 方法获取所有值,该方法 returns 包含所有 cookie 值的列表。
字符串形式的简单值:
import io.restassured.http.Cookie;
import io.restassured.http.Cookies;
import io.restassured.response.Response;
Map<String, String> allCookies = get("https://www.whosebug.com").getCookies();
List<String> myCookieValues = allCookies.getValues("myCookieName");
要从 cookie 中获取所有字段,您需要详细的 cookie :
Cookies allDetailedCookies = get("https://www.whosebug.com").getDetailedCookies();
Cookie myCookie = allDetailedCookies.get("myCookieName");
myCookie.getValue();
myCookie.getDomain();
myCookie.getExpiryDate();
myCookie.getMaxAge();
...
如果是多值 cookie:
List<Cookie> myCookies = allDetailedCookies.getList("myCookieNAme");
您可以使用 hamcrest 匹配器对 cookie 进行断言:
import static io.restassured.RestAssured.when;
import static org.hamcrest.Matchers.hasValue;
when()
.get("https://www.whosebug.com").
then()
.cookie("myCookieName", hasValue("value"));
文档推荐从 :
导入io.restassured.RestAssured.*
io.restassured.matcher.RestAssuredMatchers.*
org.hamcrest.Matchers.*