You are viewing a plain text version of this content. The canonical link for it is here.
Posted to scm@geronimo.apache.org by ri...@apache.org on 2008/03/03 14:57:06 UTC
svn commit: r633086 - in /geronimo/sandbox/async-http-client-mina2/src:
main/java/org/apache/ahc/codec/ test/java/org/apache/ahc/
Author: rickmcguire
Date: Mon Mar 3 05:57:05 2008
New Revision: 633086
URL: http://svn.apache.org/viewvc?rev=633086&view=rev
Log:
GERONIMO-3888 AsyncHttpClient does not handle Set-Cookie directive with an empty value
Patch provided by Alex Antonov
Also checkin new files accidentally omitted in an earlier commit.
Added:
geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/CookiePolicy.java (with props)
geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/DefaultCookiePolicy.java (with props)
geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/NullCookiePolicy.java (with props)
geronimo/sandbox/async-http-client-mina2/src/test/java/org/apache/ahc/CookieTest.java (with props)
Modified:
geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpDecoder.java
geronimo/sandbox/async-http-client-mina2/src/test/java/org/apache/ahc/ResponseHeaderParsingTest.java
Added: geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/CookiePolicy.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/CookiePolicy.java?rev=633086&view=auto
==============================================================================
--- geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/CookiePolicy.java (added)
+++ geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/CookiePolicy.java Mon Mar 3 05:57:05 2008
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ *
+ */
+package org.apache.ahc.codec;
+
+import java.net.URL;
+
+/**
+ * The interface that defines whether the cookie can be associated with the
+ * request.
+ */
+public interface CookiePolicy {
+ /**
+ * Returns whether the cookie can be accepted for the given URL. Not all
+ * cookies that are accepted can be sent to the server; e.g. an expired
+ * cookie. However, one can expect all cookies suitable to be sent will be
+ * accepted.
+ */
+ boolean accept(Cookie cookie, URL url);
+
+ /**
+ * Returns whether the cookie should be sent for the given URL.
+ */
+ boolean matches(Cookie cookie, URL url);
+}
Propchange: geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/CookiePolicy.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/CookiePolicy.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/CookiePolicy.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/DefaultCookiePolicy.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/DefaultCookiePolicy.java?rev=633086&view=auto
==============================================================================
--- geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/DefaultCookiePolicy.java (added)
+++ geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/DefaultCookiePolicy.java Mon Mar 3 05:57:05 2008
@@ -0,0 +1,201 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ *
+ */
+package org.apache.ahc.codec;
+
+import java.net.URL;
+import java.util.Date;
+
+/**
+ * A CookiePolicy implementation that resembles a common browser behavior. It
+ * uses the domain, path, max-age (or expires), secure attributes, as well as
+ * the cookie value to examine whether the cookie is applicable for the
+ * request.
+ */
+public class DefaultCookiePolicy implements CookiePolicy {
+ private final CookiePolicy[] policies = {
+ new DomainPolicy(),
+ new PathPolicy(),
+ new ExpiresPolicy(),
+ new SecurePolicy(),
+ new EmptyValuePolicy()
+ };
+
+ public boolean accept(Cookie cookie, URL url) {
+ for (CookiePolicy policy : policies) {
+ if (!policy.accept(cookie, url)) {
+ return false;
+ }
+ }
+ return true;
+ }
+
+ public boolean matches(Cookie cookie, URL url) {
+ for (CookiePolicy policy : policies) {
+ if (!policy.matches(cookie, url)) {
+ return false;
+ }
+ }
+ return true;
+ }
+}
+
+/**
+ * Evaluates the domain attribute.
+ */
+class DomainPolicy implements CookiePolicy {
+ /**
+ * Returns whether the domain matches the request. Returns true if the
+ * domain attribute is not empty, and the domain matches the request host
+ * literally or via domain match.
+ */
+ public boolean matches(Cookie cookie, URL url) {
+ String domain = cookie.getDomain();
+ if (domain == null || domain.length() == 0) {
+ // domain is a required attribute
+ return false;
+ }
+
+ // make sure to add a leading dot
+ // this is to prevent matching "foo.com" against "badfoo.com"
+ if (!domain.startsWith(".")) {
+ domain = "." + domain;
+ }
+
+ String host = url.getHost();
+ // handle a direct match (i.e. domain is a FQDN)
+ if (domain.substring(1).equalsIgnoreCase(host)) {
+ return true;
+ }
+
+ // now match it as a domain
+ return host.endsWith(domain);
+ }
+
+ /**
+ * Same as <tt>matches()</tt>.
+ */
+ public boolean accept(Cookie cookie, URL url) {
+ return matches(cookie, url);
+ }
+}
+
+/**
+ * Evaluates the path attribute.
+ */
+class PathPolicy implements CookiePolicy {
+ /**
+ * Returns whether the path attribute matches the request.
+ */
+ public boolean matches(Cookie cookie, URL url) {
+ String cookiePath = cookie.getPath();
+ if (cookiePath == null || cookiePath.length() == 0) {
+ // path is required
+ return false;
+ }
+
+ // strip the ending "/" if it's not a simple "/"
+ if (!cookiePath.equals("/") && cookiePath.endsWith("/")) {
+ cookiePath = cookiePath.substring(0, cookiePath.length()-1);
+ }
+
+ String requestPath = url.getPath();
+ if (requestPath == null || requestPath.length() == 0) {
+ requestPath = "/";
+ }
+ // request path should be prefixed with the cookie path attribute
+ if (!requestPath.startsWith(cookiePath)) {
+ return false;
+ }
+
+ // check the case like matching "/foo" aginst "/foobad"
+ if (!cookiePath.equals("/") && !requestPath.equals(cookiePath)) {
+ // the first character after the match should be '/'
+ return requestPath.charAt(cookiePath.length()) == '/';
+ }
+
+ // match
+ return true;
+ }
+
+ /**
+ * Same as <tt>matches()</tt>.
+ */
+ public boolean accept(Cookie cookie, URL url) {
+ return matches(cookie, url);
+ }
+}
+
+/**
+ * Evaluates the expiration attribute.
+ */
+class ExpiresPolicy implements CookiePolicy {
+ /**
+ * Returns whether the cookie is expired. Returns true if it is a session
+ * cookie.
+ */
+ public boolean matches(Cookie cookie, URL url) {
+ Date expires = cookie.getExpires();
+ if (expires == null) {
+ // it is a session cookie; accept it
+ return true;
+ }
+
+ return expires.after(new Date());
+ }
+
+ /**
+ * Expiration should not be used when accepting. Expired cookies may be
+ * used to clear existing cookies.
+ */
+ public boolean accept(Cookie cookie, URL url) {
+ return true;
+ }
+}
+
+/**
+ * Evaluates the secure attribute.
+ */
+class SecurePolicy implements CookiePolicy {
+ public boolean matches(Cookie cookie, URL url) {
+ return !cookie.isSecure() || url.getProtocol().equals("https");
+ }
+
+ /**
+ * Same as <tt>matches()</tt>.
+ */
+ public boolean accept(Cookie cookie, URL url) {
+ return matches(cookie, url);
+ }
+}
+
+class EmptyValuePolicy implements CookiePolicy {
+ public boolean matches(Cookie cookie, URL url) {
+ String value = cookie.getValue();
+ return value != null && value.length() > 0;
+ }
+
+ /**
+ * We should accept empty cookies as they are used to clear existing
+ * cookies.
+ */
+ public boolean accept(Cookie cookie, URL url) {
+ return true;
+ }
+}
Propchange: geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/DefaultCookiePolicy.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/DefaultCookiePolicy.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/DefaultCookiePolicy.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpDecoder.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpDecoder.java?rev=633086&r1=633085&r2=633086&view=diff
==============================================================================
--- geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpDecoder.java (original)
+++ geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/HttpDecoder.java Mon Mar 3 05:57:05 2008
@@ -28,10 +28,14 @@
import org.apache.ahc.util.NameValuePair;
import org.apache.mina.common.IoBuffer;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
/**
* Utility class for helping to decode the HTTP Protocol.
*/
public class HttpDecoder {
+ static final Logger LOG = LoggerFactory.getLogger(HttpDecoder.class);
/** The Constant CHUNKED. */
public static final String CHUNKED = "chunked";
@@ -236,6 +240,7 @@
* @throws Exception if any exception occurs
*/
public void decodeHeader(String line, HttpResponseMessage msg) throws Exception {
+ LOG.debug("Processing Header Line: " + line);
// first, get rid of the CRLF from linear whitespace
line = line.replaceAll("\\r\\n([ \\t])", "$1");
int pos = line.indexOf(":");
@@ -406,7 +411,7 @@
* @see Cookie
*/
public Cookie decodeCookie(String cookieStr, HttpResponseMessage msg) throws Exception {
-
+ LOG.debug("Processing Cookie Line: " + cookieStr);
Cookie cookie = null;
String pairs[] = cookieStr.split(";");
@@ -414,40 +419,51 @@
String nameValue[] = pairs[i].trim().split("=");
String name = nameValue[0].trim();
+ //Check to see if the value was passed-in.
+ //According to rfc-2109 value is an optional property and could be blank.
+ boolean valuePresent = nameValue.length == 2;
+
//First one is the cookie name/value pair
if (i == 0) {
- cookie = new Cookie(name, nameValue[1].trim());
- continue;
- }
-
- if (name.equalsIgnoreCase(COOKIE_COMMENT)) {
- cookie.setComment(nameValue[1].trim());
+ if (valuePresent) {
+ cookie = new Cookie(name, nameValue[1].trim());
+ } else {
+ //Value is not passed-in, so creating a cookie with an empty value.
+ cookie = new Cookie(name, "");
+ }
continue;
}
- if (name.equalsIgnoreCase(COOKIE_PATH)) {
- cookie.setPath(nameValue[1].trim());
- }
-
if (name.equalsIgnoreCase(COOKIE_SECURE)) {
cookie.setSecure(true);
}
- if (name.equalsIgnoreCase(COOKIE_VERSION)) {
- cookie.setVersion(Integer.parseInt(nameValue[1]));
- }
+ if (valuePresent) {
+ if (name.equalsIgnoreCase(COOKIE_COMMENT)) {
+ cookie.setComment(nameValue[1].trim());
+ continue;
+ }
- if (name.equalsIgnoreCase(COOKIE_MAX_AGE)) {
- int age = Integer.parseInt(nameValue[1]);
- cookie.setExpires(new Date(System.currentTimeMillis() + age * 1000L));
- }
+ if (name.equalsIgnoreCase(COOKIE_PATH)) {
+ cookie.setPath(nameValue[1].trim());
+ }
- if (name.equalsIgnoreCase(COOKIE_EXPIRES)) {
- cookie.setExpires(DateUtil.parseDate(nameValue[1]));
- }
+ if (name.equalsIgnoreCase(COOKIE_VERSION)) {
+ cookie.setVersion(Integer.parseInt(nameValue[1]));
+ }
- if (name.equalsIgnoreCase(COOKIE_DOMAIN)) {
- cookie.setDomain(nameValue[1]);
+ if (name.equalsIgnoreCase(COOKIE_MAX_AGE)) {
+ int age = Integer.parseInt(nameValue[1]);
+ cookie.setExpires(new Date(System.currentTimeMillis() + age * 1000L));
+ }
+
+ if (name.equalsIgnoreCase(COOKIE_EXPIRES)) {
+ cookie.setExpires(DateUtil.parseDate(nameValue[1]));
+ }
+
+ if (name.equalsIgnoreCase(COOKIE_DOMAIN)) {
+ cookie.setDomain(nameValue[1]);
+ }
}
}
Added: geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/NullCookiePolicy.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/NullCookiePolicy.java?rev=633086&view=auto
==============================================================================
--- geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/NullCookiePolicy.java (added)
+++ geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/NullCookiePolicy.java Mon Mar 3 05:57:05 2008
@@ -0,0 +1,41 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ *
+ */
+package org.apache.ahc.codec;
+
+import java.net.URL;
+
+/**
+ * A trivial cookie policy that accepts all cookies.
+ */
+public class NullCookiePolicy implements CookiePolicy {
+ /**
+ * Returns always true.
+ */
+ public boolean accept(Cookie cookie, URL url) {
+ return true;
+ }
+
+ /**
+ * Returns always true.
+ */
+ public boolean matches(Cookie cookie, URL url) {
+ return true;
+ }
+}
Propchange: geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/NullCookiePolicy.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/NullCookiePolicy.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/async-http-client-mina2/src/main/java/org/apache/ahc/codec/NullCookiePolicy.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Added: geronimo/sandbox/async-http-client-mina2/src/test/java/org/apache/ahc/CookieTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/async-http-client-mina2/src/test/java/org/apache/ahc/CookieTest.java?rev=633086&view=auto
==============================================================================
--- geronimo/sandbox/async-http-client-mina2/src/test/java/org/apache/ahc/CookieTest.java (added)
+++ geronimo/sandbox/async-http-client-mina2/src/test/java/org/apache/ahc/CookieTest.java Mon Mar 3 05:57:05 2008
@@ -0,0 +1,190 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you 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.
+ *
+ */
+package org.apache.ahc;
+
+import java.net.URL;
+import java.util.Collection;
+import java.util.Date;
+
+import junit.framework.TestCase;
+
+import org.apache.ahc.codec.Cookie;
+import org.apache.ahc.codec.DefaultCookiePolicy;
+import org.apache.ahc.codec.HttpRequestMessage;
+
+public class CookieTest extends TestCase {
+ public void testCookieDomainFQDNMatch() throws Exception {
+ HttpRequestMessage request = new HttpRequestMessage(new URL("http://www.foo.com"), null);
+ request.setCookiePolicy(new DefaultCookiePolicy());
+
+ Cookie cookie = new Cookie("name", "value");
+ cookie.setDomain("www.foo.com");
+ cookie.setPath("/");
+ request.addCookie(cookie);
+
+ assertMatch(request, cookie);
+ }
+
+ public void testCookieDomainMatch() throws Exception {
+ HttpRequestMessage request = new HttpRequestMessage(new URL("http://abc.xyz.foo.com"), null);
+ request.setCookiePolicy(new DefaultCookiePolicy());
+
+ Cookie cookie = new Cookie("name", "value");
+ cookie.setDomain(".foo.com");
+ cookie.setPath("/");
+ request.addCookie(cookie);
+
+ assertMatch(request, cookie);
+ }
+
+ public void testCookieDomainNonMatch() throws Exception {
+ HttpRequestMessage request = new HttpRequestMessage(new URL("http://www.badfoo.com"), null);
+ request.setCookiePolicy(new DefaultCookiePolicy());
+
+ Cookie cookie = new Cookie("name", "value");
+ cookie.setDomain("foo.com");
+ cookie.setPath("/");
+ request.addCookie(cookie);
+
+ assertNoMatch(request);
+ }
+
+ public void testCookiePathTopLevelMatch() throws Exception {
+ HttpRequestMessage request = new HttpRequestMessage(new URL("http://www.foo.com"), null);
+ request.setCookiePolicy(new DefaultCookiePolicy());
+
+ Cookie cookie = new Cookie("name", "value");
+ cookie.setDomain(".foo.com");
+ cookie.setPath("/");
+ request.addCookie(cookie);
+
+ assertMatch(request, cookie);
+ }
+
+ public void testCookiePathMatch() throws Exception {
+ HttpRequestMessage request = new HttpRequestMessage(new URL("http://www.foo.com/foo/bar?zoh"), null);
+ request.setCookiePolicy(new DefaultCookiePolicy());
+
+ Cookie cookie = new Cookie("name", "value");
+ cookie.setDomain(".foo.com");
+ cookie.setPath("/foo");
+ request.addCookie(cookie);
+
+ assertMatch(request, cookie);
+ }
+
+ public void testCookiePathNonMatch() throws Exception {
+ HttpRequestMessage request = new HttpRequestMessage(new URL("http://www.foo.com/foobad"), null);
+ request.setCookiePolicy(new DefaultCookiePolicy());
+
+ Cookie cookie = new Cookie("name", "value");
+ cookie.setDomain(".foo.com");
+ cookie.setPath("/foo");
+ request.addCookie(cookie);
+
+ assertNoMatch(request);
+ }
+
+ public void testFreshCookie() throws Exception {
+ HttpRequestMessage request = new HttpRequestMessage(new URL("http://www.foo.com/"), null);
+ request.setCookiePolicy(new DefaultCookiePolicy());
+
+ Cookie cookie = new Cookie("name", "value");
+ cookie.setDomain(".foo.com");
+ cookie.setPath("/");
+ cookie.setExpires(new Date(System.currentTimeMillis() + 60*1000L));
+ request.addCookie(cookie);
+
+ assertMatch(request, cookie);
+ }
+
+ public void testExpiredCookie() throws Exception {
+ HttpRequestMessage request = new HttpRequestMessage(new URL("http://www.foo.com/"), null);
+ request.setCookiePolicy(new DefaultCookiePolicy());
+
+ Cookie cookie = new Cookie("name", "value");
+ cookie.setDomain(".foo.com");
+ cookie.setPath("/");
+ cookie.setExpires(new Date(System.currentTimeMillis() - 60*1000L));
+ request.addCookie(cookie);
+
+ assertNoMatch(request);
+ }
+
+ public void testSessionCookie() throws Exception {
+ HttpRequestMessage request = new HttpRequestMessage(new URL("http://www.foo.com/"), null);
+ request.setCookiePolicy(new DefaultCookiePolicy());
+
+ Cookie cookie = new Cookie("name", "value");
+ cookie.setDomain(".foo.com");
+ cookie.setPath("/");
+ request.addCookie(cookie);
+
+ assertMatch(request, cookie);
+ }
+
+ public void testCookieSecureMatch() throws Exception {
+ HttpRequestMessage request = new HttpRequestMessage(new URL("https://www.foo.com/"), null);
+ request.setCookiePolicy(new DefaultCookiePolicy());
+
+ Cookie cookie = new Cookie("name", "value");
+ cookie.setDomain(".foo.com");
+ cookie.setPath("/");
+ cookie.setSecure(true);
+ request.addCookie(cookie);
+
+ assertMatch(request, cookie);
+ }
+
+ public void testCookieSecureNonMatch() throws Exception {
+ HttpRequestMessage request = new HttpRequestMessage(new URL("http://www.foo.com/"), null);
+ request.setCookiePolicy(new DefaultCookiePolicy());
+
+ Cookie cookie = new Cookie("name", "value");
+ cookie.setDomain(".foo.com");
+ cookie.setPath("/");
+ cookie.setSecure(true);
+ request.addCookie(cookie);
+
+ assertNoMatch(request);
+ }
+
+ public void testEmptyCookieNonMatch() throws Exception {
+ HttpRequestMessage request = new HttpRequestMessage(new URL("http://www.foo.com/"), null);
+ request.setCookiePolicy(new DefaultCookiePolicy());
+
+ Cookie cookie = new Cookie("name", "");
+ cookie.setDomain(".foo.com");
+ cookie.setPath("/");
+ request.addCookie(cookie);
+
+ assertNoMatch(request);
+ }
+
+ private void assertMatch(HttpRequestMessage request, Cookie cookie) {
+ Collection<Cookie> cookies = request.getCookies();
+ assertTrue(cookies.size() == 1);
+ assertEquals(cookie, cookies.iterator().next());
+ }
+
+ private void assertNoMatch(HttpRequestMessage request) {
+ assertTrue(request.getCookies().isEmpty());
+ }
+}
Propchange: geronimo/sandbox/async-http-client-mina2/src/test/java/org/apache/ahc/CookieTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: geronimo/sandbox/async-http-client-mina2/src/test/java/org/apache/ahc/CookieTest.java
------------------------------------------------------------------------------
svn:keywords = Date Revision
Propchange: geronimo/sandbox/async-http-client-mina2/src/test/java/org/apache/ahc/CookieTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain
Modified: geronimo/sandbox/async-http-client-mina2/src/test/java/org/apache/ahc/ResponseHeaderParsingTest.java
URL: http://svn.apache.org/viewvc/geronimo/sandbox/async-http-client-mina2/src/test/java/org/apache/ahc/ResponseHeaderParsingTest.java?rev=633086&r1=633085&r2=633086&view=diff
==============================================================================
--- geronimo/sandbox/async-http-client-mina2/src/test/java/org/apache/ahc/ResponseHeaderParsingTest.java (original)
+++ geronimo/sandbox/async-http-client-mina2/src/test/java/org/apache/ahc/ResponseHeaderParsingTest.java Mon Mar 3 05:57:05 2008
@@ -1,13 +1,17 @@
package org.apache.ahc;
+import java.net.URL;
import java.util.Arrays;
import junit.framework.TestCase;
+import org.apache.ahc.codec.Cookie;
+import org.apache.ahc.codec.HttpDecoder;
import org.apache.ahc.codec.HttpIoHandler;
import org.apache.ahc.codec.HttpRequestMessage;
import org.apache.ahc.codec.HttpResponseDecoder;
import org.apache.ahc.codec.HttpResponseMessage;
+import org.apache.ahc.util.DateUtil;
import org.apache.mina.common.IoBuffer;
import org.apache.mina.common.IoSession;
@@ -24,6 +28,7 @@
+ " and-going-and-going\r\n"
+ "\r\n"
+ "<html></html>";
+ private static final String EMPTY_VALUE_COOKIE = "token=; path=/; expires=Thu, 01 Jan 1970 00:00:00 GMT";
public void testParsing() throws Exception {
IoBuffer buffer = IoBuffer.allocate(TEST_RESPONSE.length());
@@ -46,5 +51,18 @@
assertEquals("test-continue\tit-keeps-going and-going-and-going", response.getHeader("Private-Header"));
assertTrue(Arrays.equals(response.getContent(), "<html></html>".getBytes()));
+ }
+
+ public void testParsingOfAnEmptyCookieValue() throws Exception {
+ HttpResponseMessage msg = new HttpResponseMessage(new URL("http://www.foo.com"));
+ HttpDecoder decoder = new HttpDecoder();
+ Cookie c = decoder.decodeCookie(EMPTY_VALUE_COOKIE, msg);
+
+ assertNotNull(c);
+ assertEquals( "token", c.getName());
+ assertNotNull(c.getValue());
+ assertTrue(c.getValue().length() == 0);
+ assertEquals("/", c.getPath());
+ assertEquals(DateUtil.parseDate("Thu, 01 Jan 1970 00:00:00 GMT"), c.getExpires());
}
}