public void testCrossDomainHandling() throws Exception {
   URL url = new URL("http://jakarta.apache.org/");
   assertEquals(0, man.getCookieCount()); // starts empty
   man.addCookieFromHeader("test=2;domain=.hc.apache.org", url);
   assertEquals(0, man.getCookieCount()); // should not be stored
   man.addCookieFromHeader("test=1;domain=.jakarta.apache.org", url);
   assertEquals(1, man.getCookieCount()); // OK
 }
 public void testCookieOrdering1() throws Exception {
   URL url = new URL("http://order.now/sub1/moo.html");
   man.addCookieFromHeader("test1=moo1;path=/", url);
   man.addCookieFromHeader("test2=moo2;path=/sub1", url);
   man.addCookieFromHeader("test2=moo3;path=/", url);
   assertEquals(3, man.getCookieCount());
   String s = man.getCookieHeaderForURL(url);
   assertNotNull(s);
   assertEquals("test2=moo2; test1=moo1; test2=moo3", s);
 }
 // Test duplicate cookie handling
 public void testDuplicateCookie() throws Exception {
   URL url = new URL("http://a.b.c/");
   man.addCookieFromHeader("test=1", url);
   String s = man.getCookieHeaderForURL(url);
   assertNotNull(s);
   assertEquals("test=1", s);
   man.addCookieFromHeader("test=2", url);
   s = man.getCookieHeaderForURL(url);
   assertNotNull(s);
   assertEquals("test=2", s);
 }
 public void testDuplicateCookie2() throws Exception {
   URL url = new URL("http://a.b.c/");
   man.addCookieFromHeader("test=1", url);
   man.addCookieFromHeader("test2=a", url);
   String s = man.getCookieHeaderForURL(url);
   assertNotNull(s);
   assertEquals("test=1; test2=a", s); // Assumes some kind of list is used
   man.addCookieFromHeader("test=2", url);
   man.addCookieFromHeader("test3=b", url);
   s = man.getCookieHeaderForURL(url);
   assertNotNull(s);
   assertEquals("test2=a; test=2; test3=b", s); // Assumes some kind of list is use
   // If not using a list that retains the order, then the asserts would need to change
 }
 /** Tests explicit root path with a non-trivial URL fetch from the domain */
 public void testRootPath1() throws Exception {
   URL url = new URL("http://d.e.f/moo.html");
   man.addCookieFromHeader("test=moo;path=/", url);
   String s = man.getCookieHeaderForURL(new URL("http://d.e.f/goo.html"));
   assertNotNull(s);
   assertEquals("test=moo", s);
 }
 public void testCookies2() throws Exception {
   URL url = new URL("https://a.b.c.d/testCookies2");
   man.addCookieFromHeader("test1=1;secure, test2=2;secure", url);
   assertEquals(2, man.getCookieCount());
   String s = man.getCookieHeaderForURL(url);
   assertNotNull(s);
   assertEquals("test1=1; test2=2", s);
 }
 // Test multi-cookie header handling
 public void testCookies1() throws Exception {
   URL url = new URL("http://a.b.c.d/testCookies1");
   man.addCookieFromHeader("test1=1; comment=\"how,now\", test2=2; version=1", url);
   assertEquals(2, man.getCookieCount());
   String s = man.getCookieHeaderForURL(url);
   assertNotNull(s);
   assertEquals("test1=1; test2=2", s);
 }
 // Test New cookie is returned
 public void testNewCookie() throws Exception {
   URL url = new URL("http://a.b.c/");
   man.addCookieFromHeader("test=1; expires=Mon, 01-Jan-2990 00:00:00 GMT", url);
   assertEquals(1, man.getCookieCount());
   String s = man.getCookieHeaderForURL(url);
   assertNotNull(s);
   assertEquals("test=1", s);
 }
 public void testCookieOrdering2() throws Exception {
   URL url = new URL("http://order.now/sub1/moo.html");
   man.addCookieFromHeader("test1=moo1;", url);
   man.addCookieFromHeader("test2=moo2;path=/sub1", url);
   man.addCookieFromHeader("test2=moo3;path=/", url);
   assertEquals(3, man.getCookieCount());
   assertEquals("/sub1", man.get(0).getPath()); // Defaults to caller URL
   assertEquals("/sub1", man.get(1).getPath());
   assertEquals("/", man.get(2).getPath());
   String s = man.getCookieHeaderForURL(url);
   assertNotNull(s);
   org.apache.commons.httpclient.Cookie[] c = man.getCookiesForUrl(url);
   assertEquals("/sub1", c[0].getPath());
   assertFalse(c[0].isPathAttributeSpecified());
   assertEquals("/sub1", c[1].getPath());
   assertTrue(c[1].isPathAttributeSpecified());
   assertEquals("/", c[2].getPath());
   assertEquals("test1=moo1; test2=moo2; test2=moo3", s);
 }
 // Bug 2063
 public void testCookieWithEquals() throws Exception {
   URL url = new URL("http://a.b.c/");
   man.addCookieFromHeader("NSCP_USER_LOGIN1_NEW=SHA=xxxxx", url);
   String s = man.getCookieHeaderForURL(url);
   assertNotNull(s);
   assertEquals("NSCP_USER_LOGIN1_NEW=SHA=xxxxx", s);
   Cookie c = man.get(0);
   assertEquals("NSCP_USER_LOGIN1_NEW", c.getName());
   assertEquals("SHA=xxxxx", c.getValue());
 }
 public void testCookiePolicyNetscape() throws Exception {
   man.setCookiePolicy(CookiePolicy.NETSCAPE);
   man.testStarted(); // ensure policy is picked up
   URL url = new URL("http://www.order.now/sub1/moo.html");
   man.addCookieFromHeader("test1=moo1;", url);
   man.addCookieFromHeader("test2=moo2;path=/sub1", url);
   man.addCookieFromHeader("test2=moo3;path=/", url);
   assertEquals(3, man.getCookieCount());
   assertEquals("/sub1", man.get(0).getPath());
   assertEquals("/sub1", man.get(1).getPath());
   assertEquals("/", man.get(2).getPath());
   String s = man.getCookieHeaderForURL(url);
   assertNotNull(s);
   org.apache.commons.httpclient.Cookie[] c = man.getCookiesForUrl(url);
   assertEquals("/sub1", c[0].getPath());
   assertFalse(c[0].isPathAttributeSpecified());
   assertEquals("/sub1", c[1].getPath());
   assertTrue(c[1].isPathAttributeSpecified());
   assertEquals("/", c[2].getPath());
   assertTrue(c[2].isPathAttributeSpecified());
   assertEquals("test1=moo1; test2=moo2; test2=moo3", s);
 }
 public void testCookiePolicyIgnore() throws Exception {
   man.setCookiePolicy(CookiePolicy.IGNORE_COOKIES);
   man.testStarted(); // ensure policy is picked up
   URL url = new URL("http://order.now/sub1/moo.html");
   man.addCookieFromHeader("test1=moo1;", url);
   man.addCookieFromHeader("test2=moo2;path=/sub1", url);
   man.addCookieFromHeader("test2=moo3;path=/", url);
   assertEquals(0, man.getCookieCount()); // Cookies are ignored
   Cookie cc;
   cc = new Cookie("test1", "moo1", null, "/sub1", false, 0, false, false);
   man.add(cc);
   cc = new Cookie("test2", "moo2", null, "/sub1", false, 0, true, false);
   man.add(cc);
   cc = new Cookie("test3", "moo3", null, "/", false, 0, false, false);
   man.add(cc);
   assertEquals(3, man.getCookieCount());
   assertEquals("/sub1", man.get(0).getPath());
   assertEquals("/sub1", man.get(1).getPath());
   assertEquals("/", man.get(2).getPath());
   String s = man.getCookieHeaderForURL(url);
   assertNull(s);
   org.apache.commons.httpclient.Cookie[] c = man.getCookiesForUrl(url);
   assertEquals(0, c.length); // Cookies again ignored
 }
  // Test cookie matching
  public void testCookieMatching() throws Exception {
    URL url = new URL("http://a.b.c:8080/TopDir/fred.jsp");
    man.addCookieFromHeader("ID=abcd; Path=/TopDir", url);
    String s = man.getCookieHeaderForURL(url);
    assertNotNull(s);
    assertEquals("ID=abcd", s);

    url = new URL("http://a.b.c:8080/other.jsp");
    s = man.getCookieHeaderForURL(url);
    assertNull(s);

    url = new URL("http://a.b.c:8080/TopDir/suub/another.jsp");
    s = man.getCookieHeaderForURL(url);
    assertNotNull(s);

    url = new URL("http://a.b.c:8080/TopDir");
    s = man.getCookieHeaderForURL(url);
    assertNotNull(s);

    url = new URL("http://a.b.d/");
    s = man.getCookieHeaderForURL(url);
    assertNull(s);
  }
 /**
  * Test that the cookie domain field is actually handled as browsers do (i.e.: host X matches
  * domain .X):
  */
 public void testDomainHandling() throws Exception {
   URL url = new URL("http://jakarta.apache.org/");
   man.addCookieFromHeader("test=1;domain=.jakarta.apache.org", url);
   assertNotNull(man.getCookieHeaderForURL(url));
 }
 // Test Old cookie is not returned
 public void testOldCookie() throws Exception {
   URL url = new URL("http://a.b.c/");
   man.addCookieFromHeader("test=1; expires=Mon, 01-Jan-1990 00:00:00 GMT", url);
   String s = man.getCookieHeaderForURL(url);
   assertNull(s);
 }
 /**
  * Test that we won't be tricked by similar host names (this was a past bug, although it never got
  * reported in the bug database):
  */
 public void testSimilarHostNames() throws Exception {
   URL url = new URL("http://ache.org/");
   man.addCookieFromHeader("test=1", url);
   url = new URL("http://jakarta.apache.org/");
   assertNull(man.getCookieHeaderForURL(url));
 }