Skip to content

Commit 31cadbd

Browse files
committed
Merge pull request stleary#7 from stleary/Rework-CookieListTest
Rework cookie list test
2 parents f115d6a + 912350e commit 31cadbd

1 file changed

Lines changed: 89 additions & 35 deletions

File tree

‎CookieListTest.java‎

Lines changed: 89 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,36 +1,51 @@
11
package org.json.junit;
22

3-
import java.util.*;
4-
53
import static org.junit.Assert.*;
64

75
import org.json.*;
86
import org.junit.Test;
97

10-
118
/**
12-
* Tests for JSON-Java CookieList.java
13-
* The main differences between Cookie and CookieList appears to be that
14-
* CookieList does not treat the initial name/value pair different than
15-
* the other segments, and does not handle "secure".
16-
* Therefore the tests will be similar, but not identical.
9+
* HTTP cookie specification: RFC6265
10+
*
11+
* A cookie list is a JSONObject whose members are presumed to be cookie
12+
* name/value pairs. Entries are unescaped while being added, and escaped in
13+
* the toString() output.
14+
* Unescaping means to convert %hh hex strings to the ascii equivalent
15+
* and converting '+' to ' '.
16+
* Escaping converts '+', '%', '=', ';' and ascii control chars to %hh hex strings.
17+
*
18+
* CookieList should not be considered as just a list of Cookie objects:
19+
* - CookieList stores a cookie name/value pair as a single entry; Cookie stores
20+
* it as 2 entries (key="name" and key="value").
21+
* - 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.
1724
*/
1825
public class CookieListTest {
26+
1927
@Test(expected=NullPointerException.class)
2028
public void nullCookieListException() {
29+
/**
30+
* Attempts to create a CookieList from a null string
31+
*/
2132
String cookieStr = null;
2233
CookieList.toJSONObject(cookieStr);
2334
}
2435

2536
@Test(expected=JSONException.class)
2637
public void malFormedCookieListException() {
38+
/**
39+
* Attempts to create a CookieList from a malformed string
40+
*/
2741
String cookieStr = "thisCookieHasNoEqualsChar";
2842
CookieList.toJSONObject(cookieStr);
2943
}
3044

3145
@Test(expected=JSONException.class)
3246
public void emptyStringCookieList() {
3347
/**
48+
* Creates a CookieList from an empty string.
3449
* Cookie throws an exception, but CookieList does not
3550
*/
3651
String cookieStr = "";
@@ -42,44 +57,77 @@ public void emptyStringCookieList() {
4257

4358
@Test
4459
public void simpleCookieList() {
60+
/**
61+
* The simplest cookie is a name/value pair with no delimiter
62+
*/
4563
String cookieStr = "SID=31d4d96e407aad42";
4664
String expectedCookieStr = "{\"SID\":\"31d4d96e407aad42\"}";
4765
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
4866
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
4967
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
5068
}
5169

70+
@Test
71+
public void simpleCookieListWithDelimiter() {
72+
/**
73+
* The simplest cookie is a name/value pair with a delimiter
74+
*/
75+
String cookieStr = "SID=31d4d96e407aad42;";
76+
String expectedCookieStr = "{\"SID\":\"31d4d96e407aad42\"}";
77+
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
78+
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
79+
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
80+
}
81+
5282
@Test
5383
public void multiPartCookieList() {
5484
String cookieStr =
55-
"PH=deleted; "+
56-
" expires=Wed, 19-Mar-2014 17:53:53 GMT;"+
57-
"path=/; "+
58-
" domain=.yahoo.com;";
85+
"name1=myCookieValue1; "+
86+
" name2=myCookieValue2;"+
87+
"name3=myCookieValue3;"+
88+
" name4=myCookieValue4; "+
89+
"name5=myCookieValue5;"+
90+
" name6=myCookieValue6;";
5991
String expectedCookieStr =
60-
"{\"path\":\"/\","+
61-
"\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+
62-
"\"domain\":\".yahoo.com\","+
63-
"\"PH\":\"deleted\"}";
92+
"{"+
93+
"\"name1\":\"myCookieValue1\","+
94+
"\"name2\":\"myCookieValue2\","+
95+
"\"name3\":\"myCookieValue3\","+
96+
"\"name4\":\"myCookieValue4\","+
97+
"\"name5\":\"myCookieValue5\","+
98+
"\"name6\":\"myCookieValue6\""+
99+
"}";
64100
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
65101
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
66102
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
67103
}
68104

105+
@Test
106+
public void convertCookieListWithNullValueToString() {
107+
JSONObject jsonObject = new JSONObject();
108+
jsonObject.put("key", JSONObject.NULL);
109+
String cookieToStr = CookieList.toString(jsonObject);
110+
assertTrue("toString() should be empty", "".equals(cookieToStr));
111+
}
112+
69113
@Test
70114
public void convertCookieListToString() {
71115
String cookieStr =
72-
"PH=deleted; "+
73-
" expires=Wed, 19-Mar-2014 17:53:53 GMT;"+
74-
"path=/; "+
75-
" domain=.yahoo.com;"+
76-
"thisWont=beIncluded;";
116+
"name1=myCookieValue1; "+
117+
" name2=myCookieValue2;"+
118+
"name3=myCookieValue3;"+
119+
" name4=myCookieValue4; "+
120+
"name5=myCookieValue5;"+
121+
" name6=myCookieValue6;";
77122
String expectedCookieStr =
78-
"{\"path\":\"/\","+
79-
"\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+
80-
"\"domain\":\".yahoo.com\","+
81-
"\"thisWont\":\"beIncluded\","+
82-
"\"PH\":\"deleted\"}";
123+
"{"+
124+
"\"name1\":\"myCookieValue1\","+
125+
"\"name2\":\"myCookieValue2\","+
126+
"\"name3\":\"myCookieValue3\","+
127+
"\"name4\":\"myCookieValue4\","+
128+
"\"name5\":\"myCookieValue5\","+
129+
"\"name6\":\"myCookieValue6\""+
130+
"}";
83131
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
84132
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
85133
String cookieToStr = CookieList.toString(jsonObject);
@@ -88,18 +136,24 @@ public void convertCookieListToString() {
88136
Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
89137
}
90138

91-
@Test
139+
@Test
92140
public void convertEncodedCookieListToString() {
93141
String cookieStr =
94-
"PH=deleted; "+
95-
" expires=Wed,+19-Mar-2014+17:53:53+GMT;"+
96-
"path=/%2Bthis/is%26/a/spec%3Bsegment%3D; "+
97-
" domain=.yahoo.com;";
142+
"name1=myCookieValue1; "+
143+
" name2=my+Cookie+Value+2;"+
144+
"name3=my%2BCookie%26Value%3B3%3D;"+
145+
" name4=my%25CookieValue4; "+
146+
"name5=myCookieValue5;"+
147+
" name6=myCookieValue6;";
98148
String expectedCookieStr =
99-
"{\"path\":\"/+this/is&/a/spec;segment=\","+
100-
"\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+
101-
"\"domain\":\".yahoo.com\","+
102-
"\"PH\":\"deleted\"}";
149+
"{"+
150+
"\"name1\":\"myCookieValue1\","+
151+
"\"name2\":\"my Cookie Value 2\","+
152+
"\"name3\":\"my+Cookie&Value;3=\","+
153+
"\"name4\":\"my%CookieValue4\","+
154+
"\"name5\":\"myCookieValue5\","+
155+
"\"name6\":\"myCookieValue6\""+
156+
"}";
103157
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
104158
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
105159
String cookieToStr = CookieList.toString(jsonObject);

0 commit comments

Comments
 (0)