[英]How to extract cookies from a REST Assured response using common code
Using REST Assured (4.3.3, from Java) I can freely manipulate request and response specification objects in a base test class, so that when the actual test needs a request specification to execute the actual call, it's "pre-configured" with certain common statements.使用 REST Assured(4.3.3,来自 Java)我可以自由地操作基础测试类中的请求和响应规范对象,这样当实际测试需要请求规范来执行实际调用时,它是“预配置”的常见的说法。 For example:
例如:
public abstract class TestBase
protected RequestSpecification request() {
return given()
.cookies(globalCookies)
.port(serverPort)
.log().ifValidationFails()
.then()
.log().ifValidationFails()
.given();
}
}
public class ActualTest extends TestBase {
@Test
public void test1() {
String content = request().get("/some").then()
.statusCode(200)
.extract()
.body().asString();
}
}
What I'd like to do now is to "pre-configure" the specifications to preserve cookies between the requests.我现在想做的是“预配置”规范以在请求之间保留 cookie。 I am feeding the list of cookies to send using
RequestSpecification.cookies(Cookies)
, but I can't find any way to instruct the specifications to extract the returned cookies before obtaining the instance of Response
.我正在使用
RequestSpecification.cookies(Cookies)
要发送的 cookie 列表,但我找不到任何方法来指示规范在获取Response
实例之前提取返回的 cookie。 However, such instance is only available after calling one of the RequestSender
methods, which I can't do in the base code.但是,此类实例仅在调用
RequestSender
方法之一后才可用,而我无法在基本代码中执行此操作。
The methods I've considered to solve this are outlined below, but the first two are quite clunky, and the third is probably just outright wrong as it meddles with classes in "internal" sub-packages.下面概述了我考虑过的解决此问题的方法,但前两个非常笨拙,第三个可能完全错误,因为它与“内部”子包中的类发生冲突。 Is there a "right" way of doing this?
有没有“正确”的方法来做到这一点?
saveCookies()
that takes in an instance of ValidatableResponse
, on which it can call extract()
, and get the cookies.saveCookies()
,它接受ValidatableResponse
一个实例,它可以在该实例上调用extract()
并获取 cookie。 Then each test will have to use the method.public abstract class TestBase
// ...
protected ValidatableResponse saveCookies(ValidatableResponse r) {
saveGlobalCookies(r.extract().detailedCookies());
return r;
}
}
public class ActualTest extends TestBase {
@Test
public void test1() {
String content = saveCookies(request().get("/some").then())
.statusCode(200)
.extract()
.body().asString();
}
}
ValidatableResponse
can be intercepted, and the final result (if needed) returned to the invoker.ValidatableResponse
,并将最终结果(如果需要)返回给调用者。public abstract class TestBase
// ...
<T> T cookieRequest(Function<RequestSender, ValidatableResponse> exec, Function<ValidatableResponse, T> post) {
ValidatableResponse vr = exec.apply(request());
saveGlobalCookies(vr.extract().detailedCookies());
return post.apply(vr);
}
}
public class ActualTest extends TestBase {
@Test
public void test1() {
String content = cookieRequest(r->r.get("/some").then(),
r->r.extract().body().asString());
}
}
ResponseSpecificationImpl
(dragging in RequestSpecificationImpl
and TestSpecificationImpl
).ResponseSpecificationImpl
扩展实例(拖动RequestSpecificationImpl
和TestSpecificationImpl
)。You can use CookieFilter
to satify your need.您可以使用
CookieFilter
来满足您的需求。
The cookie filter can be used to keep track of all the cookies sent by the server and use them in subsequent requests
cookie 过滤器可用于跟踪服务器发送的所有 cookie 并在后续请求中使用它们
You can use this class in 2 ways:您可以通过两种方式使用此类:
RestAssured.filters(new CookieFilter());
CookieFilter cookieFilter = new CookieFilter();
//Request 1
given().filter(cookieFilter).get("/x");
//Request 2
given().filter(cookieFilter).get("/y");
For more information:想要查询更多的信息:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.