From 3406acd0aa2bc6a5e355b5b5da4ce2109b042dcc Mon Sep 17 00:00:00 2001 From: stleary Date: Fri, 3 Apr 2015 14:35:59 -0500 Subject: [PATCH] 97.5% coverage --- CookieTest.java | 268 ++++++++++++++++-------------------------------- 1 file changed, 86 insertions(+), 182 deletions(-) diff --git a/CookieTest.java b/CookieTest.java index d8453e9..d5bc239 100644 --- a/CookieTest.java +++ b/CookieTest.java @@ -10,211 +10,115 @@ import org.junit.Test; /** * Tests for JSON-Java Cookie.java - * Paraphrased from: - * http://www.nczonline.net/blog/2009/05/05/http-cookies-explained/ - * - * 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 + * See RFC6265 + * At its most basic, a cookie is a name=value pair. + * A JSON-Java encoded cookie escapes '+', '%', '=', ';' with %hh values. */ 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) - public void shouldHandleNullCookie() { + public void nullCookieException() { String cookieStr = null; Cookie.toJSONObject(cookieStr); } @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 = ""; Cookie.toJSONObject(cookieStr); } @Test - public void shouldHandleNonEncodedCookie() { - JSONObject jsonObject = Cookie.toJSONObject(simpleCookieStr); - Set keySet = jsonObject.keySet(); - assertTrue("Keyset should have exactly 7 keys", keySet.size() == 7); - assertTrue("name should have expected value", - "PH".equals(jsonObject.getString("name"))); - 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)); + public void simpleCookie() { + String cookieStr = "SID=31d4d96e407aad42"; + String expectedCookieStr = "{\"name\":\"SID\",\"value\":\"31d4d96e407aad42\"}"; + JSONObject jsonObject = Cookie.toJSONObject(cookieStr); + JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); } @Test - public void shouldConvertNonEncodedCookieToString() { - int idx; - String expectedStr; - JSONObject jsonObject = Cookie.toJSONObject(simpleCookieStr); - String cookieStr = Cookie.toString(jsonObject); - - // check for unordered expected output - expectedStr = "path=/"; - idx = cookieStr.indexOf(expectedStr); - assertTrue("path should be included in string output", idx != -1); - cookieStr = cookieStr.substring(0, idx)+ - cookieStr.substring(idx+expectedStr.length()); - - expectedStr = "expires=Wed, 19-Mar-2014 17:53:53 GMT"; - idx = cookieStr.indexOf(expectedStr); - assertTrue("expires should be included in string output", idx != -1); - cookieStr = cookieStr.substring(0, idx)+ - 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); + public void multiPartCookie() { + String cookieStr = + "PH=deleted; "+ + " expires=Wed, 19-Mar-2014 17:53:53 GMT;"+ + "path=/; "+ + " domain=.yahoo.com;"+ + "secure"; + String expectedCookieStr = + "{\"path\":\"/\","+ + "\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+ + "\"domain\":\".yahoo.com\","+ + "\"name\":\"PH\","+ + "\"secure\":true,"+ + "\"value\":\"deleted\"}"; + JSONObject jsonObject = Cookie.toJSONObject(cookieStr); + JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); } @Test - public void shouldHandleEncodedCookie() { - JSONObject jsonObject = Cookie.toJSONObject(encodedCookieStr); - Set keySet = jsonObject.keySet(); - // Note: the 7th key/value is not used by Cookie.java - assertTrue("Keyset should have exactly 7 keys", keySet.size() == 7); - assertTrue("name should have expected value", - "PH".equals(jsonObject.getString("name"))); - assertTrue("Value should have expected value", - "contains+some+chars".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?some escape chars".equals( - jsonObject.getString("domain"))); - assertTrue("path should have expected value", - "/".equals( - jsonObject.getString("path"))); - Boolean secureBool = jsonObject.getBoolean("secure"); - assertTrue("secure should be found in jsonObject", secureBool != null); - assertTrue("secure should have expected value", - secureBool.equals(true)); - String expectedStr = "{\"33748770511_20150319\":{\"expires\":14269604134"+ - "19,\"data\":{\"nv\":3,\"bn\":0,\"collapsed\":0}}}"; - assertTrue("CRZY should have expected value", - expectedStr.equals(jsonObject.getString("CRZY"))); + public void convertCookieToString() { + String cookieStr = + "PH=deleted; "+ + " expires=Wed, 19-Mar-2014 17:53:53 GMT;"+ + "path=/; "+ + " domain=.yahoo.com;"+ + "thisWont=beIncluded;"+ + "secure"; + String expectedCookieStr = + "{\"path\":\"/\","+ + "\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+ + "\"domain\":\".yahoo.com\","+ + "\"name\":\"PH\","+ + "\"secure\":true,"+ + "\"value\":\"deleted\"}"; + /** + * ToString() will omit the non-standard segment, + * but it will still be stored in the JSONObject + */ + String expectedDirectCompareCookieStr = + expectedCookieStr.replaceAll("\\{", "\\{\"thisWont\":\"beIncluded\","); + JSONObject jsonObject = Cookie.toJSONObject(cookieStr); + JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); + JSONObject expectedDirectCompareJsonObject = + new JSONObject(expectedDirectCompareCookieStr); + String cookieToStr = Cookie.toString(jsonObject); + JSONObject finalJsonObject = Cookie.toJSONObject(cookieToStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedDirectCompareJsonObject); + Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); } @Test - public void shouldConvertEncodedCookieToString() { - int idx; - String expectedStr; - JSONObject jsonObject = Cookie.toJSONObject(encodedCookieStr); - String cookieStr = Cookie.toString(jsonObject); - - // check for unordered expected output - expectedStr = "path=/"; - idx = cookieStr.indexOf(expectedStr); - assertTrue("path should be included in string output", idx != -1); - cookieStr = cookieStr.substring(0, idx)+ - cookieStr.substring(idx+expectedStr.length()); - - expectedStr = "expires=Wed, 19-Mar-2014 17:53:53 GMT"; - idx = cookieStr.indexOf(expectedStr); - assertTrue("expires should be included in string output", idx != -1); - cookieStr = cookieStr.substring(0, idx)+ - cookieStr.substring(idx+expectedStr.length()); - - expectedStr = "domain=.yahoo.com?some escape chars"; - 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); + public void convertEncodedCookieToString() { + String cookieStr = + "PH=deleted; "+ + " expires=Wed,+19-Mar-2014+17:53:53+GMT;"+ + "path=/%2Bthis/is%26/a/spec%3Bsegment%3D; "+ + " domain=.yahoo.com;"+ + "secure"; + String expectedCookieStr = + "{\"path\":\"/+this/is&/a/spec;segment=\","+ + "\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+ + "\"domain\":\".yahoo.com\","+ + "\"name\":\"PH\","+ + "\"secure\":true,"+ + "\"value\":\"deleted\"}"; + JSONObject jsonObject = Cookie.toJSONObject(cookieStr); + JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); + String cookieToStr = Cookie.toString(jsonObject); + JSONObject finalJsonObject = Cookie.toJSONObject(cookieToStr); + Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); + Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); } }