mirror of
https://github.com/ethauvin/JSON-java.git
synced 2025-06-17 07:50:52 -07:00
97.5% coverage
This commit is contained in:
parent
ac8b1b098a
commit
3406acd0aa
1 changed files with 86 additions and 182 deletions
268
CookieTest.java
268
CookieTest.java
|
@ -10,211 +10,115 @@ import org.junit.Test;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for JSON-Java Cookie.java
|
* Tests for JSON-Java Cookie.java
|
||||||
* Paraphrased from:
|
* See RFC6265
|
||||||
* http://www.nczonline.net/blog/2009/05/05/http-cookies-explained/
|
* At its most basic, a cookie is a name=value pair.
|
||||||
*
|
* A JSON-Java encoded cookie escapes '+', '%', '=', ';' with %hh values.
|
||||||
* A web server specifies a cookie to be stored by sending an HTTP header
|
|
||||||
* called Set-Cookie. The format of the Set-Cookie header is a string as
|
|
||||||
* follows (parts in square brackets are optional):
|
|
||||||
* Set-Cookie: value[; expires=date][; domain=domain][; path=path][; secure]
|
|
||||||
* Where value is usually, but not always, a key/value pair: name=value
|
|
||||||
* Separators between the optional segments (e.g. expires=date) consist of a
|
|
||||||
* semicolon followed by a space.
|
|
||||||
*
|
|
||||||
* Although cookies are typically url-encoded, they don't have to be.
|
|
||||||
*
|
|
||||||
* expires date example:
|
|
||||||
* Set-Cookie: name=Nicholas; expires=Sat, 02 May 2009 23:38:25 GMT
|
|
||||||
*
|
|
||||||
* domain option example:
|
|
||||||
* Set-Cookie: name=Nicholas; domain=nczonline.net
|
|
||||||
*
|
|
||||||
* Path option example:
|
|
||||||
* Set-Cookie: name=Nicholas; path=/blog
|
|
||||||
*
|
|
||||||
* Secure option example (it is just a flag):
|
|
||||||
* Set-Cookie: name=Nicholas; secure
|
|
||||||
*
|
|
||||||
* Subcookies. There is a hard limit of size (4k) that can't be finessed.
|
|
||||||
* But many browsers (not Chrome) have a max cookies per domain limit
|
|
||||||
* (usually 50). To get around this, subcookies are encoded in the initial
|
|
||||||
* name/value pair as follows:
|
|
||||||
* name=a=b&c=d&e=f&g=h
|
|
||||||
*/
|
*/
|
||||||
public class CookieTest {
|
public class CookieTest {
|
||||||
|
|
||||||
String simpleCookieStr =
|
|
||||||
"PH=deleted"+
|
|
||||||
"; expires=Wed, 19-Mar-2014 17:53:53 GMT"+
|
|
||||||
";path=/"+
|
|
||||||
"; domain=.yahoo.com"+
|
|
||||||
";secure"+
|
|
||||||
";not=included";
|
|
||||||
|
|
||||||
String encodedCookieStr =
|
|
||||||
"PH=contains+some+chars"+
|
|
||||||
";expires=Wed, 19-Mar-2014 17:53:53 GMT"+
|
|
||||||
"; path=/"+
|
|
||||||
";domain=.yahoo.com?some+escape+chars"+
|
|
||||||
"; secure"+
|
|
||||||
"; CRZY=%7B%2233748770511_20150319%22%3A%7B%22expires%22%3A142696041"+
|
|
||||||
"3419%2C%22data%22%3A%7B%22nv%22%3A3%2C%22bn%22%3A0%2C%22collapsed%2"+
|
|
||||||
"2%3A0%7D%7D%7D";
|
|
||||||
|
|
||||||
@Test(expected=NullPointerException.class)
|
@Test(expected=NullPointerException.class)
|
||||||
public void shouldHandleNullCookie() {
|
public void nullCookieException() {
|
||||||
String cookieStr = null;
|
String cookieStr = null;
|
||||||
Cookie.toJSONObject(cookieStr);
|
Cookie.toJSONObject(cookieStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test(expected=JSONException.class)
|
@Test(expected=JSONException.class)
|
||||||
public void shouldHandleEmptyStringCookie() {
|
public void malFormedCookieException() {
|
||||||
|
String cookieStr = "thisCookieHasNoEqualsChar";
|
||||||
|
Cookie.toJSONObject(cookieStr);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected=JSONException.class)
|
||||||
|
public void emptyStringCookieException() {
|
||||||
|
/**
|
||||||
|
* Cookie throws an exception, but CookieList does not
|
||||||
|
*/
|
||||||
String cookieStr = "";
|
String cookieStr = "";
|
||||||
Cookie.toJSONObject(cookieStr);
|
Cookie.toJSONObject(cookieStr);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldHandleNonEncodedCookie() {
|
public void simpleCookie() {
|
||||||
JSONObject jsonObject = Cookie.toJSONObject(simpleCookieStr);
|
String cookieStr = "SID=31d4d96e407aad42";
|
||||||
Set<String> keySet = jsonObject.keySet();
|
String expectedCookieStr = "{\"name\":\"SID\",\"value\":\"31d4d96e407aad42\"}";
|
||||||
assertTrue("Keyset should have exactly 7 keys", keySet.size() == 7);
|
JSONObject jsonObject = Cookie.toJSONObject(cookieStr);
|
||||||
assertTrue("name should have expected value",
|
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
|
||||||
"PH".equals(jsonObject.getString("name")));
|
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||||
assertTrue("Value should have expected value",
|
|
||||||
"deleted".equals(jsonObject.getString("value")));
|
|
||||||
assertTrue("expires should have expected value",
|
|
||||||
"Wed, 19-Mar-2014 17:53:53 GMT".equals(
|
|
||||||
jsonObject.getString("expires")));
|
|
||||||
assertTrue("domain should have expected value",
|
|
||||||
".yahoo.com".equals(
|
|
||||||
jsonObject.getString("domain")));
|
|
||||||
assertTrue("path should have expected value",
|
|
||||||
"/".equals(
|
|
||||||
jsonObject.getString("path")));
|
|
||||||
assertTrue("not should have expected value",
|
|
||||||
"included".equals(
|
|
||||||
jsonObject.getString("not")));
|
|
||||||
Boolean secureBool = jsonObject.getBoolean("secure");
|
|
||||||
assertTrue("secure should be found in jsonObject", secureBool != null);
|
|
||||||
assertTrue("secure should have expected value",
|
|
||||||
secureBool.equals(true));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldConvertNonEncodedCookieToString() {
|
public void multiPartCookie() {
|
||||||
int idx;
|
String cookieStr =
|
||||||
String expectedStr;
|
"PH=deleted; "+
|
||||||
JSONObject jsonObject = Cookie.toJSONObject(simpleCookieStr);
|
" expires=Wed, 19-Mar-2014 17:53:53 GMT;"+
|
||||||
String cookieStr = Cookie.toString(jsonObject);
|
"path=/; "+
|
||||||
|
" domain=.yahoo.com;"+
|
||||||
// check for unordered expected output
|
"secure";
|
||||||
expectedStr = "path=/";
|
String expectedCookieStr =
|
||||||
idx = cookieStr.indexOf(expectedStr);
|
"{\"path\":\"/\","+
|
||||||
assertTrue("path should be included in string output", idx != -1);
|
"\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+
|
||||||
cookieStr = cookieStr.substring(0, idx)+
|
"\"domain\":\".yahoo.com\","+
|
||||||
cookieStr.substring(idx+expectedStr.length());
|
"\"name\":\"PH\","+
|
||||||
|
"\"secure\":true,"+
|
||||||
expectedStr = "expires=Wed, 19-Mar-2014 17:53:53 GMT";
|
"\"value\":\"deleted\"}";
|
||||||
idx = cookieStr.indexOf(expectedStr);
|
JSONObject jsonObject = Cookie.toJSONObject(cookieStr);
|
||||||
assertTrue("expires should be included in string output", idx != -1);
|
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
|
||||||
cookieStr = cookieStr.substring(0, idx)+
|
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||||
cookieStr.substring(idx+expectedStr.length());
|
|
||||||
|
|
||||||
expectedStr = "domain=.yahoo.com";
|
|
||||||
idx = cookieStr.indexOf(expectedStr);
|
|
||||||
assertTrue("domain should be included in string output", idx != -1);
|
|
||||||
cookieStr = cookieStr.substring(0, idx)+
|
|
||||||
cookieStr.substring(idx+expectedStr.length());
|
|
||||||
|
|
||||||
expectedStr = "PH=deleted";
|
|
||||||
idx = cookieStr.indexOf(expectedStr);
|
|
||||||
assertTrue("name/value should be included in string output", idx != -1);
|
|
||||||
cookieStr = cookieStr.substring(0, idx)+
|
|
||||||
cookieStr.substring(idx+expectedStr.length());
|
|
||||||
|
|
||||||
expectedStr = "secure";
|
|
||||||
idx = cookieStr.indexOf(expectedStr);
|
|
||||||
assertTrue("secure should be included in string output", idx != -1);
|
|
||||||
cookieStr = cookieStr.substring(0, idx)+
|
|
||||||
cookieStr.substring(idx+expectedStr.length());
|
|
||||||
|
|
||||||
// after semicolons, nothing should be left
|
|
||||||
cookieStr = cookieStr.replaceAll(";", "");
|
|
||||||
assertTrue("nothing else should remain in cookie toString()",
|
|
||||||
cookieStr.length() == 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldHandleEncodedCookie() {
|
public void convertCookieToString() {
|
||||||
JSONObject jsonObject = Cookie.toJSONObject(encodedCookieStr);
|
String cookieStr =
|
||||||
Set<String> keySet = jsonObject.keySet();
|
"PH=deleted; "+
|
||||||
// Note: the 7th key/value is not used by Cookie.java
|
" expires=Wed, 19-Mar-2014 17:53:53 GMT;"+
|
||||||
assertTrue("Keyset should have exactly 7 keys", keySet.size() == 7);
|
"path=/; "+
|
||||||
assertTrue("name should have expected value",
|
" domain=.yahoo.com;"+
|
||||||
"PH".equals(jsonObject.getString("name")));
|
"thisWont=beIncluded;"+
|
||||||
assertTrue("Value should have expected value",
|
"secure";
|
||||||
"contains+some+chars".equals(jsonObject.getString("value")));
|
String expectedCookieStr =
|
||||||
assertTrue("expires should have expected value",
|
"{\"path\":\"/\","+
|
||||||
"Wed, 19-Mar-2014 17:53:53 GMT".equals(
|
"\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+
|
||||||
jsonObject.getString("expires")));
|
"\"domain\":\".yahoo.com\","+
|
||||||
assertTrue("domain should have expected value",
|
"\"name\":\"PH\","+
|
||||||
".yahoo.com?some escape chars".equals(
|
"\"secure\":true,"+
|
||||||
jsonObject.getString("domain")));
|
"\"value\":\"deleted\"}";
|
||||||
assertTrue("path should have expected value",
|
/**
|
||||||
"/".equals(
|
* ToString() will omit the non-standard segment,
|
||||||
jsonObject.getString("path")));
|
* but it will still be stored in the JSONObject
|
||||||
Boolean secureBool = jsonObject.getBoolean("secure");
|
*/
|
||||||
assertTrue("secure should be found in jsonObject", secureBool != null);
|
String expectedDirectCompareCookieStr =
|
||||||
assertTrue("secure should have expected value",
|
expectedCookieStr.replaceAll("\\{", "\\{\"thisWont\":\"beIncluded\",");
|
||||||
secureBool.equals(true));
|
JSONObject jsonObject = Cookie.toJSONObject(cookieStr);
|
||||||
String expectedStr = "{\"33748770511_20150319\":{\"expires\":14269604134"+
|
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
|
||||||
"19,\"data\":{\"nv\":3,\"bn\":0,\"collapsed\":0}}}";
|
JSONObject expectedDirectCompareJsonObject =
|
||||||
assertTrue("CRZY should have expected value",
|
new JSONObject(expectedDirectCompareCookieStr);
|
||||||
expectedStr.equals(jsonObject.getString("CRZY")));
|
String cookieToStr = Cookie.toString(jsonObject);
|
||||||
|
JSONObject finalJsonObject = Cookie.toJSONObject(cookieToStr);
|
||||||
|
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedDirectCompareJsonObject);
|
||||||
|
Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldConvertEncodedCookieToString() {
|
public void convertEncodedCookieToString() {
|
||||||
int idx;
|
String cookieStr =
|
||||||
String expectedStr;
|
"PH=deleted; "+
|
||||||
JSONObject jsonObject = Cookie.toJSONObject(encodedCookieStr);
|
" expires=Wed,+19-Mar-2014+17:53:53+GMT;"+
|
||||||
String cookieStr = Cookie.toString(jsonObject);
|
"path=/%2Bthis/is%26/a/spec%3Bsegment%3D; "+
|
||||||
|
" domain=.yahoo.com;"+
|
||||||
// check for unordered expected output
|
"secure";
|
||||||
expectedStr = "path=/";
|
String expectedCookieStr =
|
||||||
idx = cookieStr.indexOf(expectedStr);
|
"{\"path\":\"/+this/is&/a/spec;segment=\","+
|
||||||
assertTrue("path should be included in string output", idx != -1);
|
"\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+
|
||||||
cookieStr = cookieStr.substring(0, idx)+
|
"\"domain\":\".yahoo.com\","+
|
||||||
cookieStr.substring(idx+expectedStr.length());
|
"\"name\":\"PH\","+
|
||||||
|
"\"secure\":true,"+
|
||||||
expectedStr = "expires=Wed, 19-Mar-2014 17:53:53 GMT";
|
"\"value\":\"deleted\"}";
|
||||||
idx = cookieStr.indexOf(expectedStr);
|
JSONObject jsonObject = Cookie.toJSONObject(cookieStr);
|
||||||
assertTrue("expires should be included in string output", idx != -1);
|
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
|
||||||
cookieStr = cookieStr.substring(0, idx)+
|
String cookieToStr = Cookie.toString(jsonObject);
|
||||||
cookieStr.substring(idx+expectedStr.length());
|
JSONObject finalJsonObject = Cookie.toJSONObject(cookieToStr);
|
||||||
|
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||||
expectedStr = "domain=.yahoo.com?some escape chars";
|
Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
|
||||||
idx = cookieStr.indexOf(expectedStr);
|
|
||||||
assertTrue("domain should be included in string output", idx != -1);
|
|
||||||
cookieStr = cookieStr.substring(0, idx)+
|
|
||||||
cookieStr.substring(idx+expectedStr.length());
|
|
||||||
|
|
||||||
expectedStr = "PH=contains%2bsome%2bchars";
|
|
||||||
idx = cookieStr.indexOf(expectedStr);
|
|
||||||
assertTrue("name/value should be included in string output", idx != -1);
|
|
||||||
cookieStr = cookieStr.substring(0, idx)+
|
|
||||||
cookieStr.substring(idx+expectedStr.length());
|
|
||||||
|
|
||||||
expectedStr = "secure";
|
|
||||||
idx = cookieStr.indexOf(expectedStr);
|
|
||||||
assertTrue("secure should be included in string output", idx != -1);
|
|
||||||
cookieStr = cookieStr.substring(0, idx)+
|
|
||||||
cookieStr.substring(idx+expectedStr.length());
|
|
||||||
|
|
||||||
// after semicolons, nothing should be left
|
|
||||||
cookieStr = cookieStr.replaceAll(";", "");
|
|
||||||
assertTrue("nothing else should remain in cookie toString()",
|
|
||||||
cookieStr.length() == 0);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue