问题
What would be a good way in REST Assured to ensure that a cookie is absent? I checked that there are many .cookie
and .cookies
methods, but none support checking the absence of a cookie.
回答1:
I'm not finding anything OOTB, but this works:
assertThat(response.getCookie("foo"), is(nullValue()));
回答2:
Would fetching all cookies, iterating over it and asserting over that the expected cookie is not present solve your problem? Am i missing something here?
回答3:
You need to extract the response to access the cookies directly. Here's a (hopefully) real world example:
@Test
public void traceNotSupported() {
ExtractableResponse<Response> response =
given()
.cookie(SOME_COOKIE)
.header(SOME_HEADER, "some-value")
.when()
.request(Method.TRACE)
.then()
.contentType(not(equalTo("message/http")))
.statusCode(HttpStatus.METHOD_NOT_ALLOWED_405)
.extract();
assertFalse(response.headers().hasHeaderWithName(SOME_HEADER));
assertFalse(response.cookies().containsKey(SOME_COOKIE));
}
来源:https://stackoverflow.com/questions/43082765/how-to-ensure-that-a-cookie-is-not-present