I'm testing out some usages on CookieManager and I noticed that when I pass my deserialized cookie to CookieHandler, the format that CookieHandler uses to put it in cookie manager is not the same that I passed.
import org.springframework.http.HttpHeaders;
import java.io.IOException;
import java.net.*;
import java.util.*;
public class MainTest {
public static void main(String[] args) throws URISyntaxException, IOException {
HttpHeaders headers = new HttpHeaders();
headers.add("Set-Cookie", "TEST=%7B%22keyType%22%3A%22abc%22%2C%22keyValue%22%3A%221.0%22%7D;Max-Age=600;Path=/;Version=0;SameSite=None");
headers.add(HttpHeaders.ACCEPT_LANGUAGE, "en-US,en;q=0.9");
System.out.println("headers-> " + headers);
String uri = "http://test.com/test";
CookieManager cookieHandler = new CookieManager();
CookieHandler.setDefault(cookieHandler);
URI uri1 = new URI(uri);
CookieHandler.getDefault().put(uri1,headers);
Map<String, List<String>> cookies = CookieHandler.getDefault().get(uri1,headers);
System.out.println("returned-> " + cookies);
}
}
And here is the output:
headers-> [Set-Cookie:"TEST=%7B%22keyType%22%3A%22abc%22%2C%22keyValue%22%3A%221.0%22%7D;Max-Age=600;Path=/;Version=0;SameSite=None", Accept-Language:"en-US,en;q=0.9"]
returned-> {Cookie=[$Version="1", TEST="%7B%22keyType%22%3A%22abc%22%2C%22keyValue%22%3A%221.0%22%7D";$Path="/";$Domain="test.com"]}
Why is the format changing? It changes even when I'm passing a serialized cookie too
Your "returned" just shows String representation that results from java.net.HttpCookie#toString
, documentation says it'd stick to RFC 2965:
//copied from java.net.HttpCookie
private String toRFC2965HeaderString() {
StringBuilder sb = new StringBuilder();
sb.append(getName()).append("=\"").append(getValue()).append('"');
if (getPath() != null)
sb.append(";$Path=\"").append(getPath()).append('"');
if (getDomain() != null)
sb.append(";$Domain=\"").append(getDomain()).append('"');
if (getPortlist() != null)
sb.append(";$Port=\"").append(getPortlist()).append('"');
return sb.toString();
}