* Made the browser compatibility and the RFC2109 cookie specs configurable. Configuration options can be passed as constructor parameters.
* More test coverage git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@417820 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
4515632b29
commit
d9186337ef
|
@ -43,7 +43,7 @@ public class BasicExpiresHandler extends AbstractCookieAttributeHandler {
|
|||
if (datepatterns == null) {
|
||||
throw new IllegalArgumentException("Array of date patterns may not be null");
|
||||
}
|
||||
this.datepatterns = (String[]) datepatterns.clone();
|
||||
this.datepatterns = datepatterns;
|
||||
}
|
||||
|
||||
public void parse(final Cookie cookie, final String value)
|
||||
|
|
|
@ -49,8 +49,8 @@ import org.apache.http.util.DateUtils;
|
|||
*/
|
||||
public class BrowserCompatSpec extends CookieSpecBase {
|
||||
|
||||
/** Valid date patterns */
|
||||
private String[] datepatterns = new String[] {
|
||||
/** Valid date patterns used per default */
|
||||
private static final String[] DATE_PATTERNS = new String[] {
|
||||
DateUtils.PATTERN_RFC1123,
|
||||
DateUtils.PATTERN_RFC1036,
|
||||
DateUtils.PATTERN_ASCTIME,
|
||||
|
@ -67,9 +67,16 @@ public class BrowserCompatSpec extends CookieSpecBase {
|
|||
"EEE, dd-MM-yyyy HH:mm:ss z",
|
||||
};
|
||||
|
||||
private final String[] datepatterns;
|
||||
|
||||
/** Default constructor */
|
||||
public BrowserCompatSpec() {
|
||||
public BrowserCompatSpec(final String[] datepatterns) {
|
||||
super();
|
||||
if (datepatterns != null) {
|
||||
this.datepatterns = (String [])datepatterns.clone();
|
||||
} else {
|
||||
this.datepatterns = DATE_PATTERNS;
|
||||
}
|
||||
registerAttribHandler("path", new BasicPathHandler());
|
||||
registerAttribHandler("domain", new BasicDomainHandler());
|
||||
registerAttribHandler("max-age", new BasicMaxAgeHandler());
|
||||
|
@ -78,6 +85,11 @@ public class BrowserCompatSpec extends CookieSpecBase {
|
|||
registerAttribHandler("expires", new BasicExpiresHandler(this.datepatterns));
|
||||
}
|
||||
|
||||
/** Default constructor */
|
||||
public BrowserCompatSpec() {
|
||||
this(null);
|
||||
}
|
||||
|
||||
public Cookie[] parse(final Header header, final CookieOrigin origin)
|
||||
throws MalformedCookieException {
|
||||
if (header == null) {
|
||||
|
|
|
@ -61,28 +61,36 @@ public class RFC2109Spec extends CookieSpecBase {
|
|||
|
||||
private final static CookiePathComparator PATH_COMPARATOR = new CookiePathComparator();
|
||||
|
||||
private boolean oneHeader = false;
|
||||
private final static String[] DATE_PATTERNS = {
|
||||
DateUtils.PATTERN_RFC1123,
|
||||
DateUtils.PATTERN_RFC1036,
|
||||
DateUtils.PATTERN_ASCTIME
|
||||
};
|
||||
|
||||
private final String[] datepatterns;
|
||||
private final boolean oneHeader;
|
||||
|
||||
/** Default constructor */
|
||||
public RFC2109Spec(boolean oneHeader) {
|
||||
public RFC2109Spec(final String[] datepatterns, boolean oneHeader) {
|
||||
super();
|
||||
if (datepatterns != null) {
|
||||
this.datepatterns = (String [])datepatterns.clone();
|
||||
} else {
|
||||
this.datepatterns = DATE_PATTERNS;
|
||||
}
|
||||
this.oneHeader = oneHeader;
|
||||
registerAttribHandler("version", new RFC2109VersionHandler());
|
||||
registerAttribHandler("path", new BasicPathHandler());
|
||||
registerAttribHandler("domain", new RFC2109DomainHandler());
|
||||
registerAttribHandler("max-age", new BasicMaxAgeHandler());
|
||||
registerAttribHandler("secure", new BasicSecureHandler());
|
||||
registerAttribHandler("comment", new BasicCommentHandler());
|
||||
registerAttribHandler("expires", new BasicExpiresHandler(
|
||||
new String[] {
|
||||
DateUtils.PATTERN_RFC1123,
|
||||
DateUtils.PATTERN_RFC1036,
|
||||
DateUtils.PATTERN_ASCTIME}));
|
||||
this.oneHeader = oneHeader;
|
||||
registerAttribHandler("expires", new BasicExpiresHandler(this.datepatterns));
|
||||
}
|
||||
|
||||
/** Default constructor */
|
||||
public RFC2109Spec() {
|
||||
this(false);
|
||||
this(null, false);
|
||||
}
|
||||
|
||||
public Cookie[] parse(final Header header, final CookieOrigin origin)
|
||||
|
|
|
@ -35,6 +35,7 @@ import org.apache.http.cookie.Cookie;
|
|||
import org.apache.http.cookie.CookieOrigin;
|
||||
import org.apache.http.cookie.CookieSpec;
|
||||
import org.apache.http.cookie.MalformedCookieException;
|
||||
import org.apache.http.util.DateUtils;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
|
@ -65,6 +66,12 @@ public class TestBrowserCompatSpec extends TestCase {
|
|||
return new TestSuite(TestBrowserCompatSpec.class);
|
||||
}
|
||||
|
||||
public void testConstructor() throws Exception {
|
||||
new BrowserCompatSpec();
|
||||
new BrowserCompatSpec(null);
|
||||
new BrowserCompatSpec(new String[] { DateUtils.PATTERN_RFC1036 });
|
||||
}
|
||||
|
||||
/**
|
||||
* Tests whether domain attribute check is case-insensitive.
|
||||
*/
|
||||
|
|
|
@ -33,6 +33,7 @@ import org.apache.http.cookie.Cookie;
|
|||
import org.apache.http.cookie.CookieOrigin;
|
||||
import org.apache.http.cookie.CookieSpec;
|
||||
import org.apache.http.cookie.MalformedCookieException;
|
||||
import org.apache.http.util.DateUtils;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
|
@ -60,6 +61,12 @@ public class TestCookieRFC2109Spec extends TestCase {
|
|||
return new TestSuite(TestCookieRFC2109Spec.class);
|
||||
}
|
||||
|
||||
public void testConstructor() throws Exception {
|
||||
new RFC2109Spec();
|
||||
new RFC2109Spec(null, false);
|
||||
new RFC2109Spec(new String[] { DateUtils.PATTERN_RFC1036 }, false);
|
||||
}
|
||||
|
||||
public void testParseVersion() throws Exception {
|
||||
Header header = new Header("Set-Cookie","cookie-name=cookie-value; version=1");
|
||||
|
||||
|
@ -288,7 +295,7 @@ public class TestCookieRFC2109Spec extends TestCase {
|
|||
* Tests RFC 2109 compiant cookie formatting.
|
||||
*/
|
||||
public void testRFC2109CookieFormatting() throws Exception {
|
||||
CookieSpec cookiespec = new RFC2109Spec(false);
|
||||
CookieSpec cookiespec = new RFC2109Spec(null, false);
|
||||
Header header = new Header("Set-Cookie",
|
||||
"name=\"value\"; version=\"1\"; path=\"/\"; domain=\".mydomain.com\"");
|
||||
CookieOrigin origin = new CookieOrigin("myhost.mydomain.com", 80, "/", false);
|
||||
|
@ -312,7 +319,7 @@ public class TestCookieRFC2109Spec extends TestCase {
|
|||
}
|
||||
|
||||
public void testRFC2109CookiesFormatting() throws Exception {
|
||||
CookieSpec cookiespec = new RFC2109Spec(true);
|
||||
CookieSpec cookiespec = new RFC2109Spec(null, true);
|
||||
Header header = new Header("Set-Cookie",
|
||||
"name1=value1; path=/; domain=.mydomain.com, " +
|
||||
"name2=\"value2\"; version=\"1\"; path=\"/\"; domain=\".mydomain.com\"");
|
||||
|
@ -392,6 +399,30 @@ public class TestCookieRFC2109Spec extends TestCase {
|
|||
assertEquals("$Version=0; name=", headers[0].getValue());
|
||||
}
|
||||
|
||||
public void testCookieOrderingByPath() {
|
||||
Cookie c1 = new Cookie("name1", "value1");
|
||||
c1.setPath("/a/b/c");
|
||||
c1.setPathAttributeSpecified(true);
|
||||
Cookie c2 = new Cookie("name2", "value2");
|
||||
c2.setPath("/a/b");
|
||||
c2.setPathAttributeSpecified(true);
|
||||
Cookie c3 = new Cookie("name3", "value3");
|
||||
c3.setPath("/a");
|
||||
c3.setPathAttributeSpecified(true);
|
||||
Cookie c4 = new Cookie("name4", "value4");
|
||||
c4.setPath("/");
|
||||
c4.setPathAttributeSpecified(true);
|
||||
|
||||
CookieSpec cookiespec = new RFC2109Spec(null, true);
|
||||
Header[] headers = cookiespec.formatCookies(new Cookie[] { c2, c4, c1, c3 });
|
||||
assertNotNull(headers);
|
||||
assertEquals(1, headers.length);
|
||||
assertEquals("$Version=0; name1=value1; $Path=/a/b/c; " +
|
||||
"name2=value2; $Path=/a/b; " +
|
||||
"name3=value3; $Path=/a; " +
|
||||
"name4=value4; $Path=/", headers[0].getValue());
|
||||
}
|
||||
|
||||
public void testInvalidInput() throws Exception {
|
||||
CookieSpec cookiespec = new RFC2109Spec();
|
||||
try {
|
||||
|
|
Loading…
Reference in New Issue