/**
* Tests RFC 2965 compliant cookie formatting.
*/
public void testRFC2965CookieFormatting() throws Exception {
CookieSpec cookiespec = new RFC2965Spec(null, true);
BasicClientCookie2 cookie1 = new BasicClientCookie2("name1", "value");
cookie1.setDomain(".domain.com");
cookie1.setPath("/");
cookie1.setPorts(new int[] {80,8080});
cookie1.setVersion(1);
// domain, path, port specified
cookie1.setAttribute(ClientCookie.DOMAIN_ATTR, ".domain.com");
cookie1.setAttribute(ClientCookie.PATH_ATTR, "/");
cookie1.setAttribute(ClientCookie.PORT_ATTR, "80,8080");
List<Cookie> cookies = new ArrayList<Cookie>();
cookies.add(cookie1);
List<Header> headers = cookiespec.formatCookies(cookies);
assertNotNull(headers);
assertEquals(1, headers.size());
assertEquals("$Version=1; name1=\"value\"; $Path=\"/\"; $Domain=\".domain.com\"; $Port=\"80,8080\"",
headers.get(0).getValue());
BasicClientCookie2 cookie2 = new BasicClientCookie2("name2", "value");
cookie2.setDomain(".domain.com");
cookie2.setPath("/a/");
cookie2.setPorts(new int[] {80,8080});
cookie2.setVersion(2);
// domain, path specified but port unspecified
cookie2.setAttribute(ClientCookie.DOMAIN_ATTR, ".domain.com");
cookie2.setAttribute(ClientCookie.PATH_ATTR, "/a/");
cookies = new ArrayList<Cookie>();
cookies.add(cookie2);
headers = cookiespec.formatCookies(cookies);
assertNotNull(headers);
assertEquals(1, headers.size());
assertEquals("$Version=2; name2=\"value\"; $Path=\"/a/\"; $Domain=\".domain.com\"",
headers.get(0).getValue());
BasicClientCookie2 cookie3 = new BasicClientCookie2("name3", "value");
cookie3.setDomain(".domain.com");
cookie3.setPath("/a/b/");
cookie3.setPorts(new int[] {80,8080});
cookie3.setVersion(1);
// path specified, port specified but blank, domain unspecified
cookie3.setAttribute(ClientCookie.PATH_ATTR, "/a/b/");
cookie3.setAttribute(ClientCookie.PORT_ATTR, " ");
cookies = new ArrayList<Cookie>();
cookies.add(cookie3);
headers = cookiespec.formatCookies(cookies);
assertNotNull(headers);
assertEquals(1, headers.size());
assertEquals("$Version=1; name3=\"value\"; $Path=\"/a/b/\"; $Port=\"\"",
headers.get(0).getValue());
cookies = new ArrayList<Cookie>();
cookies.add(cookie3);
cookies.add(cookie2);
cookies.add(cookie1);
headers = cookiespec.formatCookies(cookies);
assertNotNull(headers);
assertEquals(1, headers.size());
assertEquals("$Version=1; " +
"name3=\"value\"; $Path=\"/a/b/\"; $Port=\"\"; " +
"name2=\"value\"; $Path=\"/a/\"; $Domain=\".domain.com\"; " +