コード例 #1
0
 @Test
 public void testInvalidInput() throws Exception {
   CookieSpec cookiespec = new BrowserCompatSpec();
   try {
     cookiespec.parse(null, null);
     Assert.fail("IllegalArgumentException must have been thrown");
   } catch (IllegalArgumentException ex) {
     // expected
   }
   try {
     cookiespec.parse(new BasicHeader("Set-Cookie", "name=value"), null);
     Assert.fail("IllegalArgumentException must have been thrown");
   } catch (IllegalArgumentException ex) {
     // expected
   }
   try {
     cookiespec.validate(null, null);
     Assert.fail("IllegalArgumentException must have been thrown");
   } catch (IllegalArgumentException ex) {
     // expected
   }
   try {
     cookiespec.validate(new BasicClientCookie("name", null), null);
     Assert.fail("IllegalArgumentException must have been thrown");
   } catch (IllegalArgumentException ex) {
     // expected
   }
   try {
     cookiespec.match(null, null);
     Assert.fail("IllegalArgumentException must have been thrown");
   } catch (IllegalArgumentException ex) {
     // expected
   }
   try {
     cookiespec.match(new BasicClientCookie("name", null), null);
     Assert.fail("IllegalArgumentException must have been thrown");
   } catch (IllegalArgumentException ex) {
     // expected
   }
   try {
     cookiespec.formatCookies(null);
     Assert.fail("IllegalArgumentException must have been thrown");
   } catch (IllegalArgumentException ex) {
     // expected
   }
   try {
     List<Cookie> cookies = new ArrayList<Cookie>();
     cookiespec.formatCookies(cookies);
     Assert.fail("IllegalArgumentException must have been thrown");
   } catch (IllegalArgumentException ex) {
     // expected
   }
 }
コード例 #2
0
  /** Test basic parse (with various spacings */
  @Test
  public void testParse1() throws Exception {
    String headerValue =
        "custno = 12345; comment=test; version=1,"
            + " name=John; version=1; max-age=600; secure; domain=.apache.org";

    Header header = new BasicHeader("set-cookie", headerValue);

    CookieSpec cookiespec = new BrowserCompatSpec();
    CookieOrigin origin = new CookieOrigin("www.apache.org", 80, "/", false);
    List<Cookie> cookies = cookiespec.parse(header, origin);
    for (int i = 0; i < cookies.size(); i++) {
      cookiespec.validate(cookies.get(i), origin);
    }
    Assert.assertEquals(2, cookies.size());

    Assert.assertEquals("custno", cookies.get(0).getName());
    Assert.assertEquals("12345", cookies.get(0).getValue());
    Assert.assertEquals("test", cookies.get(0).getComment());
    Assert.assertEquals(0, cookies.get(0).getVersion());
    Assert.assertEquals("www.apache.org", cookies.get(0).getDomain());
    Assert.assertEquals("/", cookies.get(0).getPath());
    Assert.assertFalse(cookies.get(0).isSecure());

    Assert.assertEquals("name", cookies.get(1).getName());
    Assert.assertEquals("John", cookies.get(1).getValue());
    Assert.assertEquals(null, cookies.get(1).getComment());
    Assert.assertEquals(0, cookies.get(1).getVersion());
    Assert.assertEquals(".apache.org", cookies.get(1).getDomain());
    Assert.assertEquals("/", cookies.get(1).getPath());
    Assert.assertTrue(cookies.get(1).isSecure());
  }
コード例 #3
0
 private void checkDate(String date) throws Exception {
   Header header = new BasicHeader("Set-Cookie", "custno=12345;Expires='" + date + "';");
   CookieSpec cookiespec = new BrowserCompatSpec();
   CookieOrigin origin = new CookieOrigin("localhost", 80, "/", false);
   List<Cookie> cookies = cookiespec.parse(header, origin);
   for (int i = 0; i < cookies.size(); i++) {
     cookiespec.validate(cookies.get(i), origin);
   }
 }
