*
* @since 1.6
*/
public void test_Parse() {
List<HttpCookie> list = HttpCookie.parse("test=\"null\"");
HttpCookie cookie = list.get(0);
// when two '"' presents, the parser ignores it.
assertEquals("null", cookie.getValue());
assertNull(cookie.getComment());
assertNull(cookie.getCommentURL());
assertFalse(cookie.getDiscard());
assertNull(cookie.getDomain());
assertEquals(-1, cookie.getMaxAge());
assertNull(cookie.getPath());
assertNull(cookie.getPortlist());
assertFalse(cookie.getSecure());
// default version is 0
assertEquals(0, cookie.getVersion());
list = HttpCookie.parse("Set-cookie2:name=\"tes,t\"");
cookie = list.get(0);
// when two '"' presents, the parser ignores it.
assertEquals("tes,t", cookie.getValue());
// If cookie header = Set-Cookie2, version = 1
list = HttpCookie
.parse("Set-cookie2:test=null\";;Port=abde,82;Path=/temp;;;Discard;commentURl=http://harmonytest.org;Max-age=-10;");
cookie = list.get(0);
assertEquals("null\"", cookie.getValue());
assertEquals(1, cookie.getVersion());
assertEquals("/temp", cookie.getPath());
assertTrue(cookie.getDiscard());
assertEquals("http://harmonytest.org", cookie.getCommentURL());
assertEquals(-10l, cookie.getMaxAge());
assertTrue(cookie.hasExpired());
assertEquals("abde,82", cookie.getPortlist());
// Version 0 cookie
list = HttpCookie
.parse("Set-Cookie:name=tes,t;Comment=version1-cookie;Discard=false;commentURL=vers\nion1-cookie-url;Domain=x.y;");
cookie = list.get(0);
assertEquals(0, cookie.getVersion());
assertEquals("tes,t", cookie.getValue());
assertEquals("name", cookie.getName());
assertEquals("version1-cookie", cookie.getComment());
assertEquals("vers\nion1-cookie-url", cookie.getCommentURL());
assertEquals("x.y", cookie.getDomain());
assertTrue(cookie.getDiscard());
// Check value
checkValidValue("Set-Cookie:", "val,ue");
checkValidValue("Set-Cookie:", "val\nue");
checkValidValue("Set-Cookie:", "value=value");
checkValidValue("Set-Cookie2:", "val\nue");
checkValidValue("Set-Cookie2:", "val\u2029ue");
checkValidValue("Set-Cookie2:", "value=value");
// Check comment
// In RFC 2965 '=' is mandatory, but this is not the case in RI.
list = HttpCookie.parse("Set-Cookie:name=tes,t;Comment;");
cookie = list.get(0);
assertNull(cookie.getComment());
list = HttpCookie
.parse("Set-Cookie:name=tes,t;Comment=sentence;Comment=anotherSentence");
cookie = list.get(0);
assertEquals("sentence", cookie.getComment());
// Check CommentURL
list = HttpCookie
.parse("Set-Cookie:name=tes,t;Commenturl;commentuRL=(la,la)");
cookie = list.get(0);
assertEquals("(la,la)", cookie.getCommentURL());
// Check Domain
list = HttpCookie.parse("Set-Cookie:name=test;Domain=a_domain");
cookie = list.get(0);
assertEquals("a_domain", cookie.getDomain());
// Check Path
list = HttpCookie.parse("Set-Cookie:name=test;PaTh=pa$th");
cookie = list.get(0);
assertEquals("pa$th", cookie.getPath());
// Check Max-Age
list = HttpCookie.parse("Set-Cookie:name=test;Max-Age=1000");
cookie = list.get(0);
assertEquals(1000, cookie.getMaxAge());
list = HttpCookie.parse("Set-Cookie:name=test;Max-Age=-1000");
cookie = list.get(0);
assertEquals(-1000, cookie.getMaxAge());
// Check portlist
list = HttpCookie.parse("Set-Cookie:name=tes,t;port");
cookie = list.get(0);
assertEquals(null, cookie.getPortlist());
list = HttpCookie.parse("Set-Cookie:name=tes,t;port=");
cookie = list.get(0);
assertEquals("", cookie.getPortlist());
list = HttpCookie.parse("Set-Cookie:name=tes,t;port=123 345");
cookie = list.get(0);
assertEquals("123 345", cookie.getPortlist());
list = HttpCookie.parse("Set-Cookie:name=tes,t;port=123,345");
cookie = list.get(0);
assertEquals("123,345", cookie.getPortlist());
// Check Secure
list = HttpCookie.parse("Set-Cookie:name=test;secure");
cookie = list.get(0);
assertTrue(cookie.getSecure());
list = HttpCookie.parse("Set-Cookie:name=test;secure=fa");
cookie = list.get(0);
assertTrue(cookie.getSecure());
assertFalse(cookie.hasExpired());
list = HttpCookie.parse("Set-Cookie2:name=test;secure=false");
cookie = list.get(0);
assertTrue(cookie.getSecure());
// Check expire
list = HttpCookie.parse("Set-Cookie:name=test;expires=2006-10-23");
cookie = list.get(0);
assertEquals(0, cookie.getMaxAge());
assertTrue(cookie.hasExpired());
// Also recognize invalid date
list = HttpCookie
.parse("Set-Cookie:name=test;expires=Sun, 29-Feb-1999 19:14:07 GMT");
cookie = list.get(0);
assertTrue(cookie.getMaxAge() < 0);
assertTrue(cookie.hasExpired());
// Parse multiple cookies
list = HttpCookie
.parse("Set-Cookie2:name=test;,Set-Cookie2:name2=test2;comment=c234;");
cookie = list.get(0);
assertEquals("name", cookie.getName());
assertEquals(1, cookie.getVersion());
assertEquals("test", cookie.getValue());
cookie = list.get(1);
assertEquals(1, cookie.getVersion());
// From the second cookie, the "set-cookie2" header does not take effect
assertEquals("Set-Cookie2:name2", cookie.getName());
assertEquals("c234", cookie.getComment());
list = HttpCookie.parse("Set-Cookie2:name=test,name2=test2");
assertEquals(1, list.get(0).getVersion());
assertEquals(1, list.get(1).getVersion());
// Must begin with "set-cookie2" header
list = HttpCookie.parse("name=test,Set-Cookie2:name2=test2");
cookie = list.get(0);
assertEquals(1, list.size());
HttpCookie c = HttpCookie.parse(
"Set-cookie:NAME2=VALUE2;path=/t;domain=.b.c;version=1").get(0);
assertEquals(1, c.getVersion());
c = HttpCookie.parse(
"Set-cookie2:NAME2=VALUE2;path=/t;domain=.b.c;version=0")
.get(0);
assertEquals(1, c.getVersion());
list = HttpCookie.parse("Set-cookie:null=;Domain=null;Port=null");
cookie = list.get(0);
assertNotNull(cookie.getValue());