BrowserCompatSpec extends CookieSpecBase { public List<Cookie> parse(final Header header, final CookieOrigin origin) throws MalformedCookieException { if (header == null) { throw new IllegalArgumentException("Header may not be null"); } if (origin == null) { throw new IllegalArgumentException("Cookie origin may not be null"); } String headername = header.getName(); if (!headername.equalsIgnoreCase(SM.SET_COOKIE)) { throw new MalformedCookieException("Unrecognized cookie header '" + header.toString() + "'"); } HeaderElement[] helems = header.getElements(); boolean versioned = false; boolean netscape = false; for (HeaderElement helem: helems) { if (helem.getParameterByName("version") != null) { versioned = true; } if (helem.getParameterByName("expires") != null) { netscape = true; } } if (netscape || !versioned) { NetscapeDraftHeaderParser parser = NetscapeDraftHeaderParser.DEFAULT; CharArrayBuffer buffer; ParserCursor cursor; if (header instanceof FormattedHeader) { buffer = ((FormattedHeader) header).getBuffer(); cursor = new ParserCursor( ((FormattedHeader) header).getValuePos(), buffer.length()); } else { String s = header.getValue(); if (s == null) { throw new MalformedCookieException("Header value is null"); } buffer = new CharArrayBuffer(s.length()); buffer.append(s); cursor = new ParserCursor(0, buffer.length()); } helems = new HeaderElement[] { parser.parseHeader(buffer, cursor) }; } return parse(helems, origin); } BrowserCompatSpec(final String[] datepatterns);  BrowserCompatSpec(); List<Cookie> parse(final Header header, final CookieOrigin origin); List<Header> formatCookies(final List<Cookie> cookies); int getVersion(); Header getVersionHeader(); @Override String toString();  }
@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()); }
@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()); }
@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) { } }
@Test public void testParseNoName() throws Exception { Header header = new BasicHeader("Set-Cookie","=stuff; path=/"); 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 ex) { } }
@Test public void testParseWithWhiteSpace() throws Exception { Header header = new BasicHeader("Set-Cookie"," cookie-name = cookie-value "); 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.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()); }
@Test public void testParseWithQuotes() throws Exception { Header header = new BasicHeader("Set-Cookie"," cookie-name = \" cookie-value \" ;path=/"); 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.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()); }
@Test public void testParseWithPath() throws Exception { Header header = new BasicHeader("Set-Cookie","cookie-name=cookie-value; Path=/path/"); CookieSpec cookiespec = new BrowserCompatSpec(); CookieOrigin origin = new CookieOrigin("127.0.0.1", 80, "/path/path", false); 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","/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()); }
@Test public void testParseWithDomain() throws Exception { Header header = new BasicHeader("Set-Cookie","cookie-name=cookie-value; Domain=127.0.0.1"); 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.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()); }
@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()); }
@Test public void testParseWithComment() throws Exception { Header header = new BasicHeader("Set-Cookie", "cookie-name=cookie-value; comment=\"This is a comment.\""); 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.assertEquals("Comment","\"This is a comment.\"",cookies.get(0).getComment()); }
@Test public void testParseWithExpires() throws Exception { Header header = new BasicHeader("Set-Cookie", "cookie-name=cookie-value;Expires=Thu, 01-Jan-1970 00:00:10 GMT"); 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.assertEquals(new Date(10000L),cookies.get(0).getExpiryDate()); Assert.assertTrue("Comment",null == cookies.get(0).getComment()); }
@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()); }
@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()); }
@Test public void testParseWithWrongDomain() throws Exception { Header header = new BasicHeader("Set-Cookie", "cookie-name=cookie-value; domain=127.0.0.1; version=1"); CookieSpec cookiespec = new BrowserCompatSpec(); CookieOrigin origin = new CookieOrigin("127.0.0.2", 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 exception should have been thrown"); } catch (MalformedCookieException ex) { } }
@Test public void testParseWithPathMismatch() throws Exception { Header header = new BasicHeader("Set-Cookie", "cookie-name=cookie-value; path=/path/path/path"); CookieSpec cookiespec = new BrowserCompatSpec(); CookieOrigin origin = new CookieOrigin("127.0.0.1", 80, "/path", 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) { } }
@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) { } }
@Test public void testCookieNameWithBlanks() 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()); }
@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) { } }
@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()); }
@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) { } }