I made the suggested code change to check if the "expires" parameter is null before calling the getTime method on it. If it is null, we pass 0 to setCookieImpl, effectively creating a session cookie instead. Patch by: jlabanca Review by: jgw Issue: 1152, 658 git-svn-id: https://google-web-toolkit.googlecode.com/svn/trunk@1199 8db76d5a-ed1c-0410-87a9-c151d255dfc7
diff --git a/user/src/com/google/gwt/user/client/Cookies.java b/user/src/com/google/gwt/user/client/Cookies.java index 7be0b5c..9c10e61 100644 --- a/user/src/com/google/gwt/user/client/Cookies.java +++ b/user/src/com/google/gwt/user/client/Cookies.java
@@ -76,7 +76,7 @@ * @param value the cookie's value */ public static void setCookie(String name, String value) { - setCookieImpl(name, value, "", null, null, false); + setCookieImpl(name, value, 0, null, null, false); } /** @@ -102,7 +102,7 @@ */ public static void setCookie(String name, String value, Date expires, String domain, String path, boolean secure) { - setCookieImpl(name, value, expires.toGMTString(), domain, path, secure); + setCookieImpl(name, value, (expires == null) ? 0 : expires.getTime(), domain, path, secure); } static native void loadCookies(HashMap m) /*-{ @@ -147,9 +147,10 @@ }-*/; private static native void setCookieImpl(String name, String value, - String expires, String domain, String path, boolean secure) /*-{ + long expires, String domain, String path, boolean secure) /*-{ var c = encodeURIComponent(name) + '=' + encodeURIComponent(value); - c += ';expires=' + expires; + if ( expires ) + c += ';expires=' + (new Date(expires)).toGMTString(); if (domain) c += ';domain=' + domain; if (path)
diff --git a/user/test/com/google/gwt/user/client/CookieTest.java b/user/test/com/google/gwt/user/client/CookieTest.java index d48169e..c6a5a3f 100644 --- a/user/test/com/google/gwt/user/client/CookieTest.java +++ b/user/test/com/google/gwt/user/client/CookieTest.java
@@ -20,7 +20,7 @@ import java.util.Date; /** - * TODO: document me. + * Test Case for {@link Cookies}. */ public class CookieTest extends GWTTestCase { @@ -52,4 +52,38 @@ assertEquals(Cookies.getCookie("novalue"), ""); assertEquals(Cookies.getCookie("notpresent"), null); } + + /* + * Test that the cookie will expire correctly after a set amount of time, + * but does not expire before that time. + */ + public void testExpires() { + // Test that the cookie expires in 5 seconds + Date expiresEarly = new Date(new Date().getTime() + (5 * 1000)); + Date expiresLate = new Date(new Date().getTime() + (60 * 1000)); + Cookies.setCookie("shouldExpireEarly", "early", expiresEarly); + Cookies.setCookie("shouldExpireLate", "late", expiresLate); + Cookies.setCookie("shouldNotExpire", "forever", null); + + // Wait until the cookie expires before checking it + Timer timer = new Timer() { + public void run() { + // Verify that the early expiring cookie does NOT exist + assertNull(Cookies.getCookie("shouldExpireEarly")); + + // Verify that the late expiring cookie does exist + assertEquals(Cookies.getCookie("shouldExpireLate"), "late"); + + // Verify the session cookie doesn't expire + assertEquals(Cookies.getCookie("shouldNotExpire"), "forever"); + Cookies.removeCookie("shouldNotExpire"); + assertNull(Cookies.getCookie("shouldNotExpire")); + + // Finish the test + finishTest(); + } + }; + timer.schedule(5010); + delayTestFinish(6 * 1000); + } }