private static String addVersion1ResponseCookieToExchange(final Cookie cookie) { final StringBuilder header = new StringBuilder(cookie.getName()); header.append("="); header.append(cookie.getValue()); header.append("; Version=1"); if (cookie.getPath() != null) { header.append("; Path="); header.append(cookie.getPath()); } if (cookie.getDomain() != null) { header.append("; Domain="); header.append(cookie.getDomain()); } if (cookie.isDiscard()) { header.append("; Discard"); } if (cookie.isSecure()) { header.append("; Secure"); } if (cookie.isHttpOnly()) { header.append("; HttpOnly"); } if (cookie.getMaxAge() != null) { if (cookie.getMaxAge() >= 0) { header.append("; Max-Age="); header.append(cookie.getMaxAge()); } } if (cookie.getExpires() != null) { header.append("; Expires="); header.append(DateUtils.toDateString(cookie.getExpires())); } return header.toString(); }
@Test public void testCookieBuilder() { // given LocalDateTime now = LocalDateTime.now(); // when Cookie cookie = CookieBuilder.create() .name("foo") .value("bar") .path("/foobar") .domain("www.foo.com") .maxAge(1223) .expires(now) .discard(true) .secure(true) .httpOnly(true) .build(); // then assertThat(cookie, not(nullValue())); assertThat("foo", equalTo(cookie.getName())); assertThat("bar", equalTo(cookie.getValue())); assertThat("/foobar", equalTo(cookie.getPath())); assertThat("www.foo.com", equalTo(cookie.getDomain())); assertThat( Date.from(now.atZone(ZoneId.systemDefault()).toInstant()), equalTo(cookie.getExpires())); assertThat(cookie.getMaxAge(), equalTo(1223)); assertThat(cookie.isDiscard(), equalTo(true)); assertThat(cookie.isSecure(), equalTo(true)); assertThat(cookie.isHttpOnly(), equalTo(true)); }