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

96.5% coverage

This commit is contained in:
stleary 2015-04-27 14:56:01 -05:00
parent 65ae3e663f
commit 912350ec75

View file

@ -1,7 +1,5 @@
package org.json.junit; package org.json.junit;
import java.util.*;
import static org.junit.Assert.*; import static org.junit.Assert.*;
import org.json.*; import org.json.*;
@ -10,17 +8,19 @@ import org.junit.Test;
/** /**
* HTTP cookie specification: RFC6265 * HTTP cookie specification: RFC6265
* *
* A cookie list is a JSONObject whose members are cookie name/value pairs. * A cookie list is a JSONObject whose members are presumed to be cookie
* Entries are unescaped while being added, and escaped in the toString() * name/value pairs. Entries are unescaped while being added, and escaped in
* method. Unescaping means to convert %hh hex strings to the ascii equivalent * the toString() output.
* and converting '+' to ' '. Escaping converts '+', '%', '=', ';', * Unescaping means to convert %hh hex strings to the ascii equivalent
* and ascii control chars to %hh hex strings. * 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 should not be considered as just a list of Cookie objects:
* - CookieList stores a cookie name/value pair as a single entry; Cookie stores * - CookieList stores a cookie name/value pair as a single entry; Cookie stores
* it as 2 entries. * it as 2 entries (key="name" and key="value").
* - CookieList expects multiple name/value pairs as input; Cookie allows the * - CookieList requires multiple name/value pairs as input; Cookie allows the
* 'secure' name with no associated value * 'secure' name with no associated value
* - CookieList has no special handling for attribute name/value pairs.
*/ */
public class CookieListTest { public class CookieListTest {
@ -68,36 +68,66 @@ public class CookieListTest {
} }
@Test @Test
public void multiPartCookieList() { public void simpleCookieListWithDelimiter() {
String cookieStr = /**
"PH=deleted; "+ * The simplest cookie is a name/value pair with a delimiter
" expires=Wed, 19-Mar-2014 17:53:53 GMT;"+ */
"path=/; "+ String cookieStr = "SID=31d4d96e407aad42;";
" domain=.yahoo.com;"; String expectedCookieStr = "{\"SID\":\"31d4d96e407aad42\"}";
String expectedCookieStr =
"{\"path\":\"/\","+
"\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+
"\"domain\":\".yahoo.com\","+
"\"PH\":\"deleted\"}";
JSONObject jsonObject = CookieList.toJSONObject(cookieStr); JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); 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 @Test
public void convertCookieListToString() { public void convertCookieListToString() {
String cookieStr = String cookieStr =
"PH=deleted; "+ "name1=myCookieValue1; "+
" expires=Wed, 19-Mar-2014 17:53:53 GMT;"+ " name2=myCookieValue2;"+
"path=/; "+ "name3=myCookieValue3;"+
" domain=.yahoo.com;"+ " name4=myCookieValue4; "+
"thisWont=beIncluded;"; "name5=myCookieValue5;"+
" name6=myCookieValue6;";
String expectedCookieStr = String expectedCookieStr =
"{\"path\":\"/\","+ "{"+
"\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+ "\"name1\":\"myCookieValue1\","+
"\"domain\":\".yahoo.com\","+ "\"name2\":\"myCookieValue2\","+
"\"thisWont\":\"beIncluded\","+ "\"name3\":\"myCookieValue3\","+
"\"PH\":\"deleted\"}"; "\"name4\":\"myCookieValue4\","+
"\"name5\":\"myCookieValue5\","+
"\"name6\":\"myCookieValue6\""+
"}";
JSONObject jsonObject = CookieList.toJSONObject(cookieStr); JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
String cookieToStr = CookieList.toString(jsonObject); String cookieToStr = CookieList.toString(jsonObject);
@ -106,18 +136,24 @@ public class CookieListTest {
Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject);
} }
@Test @Test
public void convertEncodedCookieListToString() { public void convertEncodedCookieListToString() {
String cookieStr = String cookieStr =
"PH=deleted; "+ "name1=myCookieValue1; "+
" expires=Wed,+19-Mar-2014+17:53:53+GMT;"+ " name2=my+Cookie+Value+2;"+
"path=/%2Bthis/is%26/a/spec%3Bsegment%3D; "+ "name3=my%2BCookie%26Value%3B3%3D;"+
" domain=.yahoo.com;"; " name4=my%25CookieValue4; "+
"name5=myCookieValue5;"+
" name6=myCookieValue6;";
String expectedCookieStr = String expectedCookieStr =
"{\"path\":\"/+this/is&/a/spec;segment=\","+ "{"+
"\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+ "\"name1\":\"myCookieValue1\","+
"\"domain\":\".yahoo.com\","+ "\"name2\":\"my Cookie Value 2\","+
"\"PH\":\"deleted\"}"; "\"name3\":\"my+Cookie&Value;3=\","+
"\"name4\":\"my%CookieValue4\","+
"\"name5\":\"myCookieValue5\","+
"\"name6\":\"myCookieValue6\""+
"}";
JSONObject jsonObject = CookieList.toJSONObject(cookieStr); JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); JSONObject expectedJsonObject = new JSONObject(expectedCookieStr);
String cookieToStr = CookieList.toString(jsonObject); String cookieToStr = CookieList.toString(jsonObject);