@Override public void afterPropertiesSet() throws Exception { cookieSpecRegistry = RegistryBuilder.<CookieSpecProvider> create().register("easy", new CookieSpecProvider() { public CookieSpec create(HttpContext context) { return new DefaultCookieSpec() { @Override public void validate(Cookie cookie, CookieOrigin origin) throws MalformedCookieException { } }; } }).build(); requestConfig = RequestConfig.custom().setCookieSpec("easy") .setConnectionRequestTimeout(propertyConfigurer.getIntValue("connection.request.timeout")) .setSocketTimeout(propertyConfigurer.getIntValue("socket_timeout")) .setConnectTimeout(propertyConfigurer.getIntValue("connection_timeout")).build(); }
public CookieSpec newInstance(final HttpParams params) { if (params != null) { String[] patterns = null; Collection<?> param = (Collection<?>) params.getParameter( CookieSpecPNames.DATE_PATTERNS); if (param != null) { patterns = new String[param.size()]; patterns = param.toArray(patterns); } boolean singleHeader = params.getBooleanParameter( CookieSpecPNames.SINGLE_COOKIE_HEADER, false); return new BestMatchSpec(patterns, singleHeader); } else { return new BestMatchSpec(); } }
public CookieSpec newInstance(final HttpParams params) { if (params != null) { String[] patterns = null; Collection<?> param = (Collection<?>) params.getParameter( CookieSpecPNames.DATE_PATTERNS); if (param != null) { patterns = new String[param.size()]; patterns = param.toArray(patterns); } boolean singleHeader = params.getBooleanParameter( CookieSpecPNames.SINGLE_COOKIE_HEADER, false); return new RFC2109Spec(patterns, singleHeader); } else { return new RFC2109Spec(); } }
public CookieSpec newInstance(final HttpParams params) { if (params != null) { String[] patterns = null; Collection<?> param = (Collection<?>) params.getParameter( CookieSpecPNames.DATE_PATTERNS); if (param != null) { patterns = new String[param.size()]; patterns = param.toArray(patterns); } boolean singleHeader = params.getBooleanParameter( CookieSpecPNames.SINGLE_COOKIE_HEADER, false); return new RFC2965Spec(patterns, singleHeader); } else { return new RFC2965Spec(); } }
public CookieSpec newInstance(final HttpParams params) { if (params != null) { String[] patterns = null; final Collection<?> param = (Collection<?>) params.getParameter( CookieSpecPNames.DATE_PATTERNS); if (param != null) { patterns = new String[param.size()]; patterns = param.toArray(patterns); } final boolean singleHeader = params.getBooleanParameter( CookieSpecPNames.SINGLE_COOKIE_HEADER, false); return new RFC2109SpecHC4(patterns, singleHeader); } else { return new RFC2109SpecHC4(); } }
@Test public void testCookieNullDomainNullPathFormatting() { final BasicClientCookie cookie = new BasicClientCookie("name", null); cookie.setPath("/"); cookie.setAttribute(ClientCookie.PATH_ATTR, cookie.getPath()); final CookieSpec cookiespec = new RFC2109Spec(); List<Cookie> cookies = new ArrayList<Cookie>(); cookies.add(cookie); List<Header> headers = cookiespec.formatCookies(cookies); Assert.assertNotNull(headers); Assert.assertEquals(1, headers.size()); Assert.assertEquals("$Version=0; name=; $Path=/", headers.get(0).getValue()); cookie.removeAttribute(ClientCookie.DOMAIN_ATTR); cookie.removeAttribute(ClientCookie.PATH_ATTR); cookies = new ArrayList<Cookie>(); cookies.add(cookie); headers = cookiespec.formatCookies(cookies); Assert.assertNotNull(headers); Assert.assertEquals(1, headers.size()); Assert.assertEquals("$Version=0; name=", headers.get(0).getValue()); }
public CookieSpec newInstance(final HttpParams params) { if (params != null) { String[] patterns = null; final Collection<?> param = (Collection<?>) params.getParameter( CookieSpecPNames.DATE_PATTERNS); if (param != null) { patterns = new String[param.size()]; patterns = param.toArray(patterns); } final boolean singleHeader = params.getBooleanParameter( CookieSpecPNames.SINGLE_COOKIE_HEADER, false); return new BestMatchSpecHC4(patterns, singleHeader); } else { return new BestMatchSpecHC4(); } }
public CookieSpec newInstance(final HttpParams params) { if (params != null) { String[] patterns = null; final Collection<?> param = (Collection<?>) params.getParameter( CookieSpecPNames.DATE_PATTERNS); if (param != null) { patterns = new String[param.size()]; patterns = param.toArray(patterns); } final boolean singleHeader = params.getBooleanParameter( CookieSpecPNames.SINGLE_COOKIE_HEADER, false); return new RFC2965SpecHC4(patterns, singleHeader); } else { return new RFC2965SpecHC4(); } }
@Override public CookieSpec newInstance(final HttpParams params) { if (params != null) { String[] patterns = null; final Collection<?> param = (Collection<?>) params.getParameter( CookieSpecPNames.DATE_PATTERNS); if (param != null) { patterns = new String[param.size()]; patterns = param.toArray(patterns); } final boolean singleHeader = params.getBooleanParameter( CookieSpecPNames.SINGLE_COOKIE_HEADER, false); return new BestMatchSpec(patterns, singleHeader); } else { return new BestMatchSpec(); } }
@Override public CookieSpec newInstance(final HttpParams params) { if (params != null) { String[] patterns = null; final Collection<?> param = (Collection<?>) params.getParameter( CookieSpecPNames.DATE_PATTERNS); if (param != null) { patterns = new String[param.size()]; patterns = param.toArray(patterns); } return new NetscapeDraftSpec(patterns); } else { return new NetscapeDraftSpec(); } }
@Override public CookieSpec newInstance(final HttpParams params) { if (params != null) { String[] patterns = null; final Collection<?> param = (Collection<?>) params.getParameter( CookieSpecPNames.DATE_PATTERNS); if (param != null) { patterns = new String[param.size()]; patterns = param.toArray(patterns); } final boolean singleHeader = params.getBooleanParameter( CookieSpecPNames.SINGLE_COOKIE_HEADER, false); return new RFC2109Spec(patterns, singleHeader); } else { return new RFC2109Spec(); } }
@Override public CookieSpec newInstance(final HttpParams params) { if (params != null) { String[] patterns = null; final Collection<?> param = (Collection<?>) params.getParameter( CookieSpecPNames.DATE_PATTERNS); if (param != null) { patterns = new String[param.size()]; patterns = param.toArray(patterns); } return new BrowserCompatSpec(patterns, securityLevel); } else { return new BrowserCompatSpec(null, securityLevel); } }
@Override public CookieSpec newInstance(final HttpParams params) { if (params != null) { String[] patterns = null; final Collection<?> param = (Collection<?>) params.getParameter( CookieSpecPNames.DATE_PATTERNS); if (param != null) { patterns = new String[param.size()]; patterns = param.toArray(patterns); } final boolean singleHeader = params.getBooleanParameter( CookieSpecPNames.SINGLE_COOKIE_HEADER, false); return new RFC2965Spec(patterns, singleHeader); } else { return new RFC2965Spec(); } }
@Override public CookieSpec create(final HttpContext context) { if (cookieSpec == null) { synchronized (this) { if (cookieSpec == null) { this.cookieSpec = new RFC2109Spec(this.oneHeader, new RFC2109VersionHandler(), new BasicPathHandler(), PublicSuffixDomainFilter.decorate( new RFC2109DomainHandler(), this.publicSuffixMatcher), new BasicMaxAgeHandler(), new BasicSecureHandler(), new BasicCommentHandler()); } } } return this.cookieSpec; }
@Override public CookieSpec create(final HttpContext context) { if (cookieSpec == null) { synchronized (this) { if (cookieSpec == null) { this.cookieSpec = new RFC2965Spec(this.oneHeader, new RFC2965VersionAttributeHandler(), new BasicPathHandler(), PublicSuffixDomainFilter.decorate( new RFC2965DomainAttributeHandler(), this.publicSuffixMatcher), new RFC2965PortAttributeHandler(), new BasicMaxAgeHandler(), new BasicSecureHandler(), new BasicCommentHandler(), new RFC2965CommentUrlAttributeHandler(), new RFC2965DiscardAttributeHandler()); } } } return this.cookieSpec; }
/** * Tests RFC 2109 compiant cookie formatting. */ @Test public void testRFC2109CookieFormatting() throws Exception { final CookieSpec cookiespec = new RFC2109Spec(null, false); Header header = new BasicHeader("Set-Cookie", "name=\"value\"; version=1; path=\"/\"; domain=\".mydomain.com\""); final 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("$Version=1; name=\"value\"; $Path=\"/\"; $Domain=\".mydomain.com\"", headers.get(0).getValue()); header = new BasicHeader( "Set-Cookie", "name=value; path=/; domain=.mydomain.com"); cookies = cookiespec.parse(header, origin); cookiespec.validate(cookies.get(0), origin); headers = cookiespec.formatCookies(cookies); Assert.assertNotNull(headers); Assert.assertEquals(1, headers.size()); Assert.assertEquals("$Version=0; name=value; $Path=/; $Domain=.mydomain.com", headers.get(0).getValue()); }
@Test public void testParseAbsPath() throws Exception { final Header header = new BasicHeader("Set-Cookie", "name1=value1;Path=/path/"); final CookieSpec cookiespec = new NetscapeDraftSpec(); final CookieOrigin origin = new CookieOrigin("host", 80, "/path/", true); final 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("Domain","host",cookies.get(0).getDomain()); Assert.assertEquals("Path","/path/",cookies.get(0).getPath()); }
@Test public void testParseAbsPath2() throws Exception { final Header header = new BasicHeader("Set-Cookie", "name1=value1;Path=/"); final CookieSpec cookiespec = new NetscapeDraftSpec(); final CookieOrigin origin = new CookieOrigin("host", 80, "/", true); final 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("Domain","host",cookies.get(0).getDomain()); Assert.assertEquals("Path","/",cookies.get(0).getPath()); }
@Test public void testParseRelativePath() throws Exception { final Header header = new BasicHeader("Set-Cookie", "name1=value1;Path=whatever"); final CookieSpec cookiespec = new NetscapeDraftSpec(); final CookieOrigin origin = new CookieOrigin("host", 80, "whatever", true); final 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("Domain","host",cookies.get(0).getDomain()); Assert.assertEquals("Path","whatever",cookies.get(0).getPath()); }
@Test public void testParseWithWrongNetscapeDomain2() throws Exception { final Header header = new BasicHeader("Set-Cookie","cookie-name=cookie-value; domain=.y.z"); final CookieSpec cookiespec = new NetscapeDraftSpec(); try { final CookieOrigin origin = new CookieOrigin("x.y.z", 80, "/", false); final 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 (final MalformedCookieException e) { // expected } }
@Test public void testParseVersionIgnored() throws Exception { final Header header = new BasicHeader("Set-Cookie", "name1=value1;Path=/path/;Version=1;"); final CookieSpec cookiespec = new NetscapeDraftSpec(); final CookieOrigin origin = new CookieOrigin("host", 80, "/path/", true); final 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()); final Cookie cookie = cookies.get(0); Assert.assertEquals("Name","name1", cookie.getName()); Assert.assertEquals("Value", "value1", cookie.getValue()); Assert.assertEquals("Domain", "host", cookie.getDomain()); Assert.assertEquals("Path","/path/", cookie.getPath()); Assert.assertEquals(0, cookie.getVersion()); }
/** * Tests Netscape specific expire attribute parsing. */ @Test public void testNetscapeCookieExpireAttribute() throws Exception { final CookieSpec cookiespec = new NetscapeDraftSpec(); final Header header = new BasicHeader("Set-Cookie", "name=value; path=/; domain=.mydomain.com; expires=Thu, 01-Jan-2070 00:00:10 GMT; comment=no_comment"); final CookieOrigin origin = new CookieOrigin("myhost.mydomain.com", 80, "/", false); final List<Cookie> cookies = cookiespec.parse(header, origin); cookiespec.validate(cookies.get(0), origin); Assert.assertNotNull(cookies); Assert.assertEquals(1, cookies.size()); final Cookie cookie = cookies.get(0); final Calendar c = Calendar.getInstance(); c.setTimeZone(TimeZone.getTimeZone("GMT")); c.setTime(cookie.getExpiryDate()); final int year = c.get(Calendar.YEAR); Assert.assertEquals(2070, year); }
/** * Expire attribute with two digit year. */ @Test public void testNetscapeCookieExpireAttributeTwoDigitYear() throws Exception { final CookieSpec cookiespec = new NetscapeDraftSpec(); final Header header = new BasicHeader("Set-Cookie", "name=value; path=/; domain=.mydomain.com; expires=Thursday, 01-Jan-70 00:00:10 GMT; comment=no_comment"); final CookieOrigin origin = new CookieOrigin("myhost.mydomain.com", 80, "/", false); final List<Cookie> cookies = cookiespec.parse(header, origin); cookiespec.validate(cookies.get(0), origin); Assert.assertNotNull(cookies); Assert.assertEquals(1, cookies.size()); final Cookie cookie = cookies.get(0); final Calendar c = Calendar.getInstance(); c.setTimeZone(TimeZone.getTimeZone("GMT")); c.setTime(cookie.getExpiryDate()); final int year = c.get(Calendar.YEAR); Assert.assertEquals(2070, year); }
@Test public void testFormatCookies() throws Exception { final BasicClientCookie c1 = new BasicClientCookie("name1", "value1"); c1.setDomain(".whatever.com"); c1.setAttribute(ClientCookie.DOMAIN_ATTR, c1.getDomain()); c1.setPath("/"); c1.setAttribute(ClientCookie.PATH_ATTR, c1.getPath()); final Cookie c2 = new BasicClientCookie("name2", "value2"); final Cookie c3 = new BasicClientCookie("name3", null); final CookieSpec cookiespec = new NetscapeDraftSpec(); final List<Cookie> cookies = new ArrayList<Cookie>(); cookies.add(c1); cookies.add(c2); cookies.add(c3); final List<Header> headers = cookiespec.formatCookies(cookies); Assert.assertNotNull(headers); Assert.assertEquals(1, headers.size()); Assert.assertEquals("name1=value1; name2=value2; name3", headers.get(0).getValue()); }
/** * Tests if that invalid second domain level cookie gets * rejected in the strict mode, but gets accepted in the * browser compatibility mode. */ @Test public void testSecondDomainLevelCookie() throws Exception { final 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()); final CookieSpec cookiespec = new RFC2109Spec(); final CookieOrigin origin = new CookieOrigin("sourceforge.net", 80, "/", false); try { cookiespec.validate(cookie, origin); Assert.fail("MalformedCookieException should have been thrown"); } catch (final MalformedCookieException e) { // Expected } }
/** * Test parsing cookie {@code "Port"} attribute. */ @Test public void testParsePort() throws Exception { final CookieSpec cookiespec = new RFC2965Spec(); final CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false); final Header header = new BasicHeader("Set-Cookie2", "name=value;Port=\"80,800,8000\";Version=1;Port=nonsense"); final List<Cookie> cookies = cookiespec.parse(header, origin); Assert.assertNotNull(cookies); Assert.assertEquals(1, cookies.size()); // only the first occurrence of port attribute is considered, others ignored final ClientCookie cookie = (ClientCookie) cookies.get(0); final int[] ports = cookie.getPorts(); Assert.assertNotNull(ports); Assert.assertEquals(3, ports.length); Assert.assertEquals(80, ports[0]); Assert.assertEquals(800, ports[1]); Assert.assertEquals(8000, ports[2]); Assert.assertTrue(cookie.containsAttribute(ClientCookie.PORT_ATTR)); }
@Test public void testParseNullPort() throws Exception { final CookieSpec cookiespec = new RFC2965Spec(); final CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false); // null port defaults to request port final Header header = new BasicHeader("Set-Cookie2", "name=value;Port=;Version=1"); final List<Cookie> cookies = cookiespec.parse(header, origin); Assert.assertNotNull(cookies); Assert.assertEquals(1, cookies.size()); final ClientCookie cookie = (ClientCookie) cookies.get(0); final int[] ports = cookie.getPorts(); Assert.assertNotNull(ports); Assert.assertEquals(1, ports.length); Assert.assertEquals(80, ports[0]); Assert.assertEquals("", cookie.getAttribute(ClientCookie.PORT_ATTR)); }
@Test public void testParseBlankPort() throws Exception { final CookieSpec cookiespec = new RFC2965Spec(); final CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false); // blank port defaults to request port final Header header = new BasicHeader("Set-Cookie2", "name=value;Port=\" \";Version=1"); final List<Cookie> cookies = cookiespec.parse(header, origin); Assert.assertNotNull(cookies); Assert.assertEquals(1, cookies.size()); final ClientCookie cookie = (ClientCookie) cookies.get(0); final int[] ports = cookie.getPorts(); Assert.assertNotNull(ports); Assert.assertEquals(1, ports.length); Assert.assertEquals(80, ports[0]); Assert.assertEquals(" ", cookie.getAttribute(ClientCookie.PORT_ATTR)); }
/** * Tests if cookie constructor rejects cookie name containing blanks. */ @Test public void testCookieNameWithBlanks() throws Exception { final Header setcookie = new BasicHeader("Set-Cookie", "invalid name="); final CookieSpec cookiespec = new RFC2109Spec(); final CookieOrigin origin = new CookieOrigin("127.0.0.1", 80, "/", false); try { final List<Cookie> cookies = cookiespec.parse(setcookie, origin); for (int i = 0; i < cookies.size(); i++) { cookiespec.validate(cookies.get(i), origin); } Assert.fail("MalformedCookieException exception should have been thrown"); } catch (final MalformedCookieException e) { // expected } }
/** * Test {@code Domain} validation when domain is not specified * in {@code Set-Cookie2} header. */ @Test public void testValidateNoDomain() throws Exception { final CookieSpec cookiespec = new RFC2965Spec(); final CookieOrigin origin = new CookieOrigin("www.domain.com", 80, "/", false); final Header header = new BasicHeader("Set-Cookie2", "name=value;Version=1"); final 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()); final ClientCookie cookie = (ClientCookie) cookies.get(0); // cookie domain must string match request host Assert.assertEquals("www.domain.com", cookie.getDomain()); }
/** * test cookie {@code Domain} matching. */ @Test public void testMatchDomain() throws Exception { final BasicClientCookie2 cookie = new BasicClientCookie2("name", "value"); cookie.setDomain(".domain.com"); cookie.setPath("/"); cookie.setPorts(new int[] {80}); final CookieSpec cookiespec = new RFC2965Spec(); // effective host name minus domain must not contain any dots final CookieOrigin origin1 = new CookieOrigin("a.b.domain.com" /* request host */, 80, "/", false); Assert.assertFalse(cookiespec.match(cookie, origin1)); // The effective host name MUST domain-match the Domain // attribute of the cookie. final CookieOrigin origin2 = new CookieOrigin("www.domain.org" /* request host */, 80, "/", false); Assert.assertFalse(cookiespec.match(cookie, origin2)); final CookieOrigin origin3 = new CookieOrigin("www.domain.com" /* request host */, 80, "/", false); Assert.assertTrue(cookiespec.match(cookie, origin3)); }
/** * test cookie {@code Port} matching. */ @Test public void testMatchPort() throws Exception { // cookie can be sent to any port if port attribute not specified BasicClientCookie2 cookie = new BasicClientCookie2("name", "value"); cookie.setDomain(".domain.com"); cookie.setPath("/"); cookie.setPorts(null); final CookieSpec cookiespec = new RFC2965Spec(); final CookieOrigin origin1 = new CookieOrigin("www.domain.com", 8080 /* request port */, "/", false); Assert.assertTrue(cookiespec.match(cookie, origin1)); final CookieOrigin origin2 = new CookieOrigin("www.domain.com", 323 /* request port */, "/", false); Assert.assertTrue(cookiespec.match(cookie, origin2)); // otherwise, request port must be in cookie's port list cookie = new BasicClientCookie2("name", "value"); cookie.setDomain(".domain.com"); cookie.setPath("/"); cookie.setPorts(new int[] {80, 8080}); cookie.setAttribute(ClientCookie.PORT_ATTR, "80, 8080"); final CookieOrigin origin3 = new CookieOrigin("www.domain.com", 434 /* request port */, "/", false); Assert.assertFalse(cookiespec.match(cookie, origin3)); final CookieOrigin origin4 = new CookieOrigin("www.domain.com", 8080 /* request port */, "/", false); Assert.assertTrue(cookiespec.match(cookie, origin4)); }
/** * Tests if cookie constructor rejects cookie name starting with $. */ @Test public void testCookieNameStartingWithDollarSign() throws Exception { final Header setcookie = new BasicHeader("Set-Cookie", "$invalid_name="); final CookieSpec cookiespec = new RFC2109Spec(); final CookieOrigin origin = new CookieOrigin("127.0.0.1", 80, "/", false); try { final List<Cookie> cookies = cookiespec.parse(setcookie, origin); for (int i = 0; i < cookies.size(); i++) { cookiespec.validate(cookies.get(i), origin); } Assert.fail("MalformedCookieException exception should have been thrown"); } catch (final MalformedCookieException e) { // expected } }
@Test public void testNetscapeCookieParsing() throws Exception { final CookieSpec cookiespec = new DefaultCookieSpec(); final CookieOrigin origin = new CookieOrigin("myhost.mydomain.com", 80, "/", false); Header header = new BasicHeader("Set-Cookie", "name=value; path=/; domain=.mydomain.com; expires=Thu, 01-Jan-2070 00:00:10 GMT; comment=no_comment"); List<Cookie> cookies = cookiespec.parse(header, origin); cookiespec.validate(cookies.get(0), origin); Assert.assertEquals(1, cookies.size()); header = new BasicHeader("Set-Cookie", "name=value; path=/; domain=.mydomain.com; expires=Thu, 01-Jan-2070 00:00:10 GMT; version=1"); cookies = cookiespec.parse(header, origin); cookiespec.validate(cookies.get(0), origin); Assert.assertEquals(1, cookies.size()); }
/** * Host minus domain may not contain any dots */ @Test public void testParseWithIllegalDomain4() throws Exception { final Header header = new BasicHeader("Set-Cookie", "cookie-name=cookie-value; domain=.c.com; version=1"); final CookieSpec cookiespec = new RFC2109Spec(); final CookieOrigin origin = new CookieOrigin("a.b.c.com", 80, "/", false); try { final 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 (final MalformedCookieException e) { // expected } }
@Test public void testParseWithWrongPath() throws Exception { final Header header = new BasicHeader("Set-Cookie", "cookie-name=cookie-value; domain=127.0.0.1; path=/not/just/root"); final CookieSpec cookiespec = new RFC2109Spec(); final CookieOrigin origin = new CookieOrigin("127.0.0.1", 80, "/", false); try { final 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 (final MalformedCookieException e) { // expected } }
@Test public void testCookieStandardCompliantMatch() throws Exception { final CookieSpec cookiespec = new DefaultCookieSpec(); final CookieOrigin origin = new CookieOrigin("a.b.domain.com", 80, "/", false); // Make sure the strict (RFC2965) cookie matching // is used for version 1 cookies final BasicClientCookie2 cookie = new BasicClientCookie2("name", "value"); cookie.setVersion(1); cookie.setDomain(".domain.com"); cookie.setAttribute(ClientCookie.DOMAIN_ATTR, cookie.getDomain()); cookie.setPath("/"); cookie.setAttribute(ClientCookie.PATH_ATTR, cookie.getPath()); Assert.assertFalse(cookiespec.match(cookie, origin)); cookie.setDomain(".b.domain.com"); Assert.assertTrue(cookiespec.match(cookie, origin)); }
@Test public void testVersion1CookieWithInvalidExpires() throws Exception { final CookieSpec cookiespec = new DefaultCookieSpec(); final CookieOrigin origin = new CookieOrigin("myhost.mydomain.com", 80, "/", false); final Header origHeader = new BasicHeader("Set-Cookie", "test=\"test\"; Version=1; Expires=Mon, 11-Feb-2013 10:39:19 GMT; Path=/"); final List<Cookie> cookies = cookiespec.parse(origHeader, origin); Assert.assertNotNull(cookies); Assert.assertEquals(1, cookies.size()); final List<Header> headers = cookiespec.formatCookies(cookies); Assert.assertNotNull(headers); Assert.assertEquals(1, headers.size()); final Header header1 = headers.get(0); Assert.assertEquals("test=\"test\"", header1.getValue()); }
/** * Test domain equals host */ @Test public void testCookiesomainEqualsHost() throws Exception { final Header header = new BasicHeader("Set-Cookie", "cookie-name=cookie-value; domain=www.b.com; version=1"); final CookieSpec cookiespec = new RFC2109Spec(); final CookieOrigin origin = new CookieOrigin("www.b.com", 80, "/", false); final 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("www.b.com", cookies.get(0).getDomain()); }
/** * Domain does not start with a dot */ @Test public void testParseWithIllegalDomain1() throws Exception { final Header header = new BasicHeader("Set-Cookie", "cookie-name=cookie-value; domain=a.b.com; version=1"); final CookieSpec cookiespec = new RFC2109Spec(); final CookieOrigin origin = new CookieOrigin("www.a.b.com", 80, "/", false); try { final 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 (final MalformedCookieException e) { // expected } }