@Test public void testBasicDomainValidate2() throws Exception { final BasicClientCookie cookie = new BasicClientCookie("name", "value"); final CookieOrigin origin = new CookieOrigin("somehost", 80, "/", false); final CookieAttributeHandler h = new BasicDomainHandler(); cookie.setDomain("somehost"); h.validate(cookie, origin); cookie.setDomain("otherhost"); try { h.validate(cookie, origin); Assert.fail("MalformedCookieException should have been thrown"); } catch (final MalformedCookieException ex) { // expected } }
@Test public void testBasicDomainMatch2() throws Exception { final BasicClientCookie cookie = new BasicClientCookie("name", "value"); final CookieOrigin origin = new CookieOrigin("www.somedomain.com", 80, "/", false); final CookieAttributeHandler h = new BasicDomainHandler(); cookie.setDomain("somedomain.com"); cookie.setAttribute(ClientCookie.DOMAIN_ATTR, "somedomain.com"); Assert.assertTrue(h.match(cookie, origin)); cookie.setDomain(".somedomain.com"); Assert.assertTrue(h.match(cookie, origin)); cookie.setDomain(null); Assert.assertFalse(h.match(cookie, origin)); }
@Test public void testBasicSecureMatch() throws Exception { final BasicClientCookie cookie = new BasicClientCookie("name", "value"); final CookieAttributeHandler h = new BasicSecureHandler(); final CookieOrigin origin1 = new CookieOrigin("somehost", 80, "/stuff", false); cookie.setSecure(false); Assert.assertTrue(h.match(cookie, origin1)); cookie.setSecure(true); Assert.assertFalse(h.match(cookie, origin1)); final CookieOrigin origin2 = new CookieOrigin("somehost", 80, "/stuff", true); cookie.setSecure(false); Assert.assertTrue(h.match(cookie, origin2)); cookie.setSecure(true); Assert.assertTrue(h.match(cookie, origin2)); }
@Test public void testParseExpiry() throws Exception { final BasicClientCookie cookie = new BasicClientCookie("name", "value"); final CookieAttributeHandler h = new LaxExpiresHandler(); h.parse(cookie, "1:0:12 8-jan-2012"); final Date expiryDate = cookie.getExpiryDate(); Assert.assertNotNull(expiryDate); final Calendar c = Calendar.getInstance(); c.setTimeZone(LaxExpiresHandler.UTC); c.setTime(expiryDate); Assert.assertEquals(2012, c.get(Calendar.YEAR)); Assert.assertEquals(Calendar.JANUARY, c.get(Calendar.MONTH)); Assert.assertEquals(8, c.get(Calendar.DAY_OF_MONTH)); Assert.assertEquals(1, c.get(Calendar.HOUR_OF_DAY)); Assert.assertEquals(0, c.get(Calendar.MINUTE)); Assert.assertEquals(12, c.get(Calendar.SECOND)); Assert.assertEquals(0, c.get(Calendar.MILLISECOND)); }
@Test public void testParseExpiryFunnyTime() throws Exception { final BasicClientCookie cookie = new BasicClientCookie("name", "value"); final CookieAttributeHandler h = new LaxExpiresHandler(); h.parse(cookie, "1:59:00blah; 8-feb-2000"); final Date expiryDate = cookie.getExpiryDate(); Assert.assertNotNull(expiryDate); final Calendar c = Calendar.getInstance(); c.setTimeZone(LaxExpiresHandler.UTC); c.setTime(expiryDate); Assert.assertEquals(2000, c.get(Calendar.YEAR)); Assert.assertEquals(Calendar.FEBRUARY, c.get(Calendar.MONTH)); Assert.assertEquals(8, c.get(Calendar.DAY_OF_MONTH)); Assert.assertEquals(1, c.get(Calendar.HOUR_OF_DAY)); Assert.assertEquals(59, c.get(Calendar.MINUTE)); Assert.assertEquals(0, c.get(Calendar.SECOND)); Assert.assertEquals(0, c.get(Calendar.MILLISECOND)); }
@Test public void testParseExpiryFunnyDayOfMonth() throws Exception { final BasicClientCookie cookie = new BasicClientCookie("name", "value"); final CookieAttributeHandler h = new LaxExpiresHandler(); h.parse(cookie, "12:00:00 8blah;mar;1880"); final Date expiryDate = cookie.getExpiryDate(); Assert.assertNotNull(expiryDate); final Calendar c = Calendar.getInstance(); c.setTimeZone(LaxExpiresHandler.UTC); c.setTime(expiryDate); Assert.assertEquals(1880, c.get(Calendar.YEAR)); Assert.assertEquals(Calendar.MARCH, c.get(Calendar.MONTH)); Assert.assertEquals(8, c.get(Calendar.DAY_OF_MONTH)); Assert.assertEquals(12, c.get(Calendar.HOUR_OF_DAY)); Assert.assertEquals(0, c.get(Calendar.MINUTE)); Assert.assertEquals(0, c.get(Calendar.SECOND)); Assert.assertEquals(0, c.get(Calendar.MILLISECOND)); }
@Test public void testParseExpiryFunnyMonth() throws Exception { final BasicClientCookie cookie = new BasicClientCookie("name", "value"); final CookieAttributeHandler h = new LaxExpiresHandler(); h.parse(cookie, "23:59:59; 1-ApriLLLLL-2008"); final Date expiryDate = cookie.getExpiryDate(); Assert.assertNotNull(expiryDate); final Calendar c = Calendar.getInstance(); c.setTimeZone(LaxExpiresHandler.UTC); c.setTime(expiryDate); Assert.assertEquals(2008, c.get(Calendar.YEAR)); Assert.assertEquals(Calendar.APRIL, c.get(Calendar.MONTH)); Assert.assertEquals(1, c.get(Calendar.DAY_OF_MONTH)); Assert.assertEquals(23, c.get(Calendar.HOUR_OF_DAY)); Assert.assertEquals(59, c.get(Calendar.MINUTE)); Assert.assertEquals(59, c.get(Calendar.SECOND)); Assert.assertEquals(0, c.get(Calendar.MILLISECOND)); }
@Test public void testParseExpiryFunnyYear() throws Exception { final BasicClientCookie cookie = new BasicClientCookie("name", "value"); final CookieAttributeHandler h = new LaxExpiresHandler(); h.parse(cookie, "23:59:59; 1-Apr-2008blah"); final Date expiryDate = cookie.getExpiryDate(); Assert.assertNotNull(expiryDate); final Calendar c = Calendar.getInstance(); c.setTimeZone(LaxExpiresHandler.UTC); c.setTime(expiryDate); Assert.assertEquals(2008, c.get(Calendar.YEAR)); Assert.assertEquals(Calendar.APRIL, c.get(Calendar.MONTH)); Assert.assertEquals(1, c.get(Calendar.DAY_OF_MONTH)); Assert.assertEquals(23, c.get(Calendar.HOUR_OF_DAY)); Assert.assertEquals(59, c.get(Calendar.MINUTE)); Assert.assertEquals(59, c.get(Calendar.SECOND)); Assert.assertEquals(0, c.get(Calendar.MILLISECOND)); }
@Test public void testRFC2109DomainValidate3() throws Exception { final BasicClientCookie cookie = new BasicClientCookie("name", "value"); final CookieOrigin origin = new CookieOrigin("www.a.com", 80, "/", false); final CookieAttributeHandler h = new RFC2109DomainHandler(); cookie.setDomain(".a.com"); h.validate(cookie, origin); cookie.setDomain(".com"); try { h.validate(cookie, origin); Assert.fail("MalformedCookieException should have been thrown"); } catch (final MalformedCookieException ex) { // expected } }
@Test public void testRFC2109VersionValidate() throws Exception { final BasicClientCookie cookie = new BasicClientCookie("name", "value"); final CookieOrigin origin = new CookieOrigin("somedomain.com", 80, "/", false); final CookieAttributeHandler h = new RFC2109VersionHandler(); cookie.setVersion(12); h.validate(cookie, origin); cookie.setVersion(-12); try { h.validate(cookie, origin); Assert.fail("MalformedCookieException must have been thrown"); } catch (final MalformedCookieException ex) { // expected } }
@Test public void testNetscapeDomainValidate1() throws Exception { final BasicClientCookie cookie = new BasicClientCookie("name", "value"); final CookieOrigin origin = new CookieOrigin("somehost", 80, "/", false); final CookieAttributeHandler h = new NetscapeDomainHandler(); cookie.setDomain("somehost"); h.validate(cookie, origin); cookie.setDomain("otherhost"); try { h.validate(cookie, origin); Assert.fail("MalformedCookieException should have been thrown"); } catch (final MalformedCookieException ex) { // expected } }
@Test public void testNetscapeDomainValidate3() throws Exception { final BasicClientCookie cookie = new BasicClientCookie("name", "value"); final CookieOrigin origin = new CookieOrigin("www.a.com", 80, "/", false); final CookieAttributeHandler h = new NetscapeDomainHandler(); cookie.setDomain(".a.com"); h.validate(cookie, origin); cookie.setDomain(".com"); try { h.validate(cookie, origin); Assert.fail("MalformedCookieException should have been thrown"); } catch (final MalformedCookieException ex) { // expected } }
@Test public void testNetscapeDomainValidate4() throws Exception { final BasicClientCookie cookie = new BasicClientCookie("name", "value"); final CookieOrigin origin = new CookieOrigin("www.a.b.c", 80, "/", false); final CookieAttributeHandler h = new NetscapeDomainHandler(); cookie.setDomain(".a.b.c"); h.validate(cookie, origin); cookie.setDomain(".b.c"); try { h.validate(cookie, origin); Assert.fail("MalformedCookieException should have been thrown"); } catch (final MalformedCookieException ex) { // expected } }
protected List<Cookie> parse(final HeaderElement[] elems, final CookieOrigin origin) throws MalformedCookieException { List<Cookie> cookies = new ArrayList<Cookie>(elems.length); for (HeaderElement headerelement : elems) { String name = headerelement.getName(); String value = headerelement.getValue(); if (name == null || name.length() == 0) { throw new MalformedCookieException("Cookie name may not be empty"); } BasicClientCookie cookie = new BasicClientCookie(name, value); cookie.setPath(getDefaultPath(origin)); cookie.setDomain(getDefaultDomain(origin)); // cycle through the parameters NameValuePair[] attribs = headerelement.getParameters(); for (int j = attribs.length - 1; j >= 0; j--) { NameValuePair attrib = attribs[j]; String s = attrib.getName().toLowerCase(Locale.ENGLISH); cookie.setAttribute(s, attrib.getValue()); CookieAttributeHandler handler = findAttribHandler(s); if (handler != null) { handler.parse(cookie, attrib.getValue()); } } cookies.add(cookie); } return cookies; }
public void validate(final Cookie cookie, final CookieOrigin origin) throws MalformedCookieException { if (cookie == null) { throw new IllegalArgumentException("Cookie may not be null"); } if (origin == null) { throw new IllegalArgumentException("Cookie origin may not be null"); } for (CookieAttributeHandler handler: getAttribHandlers()) { handler.validate(cookie, origin); } }
public boolean match(final Cookie cookie, final CookieOrigin origin) { if (cookie == null) { throw new IllegalArgumentException("Cookie may not be null"); } if (origin == null) { throw new IllegalArgumentException("Cookie origin may not be null"); } for (CookieAttributeHandler handler: getAttribHandlers()) { if (!handler.match(cookie, origin)) { return false; } } return true; }
public void registerAttribHandler( final String name, final CookieAttributeHandler handler) { if (name == null) { throw new IllegalArgumentException("Attribute name may not be null"); } if (handler == null) { throw new IllegalArgumentException("Attribute handler may not be null"); } this.attribHandlerMap.put(name, handler); }
/** * Gets attribute handler {@link CookieAttributeHandler} for the * given attribute. * * @param name attribute name. e.g. Domain, Path, etc. * @throws IllegalStateException if handler not found for the * specified attribute. */ protected CookieAttributeHandler getAttribHandler(final String name) { CookieAttributeHandler handler = findAttribHandler(name); if (handler == null) { throw new IllegalStateException("Handler not registered for " + name + " attribute."); } else { return handler; } }
/** * Gets attribute handler {@link CookieAttributeHandler} for the * given attribute. * * @param name attribute name. e.g. Domain, Path, etc. * @throws IllegalStateException if handler not found for the * specified attribute. */ protected CookieAttributeHandler getAttribHandler(final String name) { final CookieAttributeHandler handler = findAttribHandler(name); if (handler == null) { throw new IllegalStateException("Handler not registered for " + name + " attribute."); } else { return handler; } }
protected List<Cookie> parse(final HeaderElement[] elems, final CookieOrigin origin) throws MalformedCookieException { final List<Cookie> cookies = new ArrayList<Cookie>(elems.length); for (final HeaderElement headerelement : elems) { final String name = headerelement.getName(); final String value = headerelement.getValue(); if (name == null || name.length() == 0) { throw new MalformedCookieException("Cookie name may not be empty"); } final BasicClientCookieHC4 cookie = new BasicClientCookieHC4(name, value); cookie.setPath(getDefaultPath(origin)); cookie.setDomain(getDefaultDomain(origin)); // cycle through the parameters final NameValuePair[] attribs = headerelement.getParameters(); for (int j = attribs.length - 1; j >= 0; j--) { final NameValuePair attrib = attribs[j]; final String s = attrib.getName().toLowerCase(Locale.ENGLISH); cookie.setAttribute(s, attrib.getValue()); final CookieAttributeHandler handler = findAttribHandler(s); if (handler != null) { handler.parse(cookie, attrib.getValue()); } } cookies.add(cookie); } return cookies; }
public void validate(final Cookie cookie, final CookieOrigin origin) throws MalformedCookieException { Args.notNull(cookie, "Cookie"); Args.notNull(origin, "Cookie origin"); for (final CookieAttributeHandler handler: getAttribHandlers()) { handler.validate(cookie, origin); } }
public boolean match(final Cookie cookie, final CookieOrigin origin) { Args.notNull(cookie, "Cookie"); Args.notNull(origin, "Cookie origin"); for (final CookieAttributeHandler handler: getAttribHandlers()) { if (!handler.match(cookie, origin)) { return false; } } return true; }
protected RFC6265CookieSpec(final CommonCookieAttributeHandler... handlers) { super(); this.attribHandlers = handlers.clone(); this.attribHandlerMap = new ConcurrentHashMap<String, CookieAttributeHandler>(handlers.length); for (final CommonCookieAttributeHandler handler: handlers) { this.attribHandlerMap.put(handler.getAttributeName().toLowerCase(Locale.ROOT), handler); } this.tokenParser = TokenParser.INSTANCE; }
@Override public final void validate(final Cookie cookie, final CookieOrigin origin) throws MalformedCookieException { Args.notNull(cookie, "Cookie"); Args.notNull(origin, "Cookie origin"); for (final CookieAttributeHandler handler: this.attribHandlers) { handler.validate(cookie, origin); } }
@Override public final boolean match(final Cookie cookie, final CookieOrigin origin) { Args.notNull(cookie, "Cookie"); Args.notNull(origin, "Cookie origin"); for (final CookieAttributeHandler handler: this.attribHandlers) { if (!handler.match(cookie, origin)) { return false; } } return true; }
protected List<Cookie> parse(final HeaderElement[] elems, final CookieOrigin origin) throws MalformedCookieException { final List<Cookie> cookies = new ArrayList<Cookie>(elems.length); for (final HeaderElement headerelement : elems) { final String name = headerelement.getName(); final String value = headerelement.getValue(); if (name == null || name.isEmpty()) { continue; } final BasicClientCookie cookie = new BasicClientCookie(name, value); cookie.setPath(getDefaultPath(origin)); cookie.setDomain(getDefaultDomain(origin)); // cycle through the parameters final NameValuePair[] attribs = headerelement.getParameters(); for (int j = attribs.length - 1; j >= 0; j--) { final NameValuePair attrib = attribs[j]; final String s = attrib.getName().toLowerCase(Locale.ROOT); cookie.setAttribute(s, attrib.getValue()); final CookieAttributeHandler handler = findAttribHandler(s); if (handler != null) { handler.parse(cookie, attrib.getValue()); } } cookies.add(cookie); } return cookies; }
@Override public void validate(final Cookie cookie, final CookieOrigin origin) throws MalformedCookieException { Args.notNull(cookie, "Cookie"); Args.notNull(origin, "Cookie origin"); for (final CookieAttributeHandler handler: getAttribHandlers()) { handler.validate(cookie, origin); } }
@Override public boolean match(final Cookie cookie, final CookieOrigin origin) { Args.notNull(cookie, "Cookie"); Args.notNull(origin, "Cookie origin"); for (final CookieAttributeHandler handler: getAttribHandlers()) { if (!handler.match(cookie, origin)) { return false; } } return true; }
/** * @since 4.4 */ protected AbstractCookieSpec(final CommonCookieAttributeHandler... handlers) { super(); this.attribHandlerMap = new ConcurrentHashMap<String, CookieAttributeHandler>(handlers.length); for (final CommonCookieAttributeHandler handler: handlers) { this.attribHandlerMap.put(handler.getAttributeName(), handler); } }
/** * @deprecated (4.4) use {@link #AbstractCookieSpec(java.util.HashMap)} or * {@link #AbstractCookieSpec(org.apache.http.cookie.CommonCookieAttributeHandler...)} * constructors instead. */ @Deprecated public void registerAttribHandler( final String name, final CookieAttributeHandler handler) { Args.notNull(name, "Attribute name"); Args.notNull(handler, "Attribute handler"); this.attribHandlerMap.put(name, handler); }
@Test public void testBasicDomainParse() throws Exception { final BasicClientCookie cookie = new BasicClientCookie("name", "value"); final CookieAttributeHandler h = new BasicDomainHandler(); h.parse(cookie, "www.somedomain.com"); Assert.assertEquals("www.somedomain.com", cookie.getDomain()); }
@Test public void testBasicDomainValidate3() throws Exception { final BasicClientCookie cookie = new BasicClientCookie("name", "value"); final CookieOrigin origin = new CookieOrigin("somedomain.com", 80, "/", false); final CookieAttributeHandler h = new BasicDomainHandler(); cookie.setDomain(".somedomain.com"); h.validate(cookie, origin); }
@Test public void testBasicDomainValidate4() throws Exception { final BasicClientCookie cookie = new BasicClientCookie("name", "value"); final CookieOrigin origin = new CookieOrigin("somedomain.com", 80, "/", false); final CookieAttributeHandler h = new BasicDomainHandler(); cookie.setDomain(null); try { h.validate(cookie, origin); Assert.fail("MalformedCookieException should have been thrown"); } catch (final MalformedCookieException ex) { // expected } }
@Test public void testBasicDomainMatch1() throws Exception { final BasicClientCookie cookie = new BasicClientCookie("name", "value"); final CookieOrigin origin = new CookieOrigin("somedomain.com", 80, "/", false); final CookieAttributeHandler h = new BasicDomainHandler(); cookie.setDomain("somedomain.com"); cookie.setAttribute(ClientCookie.DOMAIN_ATTR, "somedomain.com"); Assert.assertTrue(h.match(cookie, origin)); cookie.setDomain(".somedomain.com"); Assert.assertTrue(h.match(cookie, origin)); }
@Test public void testBasicDomainMatchOneLetterPrefix() throws Exception { final BasicClientCookie cookie = new BasicClientCookie("name", "value"); final CookieOrigin origin = new CookieOrigin("a.somedomain.com", 80, "/", false); final CookieAttributeHandler h = new BasicDomainHandler(); cookie.setDomain("somedomain.com"); cookie.setAttribute(ClientCookie.DOMAIN_ATTR, "somedomain.com"); Assert.assertTrue(h.match(cookie, origin)); }
@Test public void testBasicDomainMatchMixedCase() throws Exception { final BasicClientCookie cookie = new BasicClientCookie("name", "value"); final CookieOrigin origin = new CookieOrigin("a.SomeDomain.com", 80, "/", false); final CookieAttributeHandler h = new BasicDomainHandler(); cookie.setDomain("somedoMain.Com"); cookie.setAttribute(ClientCookie.DOMAIN_ATTR, "somedoMain.Com"); Assert.assertTrue(h.match(cookie, origin)); }
@Test public void testBasicPathParse() throws Exception { final BasicClientCookie cookie = new BasicClientCookie("name", "value"); final CookieAttributeHandler h = new BasicPathHandler(); h.parse(cookie, "stuff"); Assert.assertEquals("stuff", cookie.getPath()); h.parse(cookie, ""); Assert.assertEquals("/", cookie.getPath()); h.parse(cookie, null); Assert.assertEquals("/", cookie.getPath()); }
@Test public void testBasicPathMatch1() throws Exception { final BasicClientCookie cookie = new BasicClientCookie("name", "value"); final CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuff", false); final CookieAttributeHandler h = new BasicPathHandler(); cookie.setPath("/stuff"); Assert.assertTrue(h.match(cookie, origin)); }
@Test public void testBasicPathMatch2() throws Exception { final BasicClientCookie cookie = new BasicClientCookie("name", "value"); final CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuff/", false); final CookieAttributeHandler h = new BasicPathHandler(); cookie.setPath("/stuff"); Assert.assertTrue(h.match(cookie, origin)); }
@Test public void testBasicPathMatch3() throws Exception { final BasicClientCookie cookie = new BasicClientCookie("name", "value"); final CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuff/more-stuff", false); final CookieAttributeHandler h = new BasicPathHandler(); cookie.setPath("/stuff"); Assert.assertTrue(h.match(cookie, origin)); }