Example #1
0
  @Test
  public void testCookies_whenCookiesArePresent() {

    Collection<Cookie> cookies = new ArrayList<>();
    cookies.add(new Cookie("cookie1", "cookie1value"));
    cookies.add(new Cookie("cookie2", "cookie2value"));

    Map<String, String> expected = new HashMap<>();
    for (Cookie cookie : cookies) {
      expected.put(cookie.getName(), cookie.getValue());
    }

    Cookie[] cookieArray = cookies.toArray(new Cookie[cookies.size()]);

    when(servletRequest.getCookies()).thenReturn(cookieArray);

    assertTrue(
        "The count of cookies returned should be the same as those in the request",
        request.cookies().size() == 2);

    assertEquals(
        "A Map of Cookies should have been returned because they exist",
        expected,
        request.cookies());
  }
Example #2
0
  @Test
  public void testCookie_whenCookiesArePresent() {

    final String cookieKey = "cookie1";
    final String cookieValue = "cookie1value";

    Collection<Cookie> cookies = new ArrayList<>();
    cookies.add(new Cookie(cookieKey, cookieValue));

    Cookie[] cookieArray = cookies.toArray(new Cookie[cookies.size()]);
    when(servletRequest.getCookies()).thenReturn(cookieArray);

    assertNotNull(
        "A value for the key provided should exist because a cookie with the same key is present",
        request.cookie(cookieKey));

    assertEquals(
        "The correct value for the cookie key supplied should be returned",
        cookieValue,
        request.cookie(cookieKey));
  }