4

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.

Sanjay
  • 8,755
  • 7
  • 46
  • 62

3 Answers3

3

I'm not finding anything OOTB, but this works:

assertThat(response.getCookie("foo"), is(nullValue()));
Hazel T
  • 859
  • 1
  • 8
  • 22
0

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?

k10
  • 62
  • 1
  • 7
0

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));
  }
alext
  • 186
  • 1
  • 3