java-tutorials/json/src/test/java/com/baeldung/jsonjava/CookieIntegrationTest.java
ramansahasi 15f18bbb83 BAEL 1269 Intro to JSON-JAVA (#3493)
* Final commit

* Made changes as per last review

* Moved from core-java to json module
2018-02-01 10:57:48 +01:00

30 lines
1.0 KiB
Java

package com.baeldung.jsonjava;
import static org.junit.Assert.assertEquals;
import org.json.Cookie;
import org.json.JSONObject;
import org.junit.Test;
public class CookieIntegrationTest {
@Test
public void givenCookieString_thenConvertToJSONObject() {
String cookie = "username=John Doe; expires=Thu, 18 Dec 2013 12:00:00 UTC; path=/";
JSONObject cookieJO = Cookie.toJSONObject(cookie);
assertEquals("{\"path\":\"/\",\"expires\":\"Thu, 18 Dec 2013 12:00:00 UTC\",\"name\":\"username\",\"value\":\"John Doe\"}", cookieJO.toString());
}
@Test
public void givenJSONObject_thenConvertToCookieString() {
JSONObject cookieJO = new JSONObject();
cookieJO.put("name", "username");
cookieJO.put("value", "John Doe");
cookieJO.put("expires", "Thu, 18 Dec 2013 12:00:00 UTC");
cookieJO.put("path", "/");
String cookie = Cookie.toString(cookieJO);
assertEquals("username=John Doe;expires=Thu, 18 Dec 2013 12:00:00 UTC;path=/", cookie.toString());
}
}