繁体   English   中英

如何从“响应标头”中提取值并使用“确保放心”进行断言?

[英]How to extract values from Response Headers and make assertations using Rest Assured?

我想从响应标题中提取值并将其存储为字符串,并最终使用某些值进行断言。 我要从以下响应标题中提取* Set-Cookie:id = xxxxxx-xxxxxxx-xxxxxx; 并存储它。 我正在使用“放心”。 谢谢!

响应标头*缓存控制:无需缓存,无需存储,必须重新验证*连接:保持活动状态*内容长度:108 *内容类型:image / png *日期:2017年3月22日星期三13:19: 51 GMT *到期时间:0 *语法:no-cache *服务器:nginx / 1.4.6(Ubuntu)* Set-Cookie:AWSELB = XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX; XXX.XXX = XXX; XXX。 ; 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; 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

饼干

要获取Cookie的所有值,您需要首先从Response对象中获取Cookies对象。 从Cookies实例中,您可以使用Cookies.getValues()方法获取所有值,该方法返回具有所有cookie值的列表。

简单值如String:

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");

要从cookie获取所有字段,您需要详细的cookie:

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 matchers对cookie进行断言:

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

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

Doc建议从进口:

io.restassured.RestAssured.*
io.restassured.matcher.RestAssuredMatchers.*
org.hamcrest.Matchers.*

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM