Fix cookie encoding when maxAge is greater than 24 days

This commit is contained in:
James Roper 2015-05-12 18:47:28 +10:00 committed by Norman Maurer
parent 3a9c903c57
commit 6e2d2ce998
2 changed files with 21 additions and 1 deletions

View File

@ -95,7 +95,7 @@ public final class ServerCookieEncoder extends CookieEncoder {
if (cookie.maxAge() != Integer.MIN_VALUE) {
add(buf, CookieHeaderNames.MAX_AGE, cookie.maxAge());
Date expires = new Date(cookie.maxAge() * 1000 + System.currentTimeMillis());
Date expires = new Date((long) cookie.maxAge() * 1000 + System.currentTimeMillis());
add(buf, CookieHeaderNames.EXPIRES, HttpHeaderDateFormat.get().format(expires));
}

View File

@ -60,4 +60,24 @@ public class ServerCookieEncoderTest {
assertNotNull(encodedCookie2);
assertTrue(encodedCookie2.isEmpty());
}
@Test
public void testEncodingLargeMaxValue() throws ParseException {
int maxAge = Integer.MAX_VALUE / 1000 + 84600;
String result =
"myCookie=myValue; Max-Age=" + maxAge + "; Expires=(.+)";
Cookie cookie = new DefaultCookie("myCookie", "myValue");
cookie.setMaxAge(maxAge);
String encodedCookie = ServerCookieEncoder.STRICT.encode(cookie);
Matcher matcher = Pattern.compile(result).matcher(encodedCookie);
System.out.println(encodedCookie);
assertTrue(matcher.find());
Date expiresDate = HttpHeaderDateFormat.get().parse(matcher.group(1));
long diff = (expiresDate.getTime() - System.currentTimeMillis()) / 1000;
// 2 secs should be fine
assertTrue(Math.abs(diff - maxAge) <= 2);
}
}