@Test public void testSetCookiesInvalidValue() { String template = "some-cookie=some-cookie-value; " + "Invalid=value"; IllegalArgumentException ex = assertThrows(IllegalArgumentException.class, () -> SetCookie.parse(template)); assertThat(ex.getMessage(), is("Unexpected Set-Cookie part: Invalid")); }
public static SetCookie parse(String setCookie) { String[] cookieParts = setCookie.split(PARAM_SEPARATOR); String nameAndValue = cookieParts[0]; int equalsIndex = nameAndValue.indexOf('='); String name = nameAndValue.substring(0, equalsIndex); String value = nameAndValue.length() == equalsIndex ? null : nameAndValue.substring(equalsIndex + 1); Builder builder = builder(name, value); for (int i = 1; i < cookieParts.length; i++) { String cookiePart = cookieParts[i]; equalsIndex = cookiePart.indexOf('='); String partName; String partValue; if (equalsIndex > -1) { partName = cookiePart.substring(0, equalsIndex); partValue = cookiePart.length() == equalsIndex ? null : cookiePart.substring(equalsIndex + 1); } else { partName = cookiePart; partValue = null; } switch (partName.toLowerCase()) { case "expires": hasValue(partName, partValue); builder.expires(Http.DateTime.parse(partValue)); break; case "max-age": hasValue(partName, partValue); builder.maxAge(Duration.ofSeconds(Long.parseLong(partValue))); break; case "domain": hasValue(partName, partValue); builder.domain(partValue); break; case "path": hasValue(partName, partValue); builder.path(partValue); break; case "secure": hasNoValue(partName, partValue); builder.secure(true); break; case "httponly": hasNoValue(partName, partValue); builder.httpOnly(true); break; default: throw new IllegalArgumentException("Unexpected Set-Cookie part: " + partName); } } return builder.build(); }
SetCookie { public static SetCookie parse(String setCookie) { String[] cookieParts = setCookie.split(PARAM_SEPARATOR); String nameAndValue = cookieParts[0]; int equalsIndex = nameAndValue.indexOf('='); String name = nameAndValue.substring(0, equalsIndex); String value = nameAndValue.length() == equalsIndex ? null : nameAndValue.substring(equalsIndex + 1); Builder builder = builder(name, value); for (int i = 1; i < cookieParts.length; i++) { String cookiePart = cookieParts[i]; equalsIndex = cookiePart.indexOf('='); String partName; String partValue; if (equalsIndex > -1) { partName = cookiePart.substring(0, equalsIndex); partValue = cookiePart.length() == equalsIndex ? null : cookiePart.substring(equalsIndex + 1); } else { partName = cookiePart; partValue = null; } switch (partName.toLowerCase()) { case "expires": hasValue(partName, partValue); builder.expires(Http.DateTime.parse(partValue)); break; case "max-age": hasValue(partName, partValue); builder.maxAge(Duration.ofSeconds(Long.parseLong(partValue))); break; case "domain": hasValue(partName, partValue); builder.domain(partValue); break; case "path": hasValue(partName, partValue); builder.path(partValue); break; case "secure": hasNoValue(partName, partValue); builder.secure(true); break; case "httponly": hasNoValue(partName, partValue); builder.httpOnly(true); break; default: throw new IllegalArgumentException("Unexpected Set-Cookie part: " + partName); } } return builder.build(); } }
SetCookie { public static SetCookie parse(String setCookie) { String[] cookieParts = setCookie.split(PARAM_SEPARATOR); String nameAndValue = cookieParts[0]; int equalsIndex = nameAndValue.indexOf('='); String name = nameAndValue.substring(0, equalsIndex); String value = nameAndValue.length() == equalsIndex ? null : nameAndValue.substring(equalsIndex + 1); Builder builder = builder(name, value); for (int i = 1; i < cookieParts.length; i++) { String cookiePart = cookieParts[i]; equalsIndex = cookiePart.indexOf('='); String partName; String partValue; if (equalsIndex > -1) { partName = cookiePart.substring(0, equalsIndex); partValue = cookiePart.length() == equalsIndex ? null : cookiePart.substring(equalsIndex + 1); } else { partName = cookiePart; partValue = null; } switch (partName.toLowerCase()) { case "expires": hasValue(partName, partValue); builder.expires(Http.DateTime.parse(partValue)); break; case "max-age": hasValue(partName, partValue); builder.maxAge(Duration.ofSeconds(Long.parseLong(partValue))); break; case "domain": hasValue(partName, partValue); builder.domain(partValue); break; case "path": hasValue(partName, partValue); builder.path(partValue); break; case "secure": hasNoValue(partName, partValue); builder.secure(true); break; case "httponly": hasNoValue(partName, partValue); builder.httpOnly(true); break; default: throw new IllegalArgumentException("Unexpected Set-Cookie part: " + partName); } } return builder.build(); } private  SetCookie(Builder builder); }
SetCookie { public static SetCookie parse(String setCookie) { String[] cookieParts = setCookie.split(PARAM_SEPARATOR); String nameAndValue = cookieParts[0]; int equalsIndex = nameAndValue.indexOf('='); String name = nameAndValue.substring(0, equalsIndex); String value = nameAndValue.length() == equalsIndex ? null : nameAndValue.substring(equalsIndex + 1); Builder builder = builder(name, value); for (int i = 1; i < cookieParts.length; i++) { String cookiePart = cookieParts[i]; equalsIndex = cookiePart.indexOf('='); String partName; String partValue; if (equalsIndex > -1) { partName = cookiePart.substring(0, equalsIndex); partValue = cookiePart.length() == equalsIndex ? null : cookiePart.substring(equalsIndex + 1); } else { partName = cookiePart; partValue = null; } switch (partName.toLowerCase()) { case "expires": hasValue(partName, partValue); builder.expires(Http.DateTime.parse(partValue)); break; case "max-age": hasValue(partName, partValue); builder.maxAge(Duration.ofSeconds(Long.parseLong(partValue))); break; case "domain": hasValue(partName, partValue); builder.domain(partValue); break; case "path": hasValue(partName, partValue); builder.path(partValue); break; case "secure": hasNoValue(partName, partValue); builder.secure(true); break; case "httponly": hasNoValue(partName, partValue); builder.httpOnly(true); break; default: throw new IllegalArgumentException("Unexpected Set-Cookie part: " + partName); } } return builder.build(); } private  SetCookie(Builder builder); static Builder builder(String name, String value); static SetCookie parse(String setCookie); static SetCookie create(String name, String value); @Override String toString(); }
SetCookie { public static SetCookie parse(String setCookie) { String[] cookieParts = setCookie.split(PARAM_SEPARATOR); String nameAndValue = cookieParts[0]; int equalsIndex = nameAndValue.indexOf('='); String name = nameAndValue.substring(0, equalsIndex); String value = nameAndValue.length() == equalsIndex ? null : nameAndValue.substring(equalsIndex + 1); Builder builder = builder(name, value); for (int i = 1; i < cookieParts.length; i++) { String cookiePart = cookieParts[i]; equalsIndex = cookiePart.indexOf('='); String partName; String partValue; if (equalsIndex > -1) { partName = cookiePart.substring(0, equalsIndex); partValue = cookiePart.length() == equalsIndex ? null : cookiePart.substring(equalsIndex + 1); } else { partName = cookiePart; partValue = null; } switch (partName.toLowerCase()) { case "expires": hasValue(partName, partValue); builder.expires(Http.DateTime.parse(partValue)); break; case "max-age": hasValue(partName, partValue); builder.maxAge(Duration.ofSeconds(Long.parseLong(partValue))); break; case "domain": hasValue(partName, partValue); builder.domain(partValue); break; case "path": hasValue(partName, partValue); builder.path(partValue); break; case "secure": hasNoValue(partName, partValue); builder.secure(true); break; case "httponly": hasNoValue(partName, partValue); builder.httpOnly(true); break; default: throw new IllegalArgumentException("Unexpected Set-Cookie part: " + partName); } } return builder.build(); } private  SetCookie(Builder builder); static Builder builder(String name, String value); static SetCookie parse(String setCookie); static SetCookie create(String name, String value); @Override String toString();  }