Skip to content

Commit

Permalink
Merge pull request #466 from tbroyer/RESTEASY-1031
Browse files Browse the repository at this point in the history
[RESTEASY-1031] NewCookie discards "Expires" field
  • Loading branch information
patriot1burke committed Mar 24, 2014
2 parents 437ab0b + de76591 commit cf7b632
Show file tree
Hide file tree
Showing 2 changed files with 88 additions and 16 deletions.
@@ -1,12 +1,14 @@
package org.jboss.resteasy.plugins.delegates;

import org.jboss.resteasy.util.DateUtil;
import org.jboss.resteasy.util.ParameterParser;

import javax.ws.rs.core.NewCookie;
import javax.ws.rs.ext.RuntimeDelegate;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
import java.util.Map;

/**
Expand Down Expand Up @@ -53,7 +55,7 @@ else if (name.equalsIgnoreCase("HttpOnly"))
else if (name.equalsIgnoreCase("Expires")) {
try
{
expiry = new SimpleDateFormat(OLD_COOKIE_PATTERN).parse(value);
expiry = new SimpleDateFormat(OLD_COOKIE_PATTERN, Locale.US).parse(value);
}
catch (ParseException e)
{
Expand All @@ -65,7 +67,7 @@ else if (name.equalsIgnoreCase("Expires")) {

}

return new NewCookie(cookieName, cookieValue, path, domain, version, comment, maxAge, null, secure, httpOnly);
return new NewCookie(cookieName, cookieValue, path, domain, version, comment, maxAge, expiry, secure, httpOnly);

}

Expand Down Expand Up @@ -106,6 +108,10 @@ public String toString(Object value) {
b.append(";Max-Age=");
b.append(cookie.getMaxAge());
}
if (cookie.getExpiry() != null) {
b.append(";Expires=");
quote(b, DateUtil.formatDate(cookie.getExpiry(), OLD_COOKIE_PATTERN));
}
if (cookie.isSecure())
b.append(";Secure");
if (cookie.isHttpOnly())
Expand Down
Expand Up @@ -26,6 +26,7 @@
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -331,11 +332,14 @@ public void testNewCookie1() throws Exception
NewCookie nck26 = javax.ws.rs.core.NewCookie.valueOf(NewCookie_toParse);

pass = verifyNewCookie(nck26, name, value, path, domain, version,
"", -1, false);
"", -1, null, false, false);

// check round-tripping
Assert.assertEquals(nck26, NewCookie.valueOf(nck26.toString()));
}

/*
* Create a version 0 NewCookie instance by Parsing a String
* Create a version 1 NewCookie instance by Parsing a String
*/
@Test
public void testNewCookie2() throws Exception
Expand All @@ -353,7 +357,10 @@ public void testNewCookie2() throws Exception
NewCookie nck27 = javax.ws.rs.core.NewCookie.valueOf(NewCookie_toParse);

pass = verifyNewCookie(nck27, name, value, path, domain, version,
"", -1, false);
"", -1, null, false, false);

// check round-tripping
Assert.assertEquals(nck27, NewCookie.valueOf(nck27.toString()));
}

/*
Expand All @@ -374,15 +381,47 @@ public void testNewCookie3() throws Exception
}
}


/*
* Create a version 0 NewCookie instance by Parsing a String
*/
@Test
public void testNewCookie4() throws Exception
{
boolean pass = true;
String NewCookie_toParse =
"Customer=WILE_E_COYOTE; Path=/acme; Domain=acme.com; Max-Age=150000000; " +
"Expires=Thu, 03-May-2018 10:36:34 GMT; Secure; HttpOnly";

String name = "customer";
String value = "wile_e_coyote";
String path = "/acme";
String domain = "acme.com";
int version = 1;
String comment = "";
int maxAge = 150000000;
Date expiry = new Date(Date.UTC(118, 4, 3, 10, 36, 34));
boolean secure = true;
boolean httpOnly = true;

NewCookie nck28 = javax.ws.rs.core.NewCookie.valueOf(NewCookie_toParse);

pass = verifyNewCookie(nck28, name, value, path, domain, version,
comment, maxAge, expiry, secure, httpOnly);

// check round-tripping
Assert.assertEquals(nck28, NewCookie.valueOf(nck28.toString()));
}

private boolean verifyNewCookie(NewCookie nck, String name, String value,
String path, String domain, int version, String comment, int maxage,
boolean secure) throws Exception
Date expiry, boolean secure, boolean httpOnly) throws Exception
{

StringBuffer sb = new StringBuffer();
boolean pass = true;

if (name == "" || name == null)
if (name == null || name.isEmpty())
{
pass = false;
sb.append("NewCookie's name is empty");
Expand All @@ -394,7 +433,7 @@ else if (!nck.getName().toLowerCase().equals(name))
nck.getName());
}

if (value == "" || value == null)
if (value == null || value.isEmpty())
{
pass = false;
sb.append("NewCookie's value is empty");
Expand All @@ -413,7 +452,7 @@ else if (!nck.getValue().toLowerCase().equals(value))
nck.getVersion());
}

if (comment == "" || comment == null)
if (comment == null || comment.isEmpty())
{
if (nck.getComment() != "" && nck.getComment() != null)
{
Expand All @@ -429,7 +468,7 @@ else if (!nck.getComment().toLowerCase().equals(comment))
nck.getComment());
}

if (path == "" || path == null)
if (path == null || path.isEmpty())
{
if (nck.getPath() != "" && nck.getPath() != null)
{
Expand All @@ -438,7 +477,7 @@ else if (!nck.getComment().toLowerCase().equals(comment))
nck.getPath());
}
}
else if (nck.getPath() == null || nck.getPath() == "")
else if (nck.getPath() == null || nck.getPath().isEmpty())
{
pass = false;
sb.append("Failed path test. Got null, expecting " + path);
Expand All @@ -450,12 +489,12 @@ else if (!nck.getPath().toLowerCase().equals(path))
nck.getPath());
}

if (domain == "" || domain == null)
if (domain == null || domain.isEmpty())
{
if (nck.getDomain() != "" && nck.getDomain() != null)
if (nck.getDomain() != null && !nck.getDomain().isEmpty())
{
pass = false;
sb.append("Failed path test. Expect " + domain + " got " +
sb.append("Failed domain test. Expect " + domain + " got " +
nck.getDomain());
}
}
Expand All @@ -473,16 +512,43 @@ else if (!nck.getDomain().toLowerCase().equals(domain))
nck.getMaxAge());
}

if (expiry == null)
{
if (nck.getExpiry() != null)
{
pass = false;
sb.append("Failed expiry test. Expect " + expiry + " got " +
nck.getExpiry());
}
}
else if (nck.getExpiry() == null)
{
pass = false;
sb.append("Failed expiry test. Got null, expecting " + expiry);
}
else if (!nck.getExpiry().equals(expiry)) {
pass = false;
sb.append("Failed expirytest. Expect " + expiry + " got " +
nck.getExpiry());
}

if (nck.isSecure() != secure)
{
pass = false;
sb.append("Failed secure test. Expect " + secure + " got " +
nck.isSecure());
}

if (nck.isHttpOnly() != httpOnly)
{
pass = false;
sb.append("Failed httpOnly test. Expect " + httpOnly + " got " +
nck.isHttpOnly());
}

if (!pass)
{
throw new Exception("At least one assertion falied: " + sb.toString());
throw new Exception("At least one assertion failed: " + sb.toString());
}

return pass;
Expand Down Expand Up @@ -873,4 +939,4 @@ private String verifyResponse(Response resp, String content, int status,
}


}
}

0 comments on commit cf7b632

Please sign in to comment.