From 912350ec7526c848f022cb4fb1d48e262fe3ad7f Mon Sep 17 00:00:00 2001 From: stleary Date: Mon, 27 Apr 2015 14:56:01 -0500 Subject: [PATCH] 96.5% coverage --- CookieListTest.java | 114 +++++++++++++++++++++++++++++--------------- 1 file changed, 75 insertions(+), 39 deletions(-) diff --git a/CookieListTest.java b/CookieListTest.java index 9edc9ad..0eaef71 100644 --- a/CookieListTest.java +++ b/CookieListTest.java @@ -1,7 +1,5 @@ package org.json.junit; -import java.util.*; - import static org.junit.Assert.*; import org.json.*; @@ -10,17 +8,19 @@ import org.junit.Test; /** * HTTP cookie specification: RFC6265 * - * A cookie list is a JSONObject whose members are cookie name/value pairs. - * Entries are unescaped while being added, and escaped in the toString() - * method. Unescaping means to convert %hh hex strings to the ascii equivalent - * and converting '+' to ' '. Escaping converts '+', '%', '=', ';', - * and ascii control chars to %hh hex strings. + * A cookie list is a JSONObject whose members are presumed to be cookie + * name/value pairs. Entries are unescaped while being added, and escaped in + * 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. - * - CookieList expects multiple name/value pairs as input; Cookie allows the + * 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 { @@ -68,36 +68,66 @@ public class CookieListTest { } @Test - public void multiPartCookieList() { - String cookieStr = - "PH=deleted; "+ - " expires=Wed, 19-Mar-2014 17:53:53 GMT;"+ - "path=/; "+ - " domain=.yahoo.com;"; - String expectedCookieStr = - "{\"path\":\"/\","+ - "\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+ - "\"domain\":\".yahoo.com\","+ - "\"PH\":\"deleted\"}"; + 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 + 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 public void convertCookieListToString() { String cookieStr = - "PH=deleted; "+ - " expires=Wed, 19-Mar-2014 17:53:53 GMT;"+ - "path=/; "+ - " domain=.yahoo.com;"+ - "thisWont=beIncluded;"; + "name1=myCookieValue1; "+ + " name2=myCookieValue2;"+ + "name3=myCookieValue3;"+ + " name4=myCookieValue4; "+ + "name5=myCookieValue5;"+ + " name6=myCookieValue6;"; String expectedCookieStr = - "{\"path\":\"/\","+ - "\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+ - "\"domain\":\".yahoo.com\","+ - "\"thisWont\":\"beIncluded\","+ - "\"PH\":\"deleted\"}"; + "{"+ + "\"name1\":\"myCookieValue1\","+ + "\"name2\":\"myCookieValue2\","+ + "\"name3\":\"myCookieValue3\","+ + "\"name4\":\"myCookieValue4\","+ + "\"name5\":\"myCookieValue5\","+ + "\"name6\":\"myCookieValue6\""+ + "}"; JSONObject jsonObject = CookieList.toJSONObject(cookieStr); JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); String cookieToStr = CookieList.toString(jsonObject); @@ -106,18 +136,24 @@ public class CookieListTest { Util.compareActualVsExpectedJsonObjects(finalJsonObject,expectedJsonObject); } - @Test + @Test public void convertEncodedCookieListToString() { String cookieStr = - "PH=deleted; "+ - " expires=Wed,+19-Mar-2014+17:53:53+GMT;"+ - "path=/%2Bthis/is%26/a/spec%3Bsegment%3D; "+ - " domain=.yahoo.com;"; + "name1=myCookieValue1; "+ + " name2=my+Cookie+Value+2;"+ + "name3=my%2BCookie%26Value%3B3%3D;"+ + " name4=my%25CookieValue4; "+ + "name5=myCookieValue5;"+ + " name6=myCookieValue6;"; String expectedCookieStr = - "{\"path\":\"/+this/is&/a/spec;segment=\","+ - "\"expires\":\"Wed, 19-Mar-2014 17:53:53 GMT\","+ - "\"domain\":\".yahoo.com\","+ - "\"PH\":\"deleted\"}"; + "{"+ + "\"name1\":\"myCookieValue1\","+ + "\"name2\":\"my Cookie Value 2\","+ + "\"name3\":\"my+Cookie&Value;3=\","+ + "\"name4\":\"my%CookieValue4\","+ + "\"name5\":\"myCookieValue5\","+ + "\"name6\":\"myCookieValue6\""+ + "}"; JSONObject jsonObject = CookieList.toJSONObject(cookieStr); JSONObject expectedJsonObject = new JSONObject(expectedCookieStr); String cookieToStr = CookieList.toString(jsonObject);