Fixed cookie lookup in case of children paths.

This commit is contained in:
Simone Bordet 2012-11-07 20:11:50 +01:00
parent 28fd4cceac
commit 19e856fa7b
2 changed files with 39 additions and 7 deletions

View File

@ -41,23 +41,27 @@ public class HttpCookieStore implements CookieStore
String host = destination.getHost();
int port = destination.getPort();
String key = host + ":" + port + path;
String key = host + ":" + port;
// First lookup: direct hit
Queue<HttpCookie> cookies = allCookies.get(key);
// Root path lookup
Queue<HttpCookie> cookies = allCookies.get(key + "/");
if (cookies != null)
accumulateCookies(destination, cookies, result);
// Second lookup: root path
if (!"/".equals(path))
// Path lookup
String[] split = path.split("/");
for (int i = 1; i < split.length; i++)
{
key = host + ":" + port + "/";
String segment = split[i];
key += "/" + segment;
cookies = allCookies.get(key);
if (cookies != null)
accumulateCookies(destination, cookies, result);
if (segment.length() > 0)
key += "/";
}
// Third lookup: parent domains
// Domain lookup
int domains = host.split("\\.").length - 1;
for (int i = 2; i <= domains; ++i)
{

View File

@ -100,6 +100,21 @@ public class HttpCookieStoreTest
Assert.assertEquals("1", cookie.getValue());
}
@Test
public void testCookieStoredWithPathIsRetrievedWithChildPath() throws Exception
{
CookieStore cookies = new HttpCookieStore();
Destination destination = new HttpDestination(client, "http", "localhost", 80);
Assert.assertTrue(cookies.addCookie(destination, new HttpCookie("a", "1", null, "/path")));
List<HttpCookie> result = cookies.findCookies(destination, "/path/child");
Assert.assertNotNull(result);
Assert.assertEquals(1, result.size());
HttpCookie cookie = result.get(0);
Assert.assertEquals("a", cookie.getName());
Assert.assertEquals("1", cookie.getValue());
}
@Test
public void testCookieStoredWithParentDomainIsRetrievedWithChildDomain() throws Exception
{
@ -118,6 +133,19 @@ public class HttpCookieStoreTest
Assert.assertEquals(2, result.size());
}
@Test
public void testCookieStoredWithChildDomainIsNotRetrievedWithParentDomain() throws Exception
{
CookieStore cookies = new HttpCookieStore();
Destination childDestination = new HttpDestination(client, "http", "child.localhost.org", 80);
Assert.assertTrue(cookies.addCookie(childDestination, new HttpCookie("b", "2", null, "/")));
Destination parentDestination = new HttpDestination(client, "http", "localhost.org", 80);
List<HttpCookie> result = cookies.findCookies(parentDestination, "/path");
Assert.assertNotNull(result);
Assert.assertEquals(0, result.size());
}
@Test
public void testExpiredCookieIsNotRetrieved() throws Exception
{