コード例 #4
0
  /**
   * Tests if invalid second domain level cookie gets accepted in the browser compatibility mode.
   */
  @Test
  public void testSecondDomainLevelCookie() throws Exception {
    BasicClientCookie cookie = new BasicClientCookie("name", null);
    cookie.setDomain(".sourceforge.net");
    cookie.setAttribute(ClientCookie.DOMAIN_ATTR, cookie.getDomain());
    cookie.setPath("/");
    cookie.setAttribute(ClientCookie.PATH_ATTR, cookie.getPath());

    CookieSpec cookiespec = new BrowserCompatSpec();
    CookieOrigin origin = new CookieOrigin("sourceforge.net", 80, "/", false);
    cookiespec.validate(cookie, origin);
  }
コード例 #5
0
 /** Tests generic cookie formatting. */
 @Test
 public void testGenericCookieFormatting() throws Exception {
   Header header = new BasicHeader("Set-Cookie", "name=value; path=/; domain=.mydomain.com");
   CookieSpec cookiespec = new BrowserCompatSpec();
   CookieOrigin origin = new CookieOrigin("myhost.mydomain.com", 80, "/", false);
   List<Cookie> cookies = cookiespec.parse(header, origin);
   cookiespec.validate(cookies.get(0), origin);
   List<Header> headers = cookiespec.formatCookies(cookies);
   Assert.assertNotNull(headers);
   Assert.assertEquals(1, headers.size());
   Assert.assertEquals("name=value", headers.get(0).getValue());
 }
コード例 #6
0
 /** Tests if cookie constructor rejects cookie name starting with $. */
 @Test
 public void testCookieNameStartingWithDollarSign() throws Exception {
   Header header = new BasicHeader("Set-Cookie", "$invalid_name=");
   CookieSpec cookiespec = new BrowserCompatSpec();
   CookieOrigin origin = new CookieOrigin("127.0.0.1", 80, "/", false);
   List<Cookie> cookies = cookiespec.parse(header, origin);
   for (int i = 0; i < cookies.size(); i++) {
     cookiespec.validate(cookies.get(i), origin);
   }
   Assert.assertNotNull(cookies);
   Assert.assertEquals(1, cookies.size());
 }
コード例 #7
0
  @Test
  public void testInvalidMatchDomain() throws Exception {
    BasicClientCookie cookie = new BasicClientCookie("name", null);
    cookie.setDomain("beta.gamma.com");
    cookie.setAttribute(ClientCookie.DOMAIN_ATTR, cookie.getDomain());
    cookie.setPath("/");
    cookie.setAttribute(ClientCookie.PATH_ATTR, cookie.getPath());

    CookieSpec cookiespec = new BrowserCompatSpec();
    CookieOrigin origin = new CookieOrigin("alpha.beta.gamma.com", 80, "/", false);
    cookiespec.validate(cookie, origin);
    Assert.assertTrue(cookiespec.match(cookie, origin));
  }
コード例 #8
0
  /** Tests whether domain attribute check is case-insensitive. */
  @Test
  public void testDomainCaseInsensitivity() throws Exception {
    Header header = new BasicHeader("Set-Cookie", "name=value; path=/; domain=.whatever.com");

    CookieSpec cookiespec = new BrowserCompatSpec();
    CookieOrigin origin = new CookieOrigin("www.WhatEver.com", 80, "/", false);
    List<Cookie> cookies = cookiespec.parse(header, origin);
    for (int i = 0; i < cookies.size(); i++) {
      cookiespec.validate(cookies.get(i), origin);
    }
    Assert.assertNotNull(cookies);
    Assert.assertEquals(1, cookies.size());
    Assert.assertEquals(".whatever.com", cookies.get(0).getDomain());
  }
コード例 #9
0
 /** Tests if cookie constructor rejects cookie name containing blanks. */
 @Test
 public void testCookieNameBlank() throws Exception {
   Header header = new BasicHeader("Set-Cookie", "=stuff");
   CookieSpec cookiespec = new BrowserCompatSpec();
   CookieOrigin origin = new CookieOrigin("127.0.0.1", 80, "/", false);
   try {
     List<Cookie> cookies = cookiespec.parse(header, origin);
     for (int i = 0; i < cookies.size(); i++) {
       cookiespec.validate(cookies.get(i), origin);
     }
     Assert.fail("MalformedCookieException should have been thrown");
   } catch (MalformedCookieException expected) {
   }
 }
コード例 #10
0
  @Test
  public void testParseRelativePath() throws Exception {
    Header header = new BasicHeader("Set-Cookie", "name1=value1;Path=whatever");

    CookieSpec cookiespec = new BrowserCompatSpec();
    CookieOrigin origin = new CookieOrigin("www.apache.org", 80, "whatever", true);
    List<Cookie> cookies = cookiespec.parse(header, origin);
    for (int i = 0; i < cookies.size(); i++) {
      cookiespec.validate(cookies.get(i), origin);
    }
    Assert.assertEquals("Found 1 cookies.", 1, cookies.size());
    Assert.assertEquals("Name", "name1", cookies.get(0).getName());
    Assert.assertEquals("Value", "value1", cookies.get(0).getValue());
    Assert.assertEquals("Path", "whatever", cookies.get(0).getPath());
  }
コード例 #11
0
  // see issue #5279
  @Test
  public void testQuotedExpiresAttribute() throws Exception {
    String headerValue = "custno=12345;Expires='Thu, 01-Jan-2070 00:00:10 GMT'";

    Header header = new BasicHeader("set-cookie", headerValue);

    CookieSpec cookiespec = new BrowserCompatSpec();
    CookieOrigin origin = new CookieOrigin("www.apache.org", 80, "/", true);
    List<Cookie> cookies = cookiespec.parse(header, origin);
    for (int i = 0; i < cookies.size(); i++) {
      cookiespec.validate(cookies.get(i), origin);
    }
    Assert.assertNotNull("Expected some cookies", cookies);
    Assert.assertEquals("Expected 1 cookie", 1, cookies.size());
    Assert.assertNotNull("Expected cookie to have getExpiryDate", cookies.get(0).getExpiryDate());
  }
コード例 #12
0
  @Test
  public void testParseWithPathMismatch2() throws Exception {
    Header header = new BasicHeader("Set-Cookie", "cookie-name=cookie-value; path=/foobar");

    CookieSpec cookiespec = new BrowserCompatSpec();
    CookieOrigin origin = new CookieOrigin("127.0.0.1", 80, "/foo", false);
    try {
      List<Cookie> cookies = cookiespec.parse(header, origin);
      for (int i = 0; i < cookies.size(); i++) {
        cookiespec.validate(cookies.get(i), origin);
      }
      Assert.fail("MalformedCookieException should have been thrown.");
    } catch (MalformedCookieException e) {
      // expected
    }
  }
コード例 #13
0
  /** Tests if malformatted expires attribute is cookies correctly. */
  @Test
  public void testCookieWithComma() throws Exception {
    Header header =
        new BasicHeader("Set-Cookie", "name=value; expires=\"Thu, 01-Jan-1970 00:00:00 GMT");

    CookieSpec cookiespec = new BrowserCompatSpec();
    CookieOrigin origin = new CookieOrigin("localhost", 80, "/", false);
    try {
      List<Cookie> cookies = cookiespec.parse(header, origin);
      for (int i = 0; i < cookies.size(); i++) {
        cookiespec.validate(cookies.get(i), origin);
      }
      Assert.fail("MalformedCookieException should have been thrown");
    } catch (MalformedCookieException expected) {
    }
  }
