1
0
Fork 0
mirror of https://github.com/ethauvin/JSON-java.git synced 2025-06-17 07:50:52 -07:00

Unit tests for JSON-Java HTTP.java. See RFC7230

This commit is contained in:
stleary 2015-08-08 12:10:18 -05:00
parent 9ce62b9540
commit 6f5bcb32e5

View file

@ -1,30 +1,50 @@
package org.json.junit; package org.json.junit;
import static org.junit.Assert.*;
import org.json.*; import org.json.*;
import org.junit.Test; import org.junit.Test;
/** /**
* Tests for JSON-Java HTTP.java * Unit tests for JSON-Java HTTP.java. See RFC7230
* See RFC7230
*/ */
public class HTTPTest { public class HTTPTest {
/**
* Attempt to call HTTP.toJSONObject() with a null string
* Expects a NUllPointerException.
*/
@Test(expected=NullPointerException.class) @Test(expected=NullPointerException.class)
public void nullHTTPException() { public void nullHTTPException() {
String httpStr = null; String httpStr = null;
HTTP.toJSONObject(httpStr); HTTP.toJSONObject(httpStr);
} }
@Test(expected=JSONException.class) /**
* Attempt to call HTTP.toJSONObject() with a string containing
* an empty object. Expects a JSONException.
*/
@Test
public void notEnoughHTTPException() { public void notEnoughHTTPException() {
String httpStr = "{}"; String httpStr = "{}";
JSONObject jsonObject = new JSONObject(httpStr); JSONObject jsonObject = new JSONObject(httpStr);
try {
HTTP.toString(jsonObject); HTTP.toString(jsonObject);
assertTrue("Expected to throw exception", false);
} catch (JSONException e) {
assertTrue("Expecting an exception message",
"Not enough material for an HTTP header.".equals(e.getMessage()));
}
} }
/**
* Calling HTTP.toJSONObject() with an empty string will result in a
* populated JSONObject with keys but no values for Request-URI, Method,
* and HTTP-Version.
*/
@Test @Test
public void emptyStringHTTPException() { public void emptyStringHTTPRequest() {
String httpStr = ""; String httpStr = "";
String expectedHTTPStr = "{\"Request-URI\":\"\",\"Method\":\"\",\"HTTP-Version\":\"\"}"; String expectedHTTPStr = "{\"Request-URI\":\"\",\"Method\":\"\",\"HTTP-Version\":\"\"}";
JSONObject jsonObject = HTTP.toJSONObject(httpStr); JSONObject jsonObject = HTTP.toJSONObject(httpStr);
@ -32,6 +52,10 @@ public class HTTPTest {
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
} }
/**
* Call HTTP.toJSONObject() with a Request-URI, Method,
* and HTTP-Version.
*/
@Test @Test
public void simpleHTTPRequest() { public void simpleHTTPRequest() {
String httpStr = "GET /hello.txt HTTP/1.1"; String httpStr = "GET /hello.txt HTTP/1.1";
@ -42,6 +66,10 @@ public class HTTPTest {
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
} }
/**
* Call HTTP.toJSONObject() with a response string containing a
* HTTP-Version, Status-Code, and Reason.
*/
@Test @Test
public void simpleHTTPResponse() { public void simpleHTTPResponse() {
String httpStr = "HTTP/1.1 200 OK"; String httpStr = "HTTP/1.1 200 OK";
@ -52,6 +80,10 @@ public class HTTPTest {
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
} }
/**
* Call HTTP.toJSONObject() with a full request string including
* request headers.
*/
@Test @Test
public void extendedHTTPRequest() { public void extendedHTTPRequest() {
String httpStr = String httpStr =
@ -70,11 +102,18 @@ public class HTTPTest {
"\"Content-Type\":\"text/xml; charset=utf-8\"}"; "\"Content-Type\":\"text/xml; charset=utf-8\"}";
JSONObject jsonObject = HTTP.toJSONObject(httpStr); JSONObject jsonObject = HTTP.toJSONObject(httpStr);
JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr); JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr);
// not too easy for JSONObject to parse a string with embedded quotes /**
* Not too easy for JSONObject to parse a string with embedded quotes.
* For the sake of the test, add it here.
*/
expectedJsonObject.put("SOAPAction","\"http://clearforest.com/Enlighten\""); expectedJsonObject.put("SOAPAction","\"http://clearforest.com/Enlighten\"");
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
} }
/**
* Call HTTP.toJSONObject() with a full response string including
* response headers.
*/
@Test @Test
public void extendedHTTPResponse() { public void extendedHTTPResponse() {
String httpStr = String httpStr =
@ -92,6 +131,10 @@ public class HTTPTest {
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
} }
/**
* Call HTTP.toJSONObject() with a full POST request string including
* response headers, then convert it back into an HTTP string.
*/
@Test @Test
public void convertHTTPRequestToString() { public void convertHTTPRequestToString() {
String httpStr = String httpStr =
@ -110,8 +153,10 @@ public class HTTPTest {
JSONObject jsonObject = HTTP.toJSONObject(httpStr); JSONObject jsonObject = HTTP.toJSONObject(httpStr);
JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr); JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr);
String httpToStr = HTTP.toString(jsonObject); String httpToStr = HTTP.toString(jsonObject);
// JSONObject objects to crlfs and any trailing chars /**
// httpToStr = httpToStr.replaceAll("(\r\n\r\n)", ""); * JSONObject objects to crlfs and any trailing chars.
* For the sake of the test, simplify the resulting string
*/
httpToStr = httpToStr.replaceAll("("+HTTP.CRLF+HTTP.CRLF+")", ""); httpToStr = httpToStr.replaceAll("("+HTTP.CRLF+HTTP.CRLF+")", "");
httpToStr = httpToStr.replaceAll(HTTP.CRLF, "\n"); httpToStr = httpToStr.replaceAll(HTTP.CRLF, "\n");
JSONObject finalJsonObject = HTTP.toJSONObject(httpToStr); JSONObject finalJsonObject = HTTP.toJSONObject(httpToStr);
@ -119,6 +164,10 @@ public class HTTPTest {
Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
} }
/**
* Call HTTP.toJSONObject() with a full response string including
* response headers, then convert it back into an HTTP string.
*/
@Test @Test
public void convertHTTPResponseToString() { public void convertHTTPResponseToString() {
String httpStr = String httpStr =
@ -134,8 +183,10 @@ public class HTTPTest {
JSONObject jsonObject = HTTP.toJSONObject(httpStr); JSONObject jsonObject = HTTP.toJSONObject(httpStr);
JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr); JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr);
String httpToStr = HTTP.toString(jsonObject); String httpToStr = HTTP.toString(jsonObject);
// JSONObject objects to crlfs and any trailing chars /**
// httpToStr = httpToStr.replaceAll("(\r\n\r\n)", ""); * JSONObject objects to crlfs and any trailing chars.
* For the sake of the test, simplify the resulting string
*/
httpToStr = httpToStr.replaceAll("("+HTTP.CRLF+HTTP.CRLF+")", ""); httpToStr = httpToStr.replaceAll("("+HTTP.CRLF+HTTP.CRLF+")", "");
httpToStr = httpToStr.replaceAll(HTTP.CRLF, "\n"); httpToStr = httpToStr.replaceAll(HTTP.CRLF, "\n");
JSONObject finalJsonObject = HTTP.toJSONObject(httpToStr); JSONObject finalJsonObject = HTTP.toJSONObject(httpToStr);