SetCookieField extends HeaderField { public static final SetCookieField fromHeaderValue(String header) { StringTokenizer s = new StringTokenizer(header); NameValue nameValue = NameValue.parse(s.nextToken(";")); String domain = null; String path = null; String expiration = null; Boolean secure = null; Boolean httpOnly = null; while (s.hasMoreTokens()) { String next = s.nextToken(";"); if (next != null) { next = next.trim(); if (next.equalsIgnoreCase("Secure")) { secure = true; } else if (next.equalsIgnoreCase("HttpOnly")) { httpOnly = true; } else { NameValue property = NameValue.parse(next.trim()); String name = property.name.toLowerCase(); if (name.equals("domain")) { domain = property.value; } else if (name.equals("path")) { path = property.value; } else if (name.equals("expires")) { expiration = property.value; } } } } return new SetCookieField(nameValue.name, nameValue.value, domain, path, expiration, secure, httpOnly); } SetCookieField(String name, String value, String domain, String path,
                          DateTimeRFC6265 expiration, Boolean secure, Boolean httpOnly);  SetCookieField(String name, String value, String domain, String path,
                          String expiration, Boolean secure, Boolean httpOnly);  SetCookieField(String name, String value, String domain, String path,
                          String expiration, Boolean secure);  SetCookieField(String name, String value, String domain); static final SetCookieField fromHeaderValue(String header); boolean isSecure(); DateTimeRFC6265 parsedExpiration(); @Override T accept(HeaderFieldVisitor<T> visitor); @Override String name(); @Override String value(); @Override String toString(); boolean isHttpOnly(); final String name; final String value; final String domain; final String path; final String expiration; final Boolean secure; final Boolean httpOnly; }
@Test public void valuesMayBeQuoted() { String value = "foo=\"bar baz\"; path=\"/xyz\"; domain=\"xyz.com\"; Expires=\"Wed, 13-Jan-2021 22:23:01 GMT\""; SetCookieField c = SetCookieField.fromHeaderValue(value); assertEquals("foo", c.name); assertEquals("bar baz", c.value); assertEquals("/xyz", c.path); assertEquals("xyz.com", c.domain); assertEquals("Wed, 13-Jan-2021 22:23:01 GMT", c.expiration); }
@Test public void attributeNamesAreCaseInsentitive() { String[] caseVariations = {"path", "PATH", "pAtH", "PaTh"}; for (String pathAttributeName : caseVariations) { SetCookieField c = SetCookieField.fromHeaderValue("foo=bar; " + pathAttributeName + "=/"); assertEquals("foo", c.name); assertEquals("bar", c.value); assertEquals("/", c.path); } }
@Test public void flagsAreCaseInsensitive() { String[] httpCaseVariations = {"HTTPOnly", "httpONLY", "HttpOnly"}; for (String httpOnlyFlag : httpCaseVariations) { SetCookieField c = SetCookieField.fromHeaderValue("foo=bar; " + httpOnlyFlag); assertEquals("foo", c.name); assertEquals("bar", c.value); assertEquals(true, c.httpOnly); } String[] secureCaseVariations = {"Secure", "secure", "SECURE"}; for (String secureFlag : secureCaseVariations ) { SetCookieField c = SetCookieField.fromHeaderValue("foo=bar; " + secureFlag); assertEquals("foo", c.name); assertEquals("bar", c.value); assertEquals(true, c.secure); } }
@Test public void attributeValuesAreOptional() { String value = "foo=\"bar baz\"; ThisAttributeHasNoValue"; SetCookieField c = SetCookieField.fromHeaderValue(value); assertEquals("foo", c.name); assertEquals("bar baz", c.value); }
@Test public void basicSetCookieFieldWithDomain() { SetCookieField c = SetCookieField.fromHeaderValue("LSID=DQAAAK…Eaem_vYg; Domain=docs.foo.com"); assertSetCookieFieldSame( new SetCookieField("LSID", "DQAAAK…Eaem_vYg", "docs.foo.com"), c ); assertEquals("LSID=DQAAAK…Eaem_vYg; Domain=docs.foo.com;", c.toString()); }
@Test public void full() { SetCookieField c = SetCookieField.fromHeaderValue("SSID=Ap4P….GTEq; Domain=.foo.com; Path=/; Expires=Wed, 13-Jan-2021 22:23:01 GMT; Secure; HttpOnly;"); boolean secure = true; boolean httpOnly = true; assertSetCookieFieldSame( new SetCookieField("SSID", "Ap4P….GTEq", ".foo.com", "/", "Wed, 13-Jan-2021 22:23:01 GMT", secure, httpOnly), c ); assertEquals("SSID=Ap4P….GTEq; Domain=.foo.com; Path=/; Expires=Wed, 13-Jan-2021 22:23:01 GMT; Secure; HttpOnly;", c.toString()); }
@Test public void parseHttpOnlyTrue() { String headerValue = "name=value; HttpOnly;"; SetCookieField setCookieField = SetCookieField.fromHeaderValue(headerValue); assertTrue(setCookieField.httpOnly); }
@Test public void parseHttpOnlyFalse() { String headerValue = "name=value;"; SetCookieField setCookieField = SetCookieField.fromHeaderValue(headerValue); assertNull(setCookieField.httpOnly); }