Correctly format cookies. This fix some bug which lead to expiring of cookies to not work. See #426

This commit is contained in:
norman 2012-07-04 07:40:39 +02:00
parent f499348267
commit 18fb438949
2 changed files with 19 additions and 9 deletions

View File

@ -136,10 +136,12 @@ public class CookieEncoder {
if (cookie.isSecure()) {
sb.append(CookieHeaderNames.SECURE);
sb.append((char) HttpConstants.SEMICOLON);
sb.append((char) HttpConstants.SP);
}
if (cookie.isHttpOnly()) {
sb.append(CookieHeaderNames.HTTPONLY);
sb.append((char) HttpConstants.SEMICOLON);
sb.append((char) HttpConstants.SP);
}
if (cookie.getVersion() >= 1) {
if (cookie.getComment() != null) {
@ -162,16 +164,19 @@ public class CookieEncoder {
}
sb.setCharAt(sb.length() - 1, (char) HttpConstants.DOUBLE_QUOTE);
sb.append((char) HttpConstants.SEMICOLON);
sb.append((char) HttpConstants.SP);
}
if (cookie.isDiscard()) {
sb.append(CookieHeaderNames.DISCARD);
sb.append((char) HttpConstants.SEMICOLON);
sb.append((char) HttpConstants.SP);
}
}
}
if (sb.length() > 0) {
sb.setLength(sb.length() - 1);
sb.setLength(sb.length() - 2);
}
return sb.toString();
@ -207,12 +212,13 @@ public class CookieEncoder {
}
sb.setCharAt(sb.length() - 1, (char) HttpConstants.DOUBLE_QUOTE);
sb.append((char) HttpConstants.SEMICOLON);
sb.append((char) HttpConstants.SP);
}
}
}
if (sb.length() > 0) {
sb.setLength(sb.length() - 1);
sb.setLength(sb.length() - 2);
}
return sb.toString();
}
@ -243,6 +249,7 @@ public class CookieEncoder {
sb.append((char) HttpConstants.EQUALS);
sb.append(val);
sb.append((char) HttpConstants.SEMICOLON);
sb.append((char) HttpConstants.SP);
}
private static void addQuoted(StringBuilder sb, String name, String val) {
@ -256,6 +263,8 @@ public class CookieEncoder {
sb.append(val.replace("\\", "\\\\").replace("\"", "\\\""));
sb.append((char) HttpConstants.DOUBLE_QUOTE);
sb.append((char) HttpConstants.SEMICOLON);
sb.append((char) HttpConstants.SP);
}
private static void add(StringBuilder sb, String name, int val) {
@ -263,5 +272,6 @@ public class CookieEncoder {
sb.append((char) HttpConstants.EQUALS);
sb.append(val);
sb.append((char) HttpConstants.SEMICOLON);
sb.append((char) HttpConstants.SP);
}
}

View File

@ -25,7 +25,7 @@ import org.junit.Test;
public class CookieEncoderTest {
@Test
public void testEncodingSingleCookieV0() {
String result = "myCookie=myValue;Expires=XXX;Path=/apathsomewhere;Domain=.adomainsomewhere;Secure";
String result = "myCookie=myValue; Expires=XXX; Path=/apathsomewhere; Domain=.adomainsomewhere; Secure";
DateFormat df = new CookieDateFormat();
Cookie cookie = new DefaultCookie("myCookie", "myValue");
CookieEncoder encoder = new CookieEncoder(true);
@ -59,7 +59,7 @@ public class CookieEncoderTest {
@Test
public void testEncodingSingleCookieV1() {
String result = "myCookie=myValue;Max-Age=50;Path=\"/apathsomewhere\";Domain=.adomainsomewhere;Secure;Comment=\"this is a Comment\";Version=1";
String result = "myCookie=myValue; Max-Age=50; Path=\"/apathsomewhere\"; Domain=.adomainsomewhere; Secure; Comment=\"this is a Comment\"; Version=1";
Cookie cookie = new DefaultCookie("myCookie", "myValue");
CookieEncoder encoder = new CookieEncoder(true);
encoder.addCookie(cookie);
@ -75,7 +75,7 @@ public class CookieEncoderTest {
@Test
public void testEncodingSingleCookieV2() {
String result = "myCookie=myValue;Max-Age=50;Path=\"/apathsomewhere\";Domain=.adomainsomewhere;Secure;Comment=\"this is a Comment\";Version=1;CommentURL=\"http://aurl.com\";Port=\"80,8080\";Discard";
String result = "myCookie=myValue; Max-Age=50; Path=\"/apathsomewhere\"; Domain=.adomainsomewhere; Secure; Comment=\"this is a Comment\"; Version=1; CommentURL=\"http://aurl.com\"; Port=\"80,8080\"; Discard";
Cookie cookie = new DefaultCookie("myCookie", "myValue");
CookieEncoder encoder = new CookieEncoder(true);
encoder.addCookie(cookie);
@ -107,9 +107,9 @@ public class CookieEncoderTest {
@Test
public void testEncodingMultipleClientCookies() {
String c1 = "$Version=1;myCookie=myValue;$Path=\"/apathsomewhere\";$Domain=.adomainsomewhere;$Port=\"80,8080\";";
String c2 = "$Version=1;myCookie2=myValue2;$Path=\"/anotherpathsomewhere\";$Domain=.anotherdomainsomewhere;";
String c3 = "$Version=1;myCookie3=myValue3";
String c1 = "$Version=1; myCookie=myValue; $Path=\"/apathsomewhere\"; $Domain=.adomainsomewhere; $Port=\"80,8080\"; ";
String c2 = "$Version=1; myCookie2=myValue2; $Path=\"/anotherpathsomewhere\"; $Domain=.anotherdomainsomewhere; ";
String c3 = "$Version=1; myCookie3=myValue3";
CookieEncoder encoder = new CookieEncoder(false);
Cookie cookie = new DefaultCookie("myCookie", "myValue");
cookie.setVersion(1);
@ -135,7 +135,7 @@ public class CookieEncoderTest {
cookie3.setVersion(1);
encoder.addCookie(cookie3);
String encodedCookie = encoder.encode();
assertEquals(c1 + c2 + c3, encodedCookie);
assertEquals(c1 +c2 + c3, encodedCookie);
}
@Test