Added cookie handler impls for 'expires' attribute shared by all common cookie specs
git-svn-id: https://svn.apache.org/repos/asf/jakarta/httpcomponents/httpclient/trunk@409408 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
41c44857a5
commit
27e004244c
|
@ -0,0 +1,72 @@
|
|||
/*
|
||||
* $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;
|
||||
import org.apache.http.util.DateParseException;
|
||||
import org.apache.http.util.DateUtils;
|
||||
|
||||
public class BasicExpiresHandler extends AbstractCookieAttributeHandler {
|
||||
|
||||
/** Valid date patterns */
|
||||
private final String[] datepatterns;
|
||||
|
||||
public BasicExpiresHandler(final String[] datepatterns) {
|
||||
if (datepatterns == null) {
|
||||
throw new IllegalArgumentException("Array of date patterns may not be null");
|
||||
}
|
||||
this.datepatterns = (String[]) datepatterns.clone();
|
||||
}
|
||||
|
||||
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 expires attribute");
|
||||
}
|
||||
try {
|
||||
cookie.setExpiryDate(DateUtils.parseDate(value, this.datepatterns));
|
||||
} catch (DateParseException dpe) {
|
||||
throw new MalformedCookieException("Unable to parse expires attribute: "
|
||||
+ value);
|
||||
}
|
||||
}
|
||||
|
||||
public boolean match(final Cookie cookie, final CookieOrigin origin) {
|
||||
if (cookie == null) {
|
||||
throw new IllegalArgumentException("Cookie may not be null");
|
||||
}
|
||||
return !cookie.isExpired();
|
||||
}
|
||||
|
||||
}
|
|
@ -28,10 +28,15 @@
|
|||
|
||||
package org.apache.http.cookie.impl;
|
||||
|
||||
import java.text.DateFormat;
|
||||
import java.text.SimpleDateFormat;
|
||||
import java.util.Date;
|
||||
|
||||
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 org.apache.http.util.DateUtils;
|
||||
|
||||
import junit.framework.Test;
|
||||
import junit.framework.TestCase;
|
||||
|
@ -260,4 +265,70 @@ public class TestBasicCookieAttribHandlers extends TestCase {
|
|||
}
|
||||
}
|
||||
|
||||
public void testBasicExpiresParse() throws Exception {
|
||||
Cookie cookie = new Cookie("name", "value");
|
||||
CookieAttributeHandler h = new BasicExpiresHandler(new String[] {DateUtils.PATTERN_RFC1123});
|
||||
|
||||
DateFormat dateformat = new SimpleDateFormat(DateUtils.PATTERN_RFC1123);
|
||||
dateformat.setTimeZone(DateUtils.GMT);
|
||||
|
||||
Date now = new Date();
|
||||
|
||||
h.parse(cookie, dateformat.format(now));
|
||||
assertNotNull(cookie.getExpiryDate());
|
||||
}
|
||||
|
||||
public void testBasicExpiresParseInvalid() throws Exception {
|
||||
Cookie cookie = new Cookie("name", "value");
|
||||
CookieAttributeHandler h = new BasicExpiresHandler(new String[] {DateUtils.PATTERN_RFC1123});
|
||||
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 testBasicExpiresMatch() throws Exception {
|
||||
Cookie cookie = new Cookie("name", "value");
|
||||
CookieAttributeHandler h = new BasicExpiresHandler(new String[] {DateUtils.PATTERN_RFC1123});
|
||||
CookieOrigin origin = new CookieOrigin("somehost", 80, "/stuff", false);
|
||||
|
||||
Date past = new Date(System.currentTimeMillis() - 20000000);
|
||||
Date future = new Date(System.currentTimeMillis() + 20000000);
|
||||
|
||||
cookie.setExpiryDate(past);
|
||||
assertFalse(h.match(cookie, origin));
|
||||
cookie.setExpiryDate(future);
|
||||
assertTrue(h.match(cookie, origin));
|
||||
}
|
||||
|
||||
public void testBasicExpiresInvalidInput() throws Exception {
|
||||
try {
|
||||
new BasicExpiresHandler(null);
|
||||
fail("IllegalArgumentException must have been thrown");
|
||||
} catch (IllegalArgumentException ex) {
|
||||
// expected
|
||||
}
|
||||
CookieAttributeHandler h = new BasicExpiresHandler(new String[] {DateUtils.PATTERN_RFC1123});
|
||||
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
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue