1010
1111/**
1212 * HTTP cookie specification: RFC6265
13- *
13+ * <p>
1414 * At its most basic, a cookie is a name=value pair. The value may be subdivided
1515 * into other cookies, but that is not tested here. The cookie may also include
1616 * certain named attributes, delimited by semicolons.
17- *
17+ * <p>
1818 * The Cookie.toString() method emits certain attributes if present: expires,
1919 * domain, path, secure. All but secure are name-value pairs. Other attributes
2020 * are not included in the toString() output.
21- *
21+ * <p>
2222 * A JSON-Java encoded cookie escapes '+', '%', '=', ';' with %hh values.
2323 */
2424public class CookieTest {
2525
26+ /**
27+ * Attempts to create a JSONObject from a null string.
28+ * Expects a NullPointerException.
29+ */
2630 @ Test (expected =NullPointerException .class )
2731 public void nullCookieException () {
28- /**
29- * Attempts to create a JSONObject from a null string
30- */
3132 String cookieStr = null ;
3233 Cookie .toJSONObject (cookieStr );
3334 }
3435
35- @ Test (expected =JSONException .class )
36+ /**
37+ * Attempts to create a JSONObject from a cookie string with
38+ * no '=' char.
39+ * Expects a JSONException.
40+ */
41+ @ Test
3642 public void malFormedNameValueException () {
37- /**
38- * Attempts to create a JSONObject from a malformed cookie string
39- */
4043 String cookieStr = "thisCookieHasNoEqualsChar" ;
41- Cookie .toJSONObject (cookieStr );
44+ try {
45+ Cookie .toJSONObject (cookieStr );
46+ assertTrue ("Expecting an exception" , false );
47+ } catch (JSONException e ) {
48+ assertTrue ("Expecting an exception message" ,
49+ e .getMessage ().startsWith ("Expected '=' and instead saw '" )
50+ && e .getMessage ().endsWith ("' at 27 [character 28 line 1]" ));
51+ }
4252 }
4353
44- @ Test (expected =JSONException .class )
54+ /**
55+ * Attempts to create a JSONObject from a cookie string
56+ * with embedded ';' char.
57+ * Expects a JSONException.
58+ */
59+ @ Test
4560 public void malFormedAttributeException () {
46- /**
47- * Attempts to create a JSONObject from a malformed cookie string
48- */
4961 String cookieStr = "this=Cookie;myAttribute" ;
50- Cookie .toJSONObject (cookieStr );
62+ try {
63+ Cookie .toJSONObject (cookieStr );
64+ assertTrue ("Expecting an exception" , false );
65+ } catch (JSONException e ) {
66+ assertTrue ("Expecting an exception message" ,
67+ "Missing '=' in cookie parameter. at 25 [character 26 line 1]" .
68+ equals (e .getMessage ()));
69+ }
5170 }
5271
53- @ Test (expected =JSONException .class )
72+ /**
73+ * Attempts to create a JSONObject from an empty cookie string.<br>
74+ * Note: Cookie throws an exception, but CookieList does not.<br>
75+ * Expects a JSONException
76+ */
77+ @ Test
5478 public void emptyStringCookieException () {
55- /**
56- * Attempts to create a JSONObject from an empty cookie string
57- * Note: Cookie throws an exception, but CookieList does not
58- */
5979 String cookieStr = "" ;
60- Cookie .toJSONObject (cookieStr );
80+ try {
81+ Cookie .toJSONObject (cookieStr );
82+ assertTrue ("Expecting an exception" , false );
83+ } catch (JSONException e ) {
84+ assertTrue ("Expecting an exception message" ,
85+ e .getMessage ().startsWith ("Expected '=' and instead saw '" ) &&
86+ e .getMessage ().endsWith ("' at 2 [character 3 line 1]" ));
87+ }
6188 }
6289
90+ /**
91+ * Cookie from a simple name/value pair with no delimiter
92+ */
6393 @ Test
6494 public void simpleCookie () {
65- /**
66- * The simplest cookie is a name/value pair with no delimiter
67- */
6895 String cookieStr = "SID=31d4d96e407aad42" ;
6996 String expectedCookieStr = "{\" name\" :\" SID\" ,\" value\" :\" 31d4d96e407aad42\" }" ;
7097 JSONObject jsonObject = Cookie .toJSONObject (cookieStr );
7198 JSONObject expectedJsonObject = new JSONObject (expectedCookieStr );
7299 Util .compareActualVsExpectedJsonObjects (jsonObject ,expectedJsonObject );
73100 }
74101
102+ /**
103+ * Store a cookie with all of the supported attributes in a
104+ * JSONObject. The secure attribute, which has no value, is treated
105+ * as a boolean.
106+ */
75107 @ Test
76108 public void multiPartCookie () {
77- /**
78- * Store a cookie with all of the supported attributes in a
79- * JSONObject. The secure attribute, which has no value, is treated
80- * as a boolean.
81- */
82109 String cookieStr =
83110 "PH=deleted; " +
84111 " expires=Wed, 19-Mar-2014 17:53:53 GMT;" +
@@ -99,13 +126,13 @@ public void multiPartCookie() {
99126 Util .compareActualVsExpectedJsonObjects (jsonObject ,expectedJsonObject );
100127 }
101128
129+ /**
130+ * Cookie.toString() will omit the non-standard "thiswont=beIncluded"
131+ * attribute, but the attribute is still stored in the JSONObject.
132+ * This test confirms both behaviors.
133+ */
102134 @ Test
103135 public void convertCookieToString () {
104- /**
105- * ToString() will omit the non-standard "thiswont=beIncluded"
106- * attribute, but the attribute is still stored in the JSONObject.
107- * This test confirms both behaviors.
108- */
109136 String cookieStr =
110137 "PH=deleted; " +
111138 " expires=Wed, 19-Mar-2014 17:53:53 GMT;" +
@@ -138,14 +165,14 @@ public void convertCookieToString() {
138165 Util .compareActualVsExpectedJsonObjects (finalJsonObject ,expectedJsonObject );
139166 }
140167
168+ /**
169+ * A string may be URL-encoded when converting to JSONObject.
170+ * If found, '+' is converted to ' ', and %hh hex strings are converted
171+ * to their ascii char equivalents. This test confirms the decoding
172+ * behavior.
173+ */
141174 @ Test
142175 public void convertEncodedCookieToString () {
143- /**
144- * A string may be URL-encoded when converting to JSONObject.
145- * If found, '+' is converted to ' ', and %hh hex strings are converted
146- * to their ascii char equivalents. This test confirms the decoding
147- * behavior.
148- */
149176 String cookieStr =
150177 "PH=deleted; " +
151178 " expires=Wed,+19-Mar-2014+17:53:53+GMT;" +
@@ -167,29 +194,29 @@ public void convertEncodedCookieToString() {
167194 Util .compareActualVsExpectedJsonObjects (finalJsonObject ,expectedJsonObject );
168195 }
169196
197+ /**
198+ * A public API method performs a URL encoding for selected chars
199+ * in a string. Control chars, '+', '%', '=', ';' are all encoded
200+ * as %hh hex strings. The string is also trimmed.
201+ * This test confirms that behavior.
202+ */
170203 @ Test
171204 public void escapeString () {
172- /**
173- * A public API method performs a URL encoding for selected chars
174- * in a string. Control chars, '+', '%', '=', ';' are all encoded
175- * as %hh hex strings. The string is also trimmed.
176- * This test confirms that behavior.
177- */
178205 String str = " +%\r \n \t \b %=;;; " ;
179206 String expectedStr = "%2b%25%0d%0a%09%08%25%3d%3b%3b%3b" ;
180207 String actualStr = Cookie .escape (str );
181208 assertTrue ("expect escape() to encode correctly. Actual: " +actualStr +
182209 " expected: " +expectedStr , expectedStr .equals (actualStr ));
183210 }
184211
212+ /**
213+ * A public API method performs URL decoding for strings.
214+ * '+' is converted to space and %hh hex strings are converted to
215+ * their ascii equivalent values. The string is not trimmed.
216+ * This test confirms that behavior.
217+ */
185218 @ Test
186219 public void unescapeString () {
187- /**
188- * A public API method performs URL decoding for strings.
189- * '+' is converted to space and %hh hex strings are converted to
190- * their ascii equivalent values. The string is not trimmed.
191- * This test confirms that behavior.
192- */
193220 String str = " +%2b%25%0d%0a%09%08%25%3d%3b%3b%3b+ " ;
194221 String expectedStr = " +%\r \n \t \b %=;;; " ;
195222 String actualStr = Cookie .unescape (str );
0 commit comments