You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hc.apache.org by ol...@apache.org on 2006/05/25 15:30:49 UTC
svn commit: r409386 - in /jakarta/httpcomponents/httpclient/trunk/src:
java/org/apache/http/cookie/ java/org/apache/http/cookie/impl/
test/org/apache/http/cookie/impl/
Author: olegk
Date: Thu May 25 06:30:49 2006
New Revision: 409386
URL: http://svn.apache.org/viewvc?rev=409386&view=rev
Log:
Added cookie handler impls for 'path', 'maxage', 'secure', and 'comment' attributes shared by all common cookie specs
Added:
jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/AbstractCookieAttributeHandler.java (with props)
jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicCommentHandler.java (with props)
jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicMaxAgeHandler.java (with props)
jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicPathHandler.java (with props)
jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicSecureHandler.java (with props)
jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestBasicCookieAttribHandlers.java (with props)
Modified:
jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/Cookie.java
jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestAbstractCookieSpec.java
jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestAllCookieImpl.java
Modified: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/Cookie.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/Cookie.java?rev=409386&r1=409385&r2=409386&view=diff
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/Cookie.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/Cookie.java Thu May 25 06:30:49 2006
@@ -186,7 +186,7 @@
* @return <code>true</code> if this cookie should only be sent over secure connections.
* @see #setSecure(boolean)
*/
- public boolean getSecure() {
+ public boolean isSecure() {
return isSecure;
}
@@ -200,7 +200,7 @@
*
* @param secure The value of the secure attribute
*
- * @see #getSecure()
+ * @see #isSecure()
*/
public void setSecure (boolean secure) {
isSecure = secure;
@@ -240,19 +240,6 @@
return (cookieExpiryDate != null
&& cookieExpiryDate.getTime() <= System.currentTimeMillis());
}
-
- /**
- * Returns true if this cookie has expired according to the time passed in.
- *
- * @param now The current time.
- *
- * @return <tt>true</tt> if the cookie expired.
- */
- public boolean isExpired(Date now) {
- return (cookieExpiryDate != null
- && cookieExpiryDate.getTime() <= now.getTime());
- }
-
/**
* Indicates whether the cookie had a path specified in a
Added: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/AbstractCookieAttributeHandler.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/AbstractCookieAttributeHandler.java?rev=409386&view=auto
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/AbstractCookieAttributeHandler.java (added)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/AbstractCookieAttributeHandler.java Thu May 25 06:30:49 2006
@@ -0,0 +1,48 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ * Copyright 2002-2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.http.cookie.impl;
+
+import org.apache.http.cookie.Cookie;
+import org.apache.http.cookie.CookieAttributeHandler;
+import org.apache.http.cookie.CookieOrigin;
+import org.apache.http.cookie.MalformedCookieException;
+
+public abstract class AbstractCookieAttributeHandler implements CookieAttributeHandler {
+
+ public void validate(final Cookie cookie, final CookieOrigin origin)
+ throws MalformedCookieException {
+ // Do nothing
+ }
+
+ public boolean match(final Cookie cookie, final CookieOrigin origin) {
+ // Always match
+ return true;
+ }
+
+}
\ No newline at end of file
Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/AbstractCookieAttributeHandler.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/AbstractCookieAttributeHandler.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/AbstractCookieAttributeHandler.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicCommentHandler.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicCommentHandler.java?rev=409386&view=auto
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicCommentHandler.java (added)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicCommentHandler.java Thu May 25 06:30:49 2006
@@ -0,0 +1,48 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ * Copyright 2002-2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.http.cookie.impl;
+
+import org.apache.http.cookie.Cookie;
+import org.apache.http.cookie.MalformedCookieException;
+
+public class BasicCommentHandler extends AbstractCookieAttributeHandler {
+
+ public BasicCommentHandler() {
+ super();
+ }
+
+ public void parse(final Cookie cookie, final String value)
+ throws MalformedCookieException {
+ if (cookie == null) {
+ throw new IllegalArgumentException("Cookie may not be null");
+ }
+ cookie.setComment(value);
+ }
+
+}
\ No newline at end of file
Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicCommentHandler.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicCommentHandler.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicCommentHandler.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicMaxAgeHandler.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicMaxAgeHandler.java?rev=409386&view=auto
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicMaxAgeHandler.java (added)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicMaxAgeHandler.java Thu May 25 06:30:49 2006
@@ -0,0 +1,60 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ * Copyright 2002-2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.http.cookie.impl;
+
+import java.util.Date;
+
+import org.apache.http.cookie.Cookie;
+import org.apache.http.cookie.MalformedCookieException;
+
+public class BasicMaxAgeHandler extends AbstractCookieAttributeHandler {
+
+ public BasicMaxAgeHandler() {
+ super();
+ }
+
+ public void parse(final Cookie cookie, final String value)
+ throws MalformedCookieException {
+ if (cookie == null) {
+ throw new IllegalArgumentException("Cookie may not be null");
+ }
+ if (value == null) {
+ throw new MalformedCookieException("Missing value for max-age attribute");
+ }
+ int age;
+ try {
+ age = Integer.parseInt(value);
+ } catch (NumberFormatException e) {
+ throw new MalformedCookieException ("Invalid max-age attribute: "
+ + value);
+ }
+ cookie.setExpiryDate(new Date(System.currentTimeMillis() + age * 1000L));
+ }
+
+}
\ No newline at end of file
Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicMaxAgeHandler.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicMaxAgeHandler.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicMaxAgeHandler.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicPathHandler.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicPathHandler.java?rev=409386&view=auto
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicPathHandler.java (added)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicPathHandler.java Thu May 25 06:30:49 2006
@@ -0,0 +1,89 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ * Copyright 2002-2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.http.cookie.impl;
+
+import org.apache.http.cookie.Cookie;
+import org.apache.http.cookie.CookieAttributeHandler;
+import org.apache.http.cookie.CookieOrigin;
+import org.apache.http.cookie.MalformedCookieException;
+
+public class BasicPathHandler implements CookieAttributeHandler {
+
+ public BasicPathHandler() {
+ super();
+ }
+
+ public void parse(final Cookie cookie, String value)
+ throws MalformedCookieException {
+ if (cookie == null) {
+ throw new IllegalArgumentException("Cookie may not be null");
+ }
+ if (value == null || value.trim().equals("")) {
+ value = "/";
+ }
+ cookie.setPath(value);
+ cookie.setPathAttributeSpecified(true);
+ }
+
+ public void validate(final Cookie cookie, final CookieOrigin origin)
+ throws MalformedCookieException {
+ if (!match(cookie, origin)) {
+ throw new MalformedCookieException(
+ "Illegal path attribute \"" + cookie.getPath()
+ + "\". Path of origin: \"" + origin.getPath() + "\"");
+ }
+ }
+
+ 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");
+ }
+ String targetpath = origin.getPath();
+ String topmostPath = cookie.getPath();
+ if (topmostPath == null) {
+ topmostPath = "/";
+ }
+ if (topmostPath.length() > 1 && topmostPath.endsWith("/")) {
+ topmostPath = topmostPath.substring(0, topmostPath.length() - 1);
+ }
+ boolean match = targetpath.startsWith (topmostPath);
+ // if there is a match and these values are not exactly the same we have
+ // to make sure we're not matcing "/foobar" and "/foo"
+ if (match && targetpath.length() != topmostPath.length()) {
+ if (!topmostPath.endsWith("/")) {
+ match = (targetpath.charAt(topmostPath.length()) == '/');
+ }
+ }
+ return match;
+ }
+
+}
\ No newline at end of file
Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicPathHandler.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicPathHandler.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicPathHandler.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicSecureHandler.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicSecureHandler.java?rev=409386&view=auto
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicSecureHandler.java (added)
+++ jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicSecureHandler.java Thu May 25 06:30:49 2006
@@ -0,0 +1,59 @@
+/*
+ * $HeadURL$
+ * $Revision$
+ * $Date$
+ *
+ * ====================================================================
+ *
+ * Copyright 2002-2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+package org.apache.http.cookie.impl;
+
+import org.apache.http.cookie.Cookie;
+import org.apache.http.cookie.CookieOrigin;
+import org.apache.http.cookie.MalformedCookieException;
+
+public class BasicSecureHandler extends AbstractCookieAttributeHandler {
+
+ public BasicSecureHandler() {
+ super();
+ }
+
+ public void parse(final Cookie cookie, final String value)
+ throws MalformedCookieException {
+ if (cookie == null) {
+ throw new IllegalArgumentException("Cookie may not be null");
+ }
+ cookie.setSecure(true);
+ }
+
+ 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");
+ }
+ return cookie.isSecure() ? origin.isSecure() : true;
+ }
+
+}
\ No newline at end of file
Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicSecureHandler.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicSecureHandler.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/httpcomponents/httpclient/trunk/src/java/org/apache/http/cookie/impl/BasicSecureHandler.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestAbstractCookieSpec.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestAbstractCookieSpec.java?rev=409386&r1=409385&r2=409386&view=diff
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestAbstractCookieSpec.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestAbstractCookieSpec.java Thu May 25 06:30:49 2006
@@ -29,6 +29,7 @@
package org.apache.http.cookie.impl;
import java.io.IOException;
+import java.util.Iterator;
import org.apache.http.Header;
import org.apache.http.cookie.Cookie;
@@ -96,9 +97,18 @@
AbstractCookieSpec cookiespec = new DummyCookieSpec();
cookiespec.registerAttribHandler("this", h1);
cookiespec.registerAttribHandler("that", h2);
-
+ cookiespec.registerAttribHandler("thistoo", h1);
+ cookiespec.registerAttribHandler("thattoo", h2);
+
assertTrue(h1 == cookiespec.getAttribHandler("this"));
assertTrue(h2 == cookiespec.getAttribHandler("that"));
+ assertTrue(h1 == cookiespec.getAttribHandler("thistoo"));
+ assertTrue(h2 == cookiespec.getAttribHandler("thattoo"));
+
+ Iterator it = cookiespec.getAttribHandlerIterator();
+ assertNotNull(it.next());
+ assertNotNull(it.next());
+ assertFalse(it.hasNext());
}
public void testInvalidHandler() throws IOException {
@@ -114,6 +124,22 @@
cookiespec.getAttribHandler("whatever");
fail("IllegalStateException should have been thrown");
} catch (IllegalStateException ex) {
+ // expected
+ }
+ }
+
+ public void testBasicPathInvalidInput() throws Exception {
+ AbstractCookieSpec cookiespec = new DummyCookieSpec();
+ try {
+ cookiespec.registerAttribHandler(null, null);
+ fail("IllegalArgumentException must have been thrown");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ try {
+ cookiespec.registerAttribHandler("whatever", null);
+ fail("IllegalArgumentException must have been thrown");
+ } catch (IllegalArgumentException ex) {
// expected
}
}
Modified: jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestAllCookieImpl.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestAllCookieImpl.java?rev=409386&r1=409385&r2=409386&view=diff
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestAllCookieImpl.java (original)
+++ jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestAllCookieImpl.java Thu May 25 06:30:49 2006
@@ -39,6 +39,7 @@
public static Test suite() {
TestSuite suite = new TestSuite();
suite.addTest(TestAbstractCookieSpec.suite());
+ suite.addTest(TestBasicCookieAttribHandlers.suite());
return suite;
}
Added: jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestBasicCookieAttribHandlers.java
URL: http://svn.apache.org/viewvc/jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestBasicCookieAttribHandlers.java?rev=409386&view=auto
==============================================================================
--- jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestBasicCookieAttribHandlers.java (added)
+++ jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestBasicCookieAttribHandlers.java Thu May 25 06:30:49 2006
@@ -0,0 +1,263 @@
+/*
+ * $HeadURL$
+ * $Revisio$
+ * $Date$
+ * ====================================================================
+ *
+ * Copyright 2002-2004 The Apache Software Foundation
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ====================================================================
+ *
+ * This software consists of voluntary contributions made by many
+ * individuals on behalf of the Apache Software Foundation. For more
+ * information on the Apache Software Foundation, please see
+ * <http://www.apache.org/>.
+ *
+ */
+
+package org.apache.http.cookie.impl;
+
+import org.apache.http.cookie.Cookie;
+import org.apache.http.cookie.CookieAttributeHandler;
+import org.apache.http.cookie.CookieOrigin;
+import org.apache.http.cookie.MalformedCookieException;
+
+import junit.framework.Test;
+import junit.framework.TestCase;
+import junit.framework.TestSuite;
+
+public class TestBasicCookieAttribHandlers extends TestCase {
+
+ public TestBasicCookieAttribHandlers(String testName) {
+ super(testName);
+ }
+
+ public static Test suite() {
+ return new TestSuite(TestBasicCookieAttribHandlers.class);
+ }
+
+ // ------------------------------------------------------------------- Main
+ public static void main(String args[]) {
+ String[] testCaseName = { TestBasicCookieAttribHandlers.class.getName() };
+ junit.textui.TestRunner.main(testCaseName);
+ }
+
+ public void testBasicPathParse() throws Exception {
+ Cookie cookie = new Cookie("name", "value");
+ CookieAttributeHandler h = new BasicPathHandler();
+ h.parse(cookie, "stuff");
+ assertEquals("stuff", cookie.getPath());
+ assertTrue(cookie.isPathAttributeSpecified());
+ h.parse(cookie, "");
+ assertEquals("/", cookie.getPath());
+ assertTrue(cookie.isPathAttributeSpecified());
+ h.parse(cookie, null);
+ assertEquals("/", cookie.getPath());
+ assertTrue(cookie.isPathAttributeSpecified());
+ }
+
+ public void testBasicPathMatch1() throws Exception {
+ Cookie cookie = new Cookie("name", "value");
+ CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuff", false);
+ CookieAttributeHandler h = new BasicPathHandler();
+ cookie.setPath("/stuff");
+ assertTrue(h.match(cookie, origin));
+ }
+
+ public void testBasicPathMatch2() throws Exception {
+ Cookie cookie = new Cookie("name", "value");
+ CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuff/", false);
+ CookieAttributeHandler h = new BasicPathHandler();
+ cookie.setPath("/stuff");
+ assertTrue(h.match(cookie, origin));
+ }
+
+ public void testBasicPathMatch3() throws Exception {
+ Cookie cookie = new Cookie("name", "value");
+ CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuff/more-stuff", false);
+ CookieAttributeHandler h = new BasicPathHandler();
+ cookie.setPath("/stuff");
+ assertTrue(h.match(cookie, origin));
+ }
+
+ public void testBasicPathMatch4() throws Exception {
+ Cookie cookie = new Cookie("name", "value");
+ CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuffed", false);
+ CookieAttributeHandler h = new BasicPathHandler();
+ cookie.setPath("/stuff");
+ assertFalse(h.match(cookie, origin));
+ }
+
+ public void testBasicPathMatch5() throws Exception {
+ Cookie cookie = new Cookie("name", "value");
+ CookieOrigin origin = new CookieOrigin("somehost", 80, "/otherstuff", false);
+ CookieAttributeHandler h = new BasicPathHandler();
+ cookie.setPath("/stuff");
+ assertFalse(h.match(cookie, origin));
+ }
+
+ public void testBasicPathMatch6() throws Exception {
+ Cookie cookie = new Cookie("name", "value");
+ CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuff", false);
+ CookieAttributeHandler h = new BasicPathHandler();
+ cookie.setPath("/stuff/");
+ assertTrue(h.match(cookie, origin));
+ }
+
+ public void testBasicPathMatch7() throws Exception {
+ Cookie cookie = new Cookie("name", "value");
+ CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuff", false);
+ CookieAttributeHandler h = new BasicPathHandler();
+ assertTrue(h.match(cookie, origin));
+ }
+
+ public void testBasicPathValidate() throws Exception {
+ Cookie cookie = new Cookie("name", "value");
+ CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuff", false);
+ CookieAttributeHandler h = new BasicPathHandler();
+ cookie.setPath("/stuff");
+ h.validate(cookie, origin);
+ cookie.setPath("/stuffed");
+ try {
+ h.validate(cookie, origin);
+ fail("MalformedCookieException must have been thrown");
+ } catch (MalformedCookieException ex) {
+ // expected
+ }
+ }
+
+ public void testBasicPathInvalidInput() throws Exception {
+ CookieAttributeHandler h = new BasicPathHandler();
+ try {
+ h.parse(null, null);
+ fail("IllegalArgumentException must have been thrown");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ try {
+ h.match(null, null);
+ fail("IllegalArgumentException must have been thrown");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ try {
+ h.match(new Cookie("name", "value"), null);
+ fail("IllegalArgumentException must have been thrown");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ }
+
+ public void testBasicMaxAgeParse() throws Exception {
+ Cookie cookie = new Cookie("name", "value");
+ CookieAttributeHandler h = new BasicMaxAgeHandler();
+ h.parse(cookie, "2000");
+ assertNotNull(cookie.getExpiryDate());
+ }
+
+ public void testBasicMaxAgeParseInvalid() throws Exception {
+ Cookie cookie = new Cookie("name", "value");
+ CookieAttributeHandler h = new BasicMaxAgeHandler();
+ try {
+ h.parse(cookie, "garbage");
+ fail("MalformedCookieException must have been thrown");
+ } catch (MalformedCookieException ex) {
+ // expected
+ }
+ try {
+ h.parse(cookie, null);
+ fail("MalformedCookieException must have been thrown");
+ } catch (MalformedCookieException ex) {
+ // expected
+ }
+ }
+
+ public void testBasicMaxAgeInvalidInput() throws Exception {
+ CookieAttributeHandler h = new BasicMaxAgeHandler();
+ try {
+ h.parse(null, null);
+ fail("IllegalArgumentException must have been thrown");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ }
+
+ public void testBasicCommentParse() throws Exception {
+ Cookie cookie = new Cookie("name", "value");
+ CookieAttributeHandler h = new BasicCommentHandler();
+ h.parse(cookie, "whatever");
+ assertEquals("whatever", cookie.getComment());
+ h.parse(cookie, null);
+ assertEquals(null, cookie.getComment());
+ }
+
+ public void testBasicCommentInvalidInput() throws Exception {
+ CookieAttributeHandler h = new BasicCommentHandler();
+ try {
+ h.parse(null, null);
+ fail("IllegalArgumentException must have been thrown");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ }
+
+ public void testBasicSecureParse() throws Exception {
+ Cookie cookie = new Cookie("name", "value");
+ CookieAttributeHandler h = new BasicSecureHandler();
+ h.parse(cookie, "whatever");
+ assertTrue(cookie.isSecure());
+ h.parse(cookie, null);
+ assertTrue(cookie.isSecure());
+ }
+
+ public void testBasicSecureMatch() throws Exception {
+ Cookie cookie = new Cookie("name", "value");
+ CookieAttributeHandler h = new BasicSecureHandler();
+
+ CookieOrigin origin1 = new CookieOrigin("somehost", 80, "/stuff", false);
+ cookie.setSecure(false);
+ assertTrue(h.match(cookie, origin1));
+ cookie.setSecure(true);
+ assertFalse(h.match(cookie, origin1));
+
+ CookieOrigin origin2 = new CookieOrigin("somehost", 80, "/stuff", true);
+ cookie.setSecure(false);
+ assertTrue(h.match(cookie, origin2));
+ cookie.setSecure(true);
+ assertTrue(h.match(cookie, origin2));
+ }
+
+ public void testBasicSecureInvalidInput() throws Exception {
+ CookieAttributeHandler h = new BasicSecureHandler();
+ try {
+ h.parse(null, null);
+ fail("IllegalArgumentException must have been thrown");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ try {
+ h.match(null, null);
+ fail("IllegalArgumentException must have been thrown");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ try {
+ h.match(new Cookie("name", "value"), null);
+ fail("IllegalArgumentException must have been thrown");
+ } catch (IllegalArgumentException ex) {
+ // expected
+ }
+ }
+
+}
\ No newline at end of file
Propchange: jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestBasicCookieAttribHandlers.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestBasicCookieAttribHandlers.java
------------------------------------------------------------------------------
svn:keywords = Date Author Id Revision HeadURL
Propchange: jakarta/httpcomponents/httpclient/trunk/src/test/org/apache/http/cookie/impl/TestBasicCookieAttribHandlers.java
------------------------------------------------------------------------------
svn:mime-type = text/plain