forked from stleary/JSON-Java-unit-test
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCookieListTest.java
More file actions
166 lines (152 loc) · 6.5 KB
/
CookieListTest.java
File metadata and controls
166 lines (152 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
package org.json.junit;
import static org.junit.Assert.*;
import org.json.*;
import org.junit.Test;
/**
* HTTP cookie specification: RFC6265
*
* A cookie list is a JSONObject whose members are presumed to be cookie
* name/value pairs. Entries are unescaped while being added, and escaped in
* the toString() output.
* Unescaping means to convert %hh hex strings to the ascii equivalent
* and converting '+' to ' '.
* Escaping converts '+', '%', '=', ';' and ascii control chars to %hh hex strings.
*
* CookieList should not be considered as just a list of Cookie objects:
* - CookieList stores a cookie name/value pair as a single entry; Cookie stores
* it as 2 entries (key="name" and key="value").
* - CookieList requires multiple name/value pairs as input; Cookie allows the
* 'secure' name with no associated value
* - CookieList has no special handling for attribute name/value pairs.
*/
public class CookieListTest {
@Test(expected=NullPointerException.class)
public void nullCookieListException() {
/**
* Attempts to create a CookieList from a null string
*/
String cookieStr = null;
CookieList.toJSONObject(cookieStr);
}
@Test(expected=JSONException.class)
public void malFormedCookieListException() {
/**
* Attempts to create a CookieList from a malformed string
*/
String cookieStr = "thisCookieHasNoEqualsChar";
CookieList.toJSONObject(cookieStr);
}
@Test(expected=JSONException.class)
public void emptyStringCookieList() {
/**
* Creates a CookieList from an empty string.
* Cookie throws an exception, but CookieList does not
*/
String cookieStr = "";
String expectedCookieStr = "";
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
}
@Test
public void simpleCookieList() {
/**
* The simplest cookie is a name/value pair with no delimiter
*/
String cookieStr = "SID=31d4d96e407aad42";
String expectedCookieStr = "{\"SID\":\"31d4d96e407aad42\"}";
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
}
@Test
public void simpleCookieListWithDelimiter() {
/**
* The simplest cookie is a name/value pair with a delimiter
*/
String cookieStr = "SID=31d4d96e407aad42;";
String expectedCookieStr = "{\"SID\":\"31d4d96e407aad42\"}";
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
}
@Test
public void multiPartCookieList() {
String cookieStr =
"name1=myCookieValue1; "+
" name2=myCookieValue2;"+
"name3=myCookieValue3;"+
" name4=myCookieValue4; "+
"name5=myCookieValue5;"+
" name6=myCookieValue6;";
String expectedCookieStr =
"{"+
"\"name1\":\"myCookieValue1\","+
"\"name2\":\"myCookieValue2\","+
"\"name3\":\"myCookieValue3\","+
"\"name4\":\"myCookieValue4\","+
"\"name5\":\"myCookieValue5\","+
"\"name6\":\"myCookieValue6\""+
"}";
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
}
@Test
public void convertCookieListWithNullValueToString() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("key", JSONObject.NULL);
String cookieToStr = CookieList.toString(jsonObject);
assertTrue("toString() should be empty", "".equals(cookieToStr));
}
@Test
public void convertCookieListToString() {
String cookieStr =
"name1=myCookieValue1; "+
" name2=myCookieValue2;"+
"name3=myCookieValue3;"+
" name4=myCookieValue4; "+
"name5=myCookieValue5;"+
" name6=myCookieValue6;";
String expectedCookieStr =
"{"+
"\"name1\":\"myCookieValue1\","+
"\"name2\":\"myCookieValue2\","+
"\"name3\":\"myCookieValue3\","+
"\"name4\":\"myCookieValue4\","+
"\"name5\":\"myCookieValue5\","+
"\"name6\":\"myCookieValue6\""+
"}";
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
String cookieToStr = CookieList.toString(jsonObject);
JSONObject finalJsonObject = CookieList.toJSONObject(cookieToStr);
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
}
@Test
public void convertEncodedCookieListToString() {
String cookieStr =
"name1=myCookieValue1; "+
" name2=my+Cookie+Value+2;"+
"name3=my%2BCookie%26Value%3B3%3D;"+
" name4=my%25CookieValue4; "+
"name5=myCookieValue5;"+
" name6=myCookieValue6;";
String expectedCookieStr =
"{"+
"\"name1\":\"myCookieValue1\","+
"\"name2\":\"my Cookie Value 2\","+
"\"name3\":\"my+Cookie&Value;3=\","+
"\"name4\":\"my%CookieValue4\","+
"\"name5\":\"myCookieValue5\","+
"\"name6\":\"myCookieValue6\""+
"}";
JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
String cookieToStr = CookieList.toString(jsonObject);
JSONObject finalJsonObject = CookieList.toJSONObject(cookieToStr);
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
}
}