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:
parent
9ce62b9540
commit
6f5bcb32e5
1 changed files with 61 additions and 10 deletions
|
@ -1,30 +1,50 @@
|
|||
package org.json.junit;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
import org.json.*;
|
||||
import org.junit.Test;
|
||||
|
||||
|
||||
/**
|
||||
* Tests for JSON-Java HTTP.java
|
||||
* See RFC7230
|
||||
* Unit tests for JSON-Java HTTP.java. See RFC7230
|
||||
*/
|
||||
public class HTTPTest {
|
||||
|
||||
/**
|
||||
* Attempt to call HTTP.toJSONObject() with a null string
|
||||
* Expects a NUllPointerException.
|
||||
*/
|
||||
@Test(expected=NullPointerException.class)
|
||||
public void nullHTTPException() {
|
||||
String httpStr = null;
|
||||
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() {
|
||||
String httpStr = "{}";
|
||||
JSONObject jsonObject = new JSONObject(httpStr);
|
||||
try {
|
||||
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
|
||||
public void emptyStringHTTPException() {
|
||||
public void emptyStringHTTPRequest() {
|
||||
String httpStr = "";
|
||||
String expectedHTTPStr = "{\"Request-URI\":\"\",\"Method\":\"\",\"HTTP-Version\":\"\"}";
|
||||
JSONObject jsonObject = HTTP.toJSONObject(httpStr);
|
||||
|
@ -32,6 +52,10 @@ public class HTTPTest {
|
|||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call HTTP.toJSONObject() with a Request-URI, Method,
|
||||
* and HTTP-Version.
|
||||
*/
|
||||
@Test
|
||||
public void simpleHTTPRequest() {
|
||||
String httpStr = "GET /hello.txt HTTP/1.1";
|
||||
|
@ -42,6 +66,10 @@ public class HTTPTest {
|
|||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call HTTP.toJSONObject() with a response string containing a
|
||||
* HTTP-Version, Status-Code, and Reason.
|
||||
*/
|
||||
@Test
|
||||
public void simpleHTTPResponse() {
|
||||
String httpStr = "HTTP/1.1 200 OK";
|
||||
|
@ -52,6 +80,10 @@ public class HTTPTest {
|
|||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call HTTP.toJSONObject() with a full request string including
|
||||
* request headers.
|
||||
*/
|
||||
@Test
|
||||
public void extendedHTTPRequest() {
|
||||
String httpStr =
|
||||
|
@ -70,11 +102,18 @@ public class HTTPTest {
|
|||
"\"Content-Type\":\"text/xml; charset=utf-8\"}";
|
||||
JSONObject jsonObject = HTTP.toJSONObject(httpStr);
|
||||
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\"");
|
||||
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call HTTP.toJSONObject() with a full response string including
|
||||
* response headers.
|
||||
*/
|
||||
@Test
|
||||
public void extendedHTTPResponse() {
|
||||
String httpStr =
|
||||
|
@ -92,6 +131,10 @@ public class HTTPTest {
|
|||
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
|
||||
public void convertHTTPRequestToString() {
|
||||
String httpStr =
|
||||
|
@ -110,8 +153,10 @@ public class HTTPTest {
|
|||
JSONObject jsonObject = HTTP.toJSONObject(httpStr);
|
||||
JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr);
|
||||
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, "\n");
|
||||
JSONObject finalJsonObject = HTTP.toJSONObject(httpToStr);
|
||||
|
@ -119,6 +164,10 @@ public class HTTPTest {
|
|||
Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
|
||||
}
|
||||
|
||||
/**
|
||||
* Call HTTP.toJSONObject() with a full response string including
|
||||
* response headers, then convert it back into an HTTP string.
|
||||
*/
|
||||
@Test
|
||||
public void convertHTTPResponseToString() {
|
||||
String httpStr =
|
||||
|
@ -134,8 +183,10 @@ public class HTTPTest {
|
|||
JSONObject jsonObject = HTTP.toJSONObject(httpStr);
|
||||
JSONObject expectedJsonObject = new JSONObject(expectedHTTPStr);
|
||||
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, "\n");
|
||||
JSONObject finalJsonObject = HTTP.toJSONObject(httpToStr);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue