66import org .junit .Test ;
77
88/**
9- * HTTP cookie specification: RFC6265
10- *
9+ * HTTP cookie specification RFC6265: http://tools.ietf.org/html/rfc6265
10+ * <p>
1111 * A cookie list is a JSONObject whose members are presumed to be cookie
1212 * name/value pairs. Entries are unescaped while being added, and escaped in
1313 * the toString() output.
1414 * Unescaping means to convert %hh hex strings to the ascii equivalent
1515 * and converting '+' to ' '.
1616 * Escaping converts '+', '%', '=', ';' and ascii control chars to %hh hex strings.
17- *
18- * CookieList should not be considered as just a list of Cookie objects:
17+ * <p>
18+ * CookieList should not be considered as just a list of Cookie objects:<br>
1919 * - CookieList stores a cookie name/value pair as a single entry; Cookie stores
20- * it as 2 entries (key="name" and key="value").
20+ * it as 2 entries (key="name" and key="value").<br>
2121 * - CookieList requires multiple name/value pairs as input; Cookie allows the
22- * 'secure' name with no associated value
23- * - CookieList has no special handling for attribute name/value pairs.
22+ * 'secure' name with no associated value<br>
23+ * - CookieList has no special handling for attribute name/value pairs.<br>
2424 */
2525public class CookieListTest {
2626
27+ /**
28+ * Attempts to create a CookieList from a null string.
29+ * Expects a NullPointerException.
30+ */
2731 @ Test (expected =NullPointerException .class )
2832 public void nullCookieListException () {
29- /**
30- * Attempts to create a CookieList from a null string
31- */
3233 String cookieStr = null ;
3334 CookieList .toJSONObject (cookieStr );
3435 }
3536
36- @ Test (expected =JSONException .class )
37+ /**
38+ * Attempts to create a CookieList from a malformed string.
39+ * Expects a JSONException.
40+ */
41+ @ Test
3742 public void malFormedCookieListException () {
38- /**
39- * Attempts to create a CookieList from a malformed string
40- */
4143 String cookieStr = "thisCookieHasNoEqualsChar" ;
42- CookieList .toJSONObject (cookieStr );
44+ try {
45+ CookieList .toJSONObject (cookieStr );
46+ assertTrue ("should throw an exception" , false );
47+ } catch (JSONException e ) {
48+ /**
49+ * Not sure of the missing char, but full string compare fails
50+ */
51+ assertTrue ("Expecting an exception message" ,
52+ e .getMessage ().startsWith ("Expected '=' and instead saw '" ) &&
53+ e .getMessage ().endsWith ("' at 27 [character 28 line 1]" ));
54+ }
4355 }
4456
45- @ Test (expected =JSONException .class )
57+ /**
58+ * Creates a CookieList from an empty string.
59+ */
60+ @ Test
4661 public void emptyStringCookieList () {
47- /**
48- * Creates a CookieList from an empty string.
49- * Cookie throws an exception, but CookieList does not
50- */
5162 String cookieStr = "" ;
5263 String expectedCookieStr = "" ;
5364 JSONObject jsonObject = CookieList .toJSONObject (cookieStr );
54- JSONObject expectedJsonObject = new JSONObject (expectedCookieStr );
55- Util .compareActualVsExpectedJsonObjects (jsonObject ,expectedJsonObject );
65+ assertTrue (jsonObject .length () == 0 );
5666 }
5767
68+ /**
69+ * CookieList with the simplest cookie - a name/value pair with no delimiter.
70+ */
5871 @ Test
5972 public void simpleCookieList () {
60- /**
61- * The simplest cookie is a name/value pair with no delimiter
62- */
6373 String cookieStr = "SID=31d4d96e407aad42" ;
6474 String expectedCookieStr = "{\" SID\" :\" 31d4d96e407aad42\" }" ;
6575 JSONObject jsonObject = CookieList .toJSONObject (cookieStr );
6676 JSONObject expectedJsonObject = new JSONObject (expectedCookieStr );
6777 Util .compareActualVsExpectedJsonObjects (jsonObject ,expectedJsonObject );
6878 }
6979
80+ /**
81+ * CookieList with a single a cookie which has a name/value pair and delimiter.
82+ */
7083 @ Test
7184 public void simpleCookieListWithDelimiter () {
72- /**
73- * The simplest cookie is a name/value pair with a delimiter
74- */
7585 String cookieStr = "SID=31d4d96e407aad42;" ;
7686 String expectedCookieStr = "{\" SID\" :\" 31d4d96e407aad42\" }" ;
7787 JSONObject jsonObject = CookieList .toJSONObject (cookieStr );
7888 JSONObject expectedJsonObject = new JSONObject (expectedCookieStr );
7989 Util .compareActualVsExpectedJsonObjects (jsonObject ,expectedJsonObject );
8090 }
8191
92+ /**
93+ * CookieList with multiple cookies consisting of name/value pairs
94+ * with delimiters.
95+ */
8296 @ Test
8397 public void multiPartCookieList () {
8498 String cookieStr =
@@ -102,6 +116,9 @@ public void multiPartCookieList() {
102116 Util .compareActualVsExpectedJsonObjects (jsonObject ,expectedJsonObject );
103117 }
104118
119+ /**
120+ * CookieList from a JSONObject with valid key and null value
121+ */
105122 @ Test
106123 public void convertCookieListWithNullValueToString () {
107124 JSONObject jsonObject = new JSONObject ();
@@ -110,6 +127,9 @@ public void convertCookieListWithNullValueToString() {
110127 assertTrue ("toString() should be empty" , "" .equals (cookieToStr ));
111128 }
112129
130+ /**
131+ * CookieList with multiple entries converted to a JSON document.
132+ */
113133 @ Test
114134 public void convertCookieListToString () {
115135 String cookieStr =
@@ -136,6 +156,10 @@ public void convertCookieListToString() {
136156 Util .compareActualVsExpectedJsonObjects (finalJsonObject ,expectedJsonObject );
137157 }
138158
159+ /**
160+ * CookieList with multiple entries and some '+' chars and URL-encoded
161+ * values converted to a JSON document.
162+ */
139163 @ Test
140164 public void convertEncodedCookieListToString () {
141165 String cookieStr =
0 commit comments