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

Merge pull request #7 from stleary/Rework-CookieListTest

Rework cookie list test
This commit is contained in:
Sean Leary 2015-04-27 14:56:59 -05:00
commit 31cadbd810

View file

@ -1,29 +1,43 @@
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.*;
import org.junit.Test; import org.junit.Test;
/** /**
* Tests for JSON-Java CookieList.java * HTTP cookie specification: RFC6265
* The main differences between Cookie and CookieList appears to be that *
* CookieList does not treat the initial name/value pair different than * A cookie list is a JSONObject whose members are presumed to be cookie
* the other segments, and does not handle "secure". * name/value pairs. Entries are unescaped while being added, and escaped in
* Therefore the tests will be similar, but not identical. * 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 { public class CookieListTest {
@Test(expected=NullPointerException.class) @Test(expected=NullPointerException.class)
public void nullCookieListException() { public void nullCookieListException() {
/**
* Attempts to create a CookieList from a null string
*/
String cookieStr = null; String cookieStr = null;
CookieList.toJSONObject(cookieStr); CookieList.toJSONObject(cookieStr);
} }
@Test(expected=JSONException.class) @Test(expected=JSONException.class)
public void malFormedCookieListException() { public void malFormedCookieListException() {
/**
* Attempts to create a CookieList from a malformed string
*/
String cookieStr = "thisCookieHasNoEqualsChar"; String cookieStr = "thisCookieHasNoEqualsChar";
CookieList.toJSONObject(cookieStr); CookieList.toJSONObject(cookieStr);
} }
@ -31,6 +45,7 @@ public class CookieListTest {
@Test(expected=JSONException.class) @Test(expected=JSONException.class)
public void emptyStringCookieList() { public void emptyStringCookieList() {
/** /**
* Creates a CookieList from an empty string.
* Cookie throws an exception, but CookieList does not * Cookie throws an exception, but CookieList does not
*/ */
String cookieStr = ""; String cookieStr = "";
@ -42,6 +57,9 @@ public class CookieListTest {
@Test @Test
public void simpleCookieList() { public void simpleCookieList() {
/**
* The simplest cookie is a name/value pair with no delimiter
*/
String cookieStr = "SID=31d4d96e407aad42"; String cookieStr = "SID=31d4d96e407aad42";
String expectedCookieStr = "{\"SID\":\"31d4d96e407aad42\"}"; String expectedCookieStr = "{\"SID\":\"31d4d96e407aad42\"}";
JSONObject jsonObject = CookieList.toJSONObject(cookieStr); JSONObject jsonObject = CookieList.toJSONObject(cookieStr);
@ -49,37 +67,67 @@ public class CookieListTest {
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); 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 @Test
public void multiPartCookieList() { public void multiPartCookieList() {
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; "+
"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\","+
"\"PH\":\"deleted\"}"; "\"name3\":\"myCookieValue3\","+
"\"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);
Util.compareActualVsExpectedJsonObjects(jsonObject,expectedJsonObject); 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);
@ -91,15 +139,21 @@ public class CookieListTest {
@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);