コード例 #14
0
  @Test
  public void testParseWithSecure() throws Exception {
    Header header = new BasicHeader("Set-Cookie", "cookie-name=cookie-value; secure");

    CookieSpec cookiespec = new BrowserCompatSpec();
    CookieOrigin origin = new CookieOrigin("127.0.0.1", 80, "/", true);
    List<Cookie> cookies = cookiespec.parse(header, origin);
    for (int i = 0; i < cookies.size(); i++) {
      cookiespec.validate(cookies.get(i), origin);
    }
    Assert.assertEquals("Found 1 cookie.", 1, cookies.size());
    Assert.assertEquals("Name", "cookie-name", cookies.get(0).getName());
    Assert.assertEquals("Value", "cookie-value", cookies.get(0).getValue());
    Assert.assertEquals("Domain", "127.0.0.1", cookies.get(0).getDomain());
    Assert.assertEquals("Path", "/", cookies.get(0).getPath());
    Assert.assertTrue("Secure", cookies.get(0).isSecure());
    Assert.assertTrue("ExpiryDate", null == cookies.get(0).getExpiryDate());
    Assert.assertTrue("Comment", null == cookies.get(0).getComment());
  }
コード例 #15
0
  @Test
  public void testSecurityError() throws Exception {
    String headerValue =
        "custno=12345;comment=test; version=1,"
            + "name=John;version=1;max-age=600;secure;domain=jakarta.apache.org";
    Header header = new BasicHeader("set-cookie", headerValue);

    CookieSpec cookiespec = new BrowserCompatSpec();
    CookieOrigin origin = new CookieOrigin("www.apache.org", 80, "/", true);
    try {
      List<Cookie> cookies = cookiespec.parse(header, origin);
      for (int i = 0; i < cookies.size(); i++) {
        cookiespec.validate(cookies.get(i), origin);
      }
      Assert.fail("MalformedCookieException exception should have been thrown");
    } catch (MalformedCookieException ex) {
      // expected
    }
  }
コード例 #16
0
  @Test
  public void testParseMultipleDifferentPaths() throws Exception {
    Header header =
        new BasicHeader(
            "Set-Cookie",
            "name1=value1;Version=1;Path=/commons,name1=value2;Version=1;"
                + "Path=/commons/httpclient;Version=1");

    CookieSpec cookiespec = new BrowserCompatSpec();
    CookieOrigin origin = new CookieOrigin("www.apache.org", 80, "/commons/httpclient", true);
    List<Cookie> cookies = cookiespec.parse(header, origin);
    for (int i = 0; i < cookies.size(); i++) {
      cookiespec.validate(cookies.get(i), origin);
    }
    Assert.assertEquals("Wrong number of cookies.", 2, cookies.size());
    Assert.assertEquals("Name", "name1", cookies.get(0).getName());
    Assert.assertEquals("Value", "value1", cookies.get(0).getValue());
    Assert.assertEquals("Name", "name1", cookies.get(1).getName());
    Assert.assertEquals("Value", "value2", cookies.get(1).getValue());
  }
コード例 #17
0
  @Test
  public void testParseWithAll() throws Exception {
    Header header =
        new BasicHeader(
            "Set-Cookie",
            "cookie-name=cookie-value;Version=1;Path=/commons;Domain=.apache.org;"
                + "Comment=This is a comment.;secure;Expires=Thu, 01-Jan-1970 00:00:10 GMT");

    CookieSpec cookiespec = new BrowserCompatSpec();
    CookieOrigin origin = new CookieOrigin("www.apache.org", 80, "/commons/httpclient", true);
    List<Cookie> cookies = cookiespec.parse(header, origin);
    for (int i = 0; i < cookies.size(); i++) {
      cookiespec.validate(cookies.get(i), origin);
    }
    Assert.assertEquals("Found 1 cookie.", 1, cookies.size());
    Assert.assertEquals("Name", "cookie-name", cookies.get(0).getName());
    Assert.assertEquals("Value", "cookie-value", cookies.get(0).getValue());
    Assert.assertEquals("Domain", ".apache.org", cookies.get(0).getDomain());
    Assert.assertEquals("Path", "/commons", cookies.get(0).getPath());
    Assert.assertTrue("Secure", cookies.get(0).isSecure());
    Assert.assertEquals(new Date(10000L), cookies.get(0).getExpiryDate());
    Assert.assertEquals("Comment", "This is a comment.", cookies.get(0).getComment());
    Assert.assertEquals("Version", 0, cookies.get(0).getVersion());
  }