Skip to content

Commit 6f5bcb3

Browse files
committed
Unit tests for JSON-Java HTTP.java. See RFC7230
1 parent 9ce62b9 commit 6f5bcb3

1 file changed

Lines changed: 61 additions & 10 deletions

File tree

HTTPTest.java

Lines changed: 61 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,61 @@
11
package org.json.junit;
22

3+
import static org.junit.Assert.*;
4+
35
import org.json.*;
46
import org.junit.Test;
57

68

79
/**
8-
* Tests for JSON-Java HTTP.java
9-
* See RFC7230
10+
* Unit tests for JSON-Java HTTP.java. See RFC7230
1011
*/
1112
public class HTTPTest {
1213

14+
/**
15+
* Attempt to call HTTP.toJSONObject() with a null string
16+
* Expects a NUllPointerException.
17+
*/
1318
@Test(expected=NullPointerException.class)
1419
public void nullHTTPException() {
1520
String httpStr = null;
1621
HTTP.toJSONObject(httpStr);
1722
}
1823

19-
@Test(expected=JSONException.class)
24+
/**
25+
* Attempt to call HTTP.toJSONObject() with a string containing
26+
* an empty object. Expects a JSONException.
27+
*/
28+
@Test
2029
public void notEnoughHTTPException() {
2130
String httpStr = "{}";
2231
JSONObject jsonObject = new JSONObject(httpStr);
23-
HTTP.toString(jsonObject);
32+
try {
33+
HTTP.toString(jsonObject);
34+
assertTrue("Expected to throw exception", false);
35+
} catch (JSONException e) {
36+
assertTrue("Expecting an exception message",
37+
"Not enough material for an HTTP header.".equals(e.getMessage()));
38+
}
2439
}
2540

41+
/**
42+
* Calling HTTP.toJSONObject() with an empty string will result in a
43+
* populated JSONObject with keys but no values for Request-URI, Method,
44+
* and HTTP-Version.
45+
*/
2646
@Test
27-
public void emptyStringHTTPException() {
47+
public void emptyStringHTTPRequest() {
2848
String httpStr = "";
2949
String expectedHTTPStr = "{\"Request-URI\":\"\",\"Method\":\"\",\"HTTP-Version\":\"\"}";
3050
JSONObject jsonObject = HTTP.toJSONObject(httpStr);
3151
JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr);
3252
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
3353
}
3454

55+
/**
56+
* Call HTTP.toJSONObject() with a Request-URI, Method,
57+
* and HTTP-Version.
58+
*/
3559
@Test
3660
public void simpleHTTPRequest() {
3761
String httpStr = "GET /hello.txt HTTP/1.1";
@@ -42,6 +66,10 @@ public void simpleHTTPRequest() {
4266
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
4367
}
4468

69+
/**
70+
* Call HTTP.toJSONObject() with a response string containing a
71+
* HTTP-Version, Status-Code, and Reason.
72+
*/
4573
@Test
4674
public void simpleHTTPResponse() {
4775
String httpStr = "HTTP/1.1 200 OK";
@@ -52,6 +80,10 @@ public void simpleHTTPResponse() {
5280
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
5381
}
5482

83+
/**
84+
* Call HTTP.toJSONObject() with a full request string including
85+
* request headers.
86+
*/
5587
@Test
5688
public void extendedHTTPRequest() {
5789
String httpStr =
@@ -70,11 +102,18 @@ public void extendedHTTPRequest() {
70102
"\"Content-Type\":\"text/xml; charset=utf-8\"}";
71103
JSONObject jsonObject = HTTP.toJSONObject(httpStr);
72104
JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr);
73-
// not too easy for JSONObject to parse a string with embedded quotes
105+
/**
106+
* Not too easy for JSONObject to parse a string with embedded quotes.
107+
* For the sake of the test, add it here.
108+
*/
74109
expectedJsonObject.put("SOAPAction","\"http://clearforest.com/Enlighten\"");
75110
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
76111
}
77112

113+
/**
114+
* Call HTTP.toJSONObject() with a full response string including
115+
* response headers.
116+
*/
78117
@Test
79118
public void extendedHTTPResponse() {
80119
String httpStr =
@@ -92,6 +131,10 @@ public void extendedHTTPResponse() {
92131
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
93132
}
94133

134+
/**
135+
* Call HTTP.toJSONObject() with a full POST request string including
136+
* response headers, then convert it back into an HTTP string.
137+
*/
95138
@Test
96139
public void convertHTTPRequestToString() {
97140
String httpStr =
@@ -110,15 +153,21 @@ public void convertHTTPRequestToString() {
110153
JSONObject jsonObject = HTTP.toJSONObject(httpStr);
111154
JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr);
112155
String httpToStr = HTTP.toString(jsonObject);
113-
// JSONObject objects to crlfs and any trailing chars
114-
// httpToStr = httpToStr.replaceAll("(\r\n\r\n)", "");
156+
/**
157+
* JSONObject objects to crlfs and any trailing chars.
158+
* For the sake of the test, simplify the resulting string
159+
*/
115160
httpToStr = httpToStr.replaceAll("("+HTTP.CRLF+HTTP.CRLF+")", "");
116161
httpToStr = httpToStr.replaceAll(HTTP.CRLF, "\n");
117162
JSONObject finalJsonObject = HTTP.toJSONObject(httpToStr);
118163
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
119164
Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
120165
}
121166

167+
/**
168+
* Call HTTP.toJSONObject() with a full response string including
169+
* response headers, then convert it back into an HTTP string.
170+
*/
122171
@Test
123172
public void convertHTTPResponseToString() {
124173
String httpStr =
@@ -134,8 +183,10 @@ public void convertHTTPResponseToString() {
134183
JSONObject jsonObject = HTTP.toJSONObject(httpStr);
135184
JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr);
136185
String httpToStr = HTTP.toString(jsonObject);
137-
// JSONObject objects to crlfs and any trailing chars
138-
// httpToStr = httpToStr.replaceAll("(\r\n\r\n)", "");
186+
/**
187+
* JSONObject objects to crlfs and any trailing chars.
188+
* For the sake of the test, simplify the resulting string
189+
*/
139190
httpToStr = httpToStr.replaceAll("("+HTTP.CRLF+HTTP.CRLF+")", "");
140191
httpToStr = httpToStr.replaceAll(HTTP.CRLF, "\n");
141192
JSONObject finalJsonObject = HTTP.toJSONObject(httpToStr);

0 commit comments

Comments
 (0)