CookieParser implements Parser<Map<String, Cookie>> { @Override public Map<String, Cookie> parse(final String input) throws MalformedInputException { Map<String, Cookie> cookies = new HashMap<>(); String[] cookiesStr = input.split(COOKIE_SEPARATOR); for (int i = 0; i < cookiesStr.length; i++) { String[] cookieValues = cookiesStr[i].split(VALUE_SEPARATOR, 2); String cookieName = cookieValues[0].trim(); if (cookieValues.length > 1 && cookieName.length() > 0) { Cookie cookie = new Cookie(cookieName, StringUtilities.urlDecode(cookieValues[1])); cookies.put(cookie.getName(), cookie); } } return cookies; }  @Override Map<String, Cookie> parse(final String input);  }
@Test public void shouldParseCookieHavingSpaceInValue() throws MalformedInputException { String value = "value containing spaces"; Map<String, Cookie> cookies = cookieParser.parse("name=" + value); assertThat(cookies, hasKey("name")); assertThat(cookies.get("name").getValue(), is(value)); }
@Test public void shouldParseCookieHavingUrlEncodedValue() throws MalformedInputException { String value = "&<>some value"; Map<String, Cookie> cookies = cookieParser.parse("name=" + StringUtilities.urlEncode(value)); assertThat(cookies, hasKey("name")); assertThat(cookies.get("name").getValue(), is(value)); }
@Test public void shouldTrimCookieNameValue() throws MalformedInputException { Map<String, Cookie> cookies = cookieParser.parse(" name ="); assertThat(cookies, hasKey("name")); }
@Test public void shouldParseEmptyValue() throws MalformedInputException { Map<String, Cookie> cookies = cookieParser.parse(""); assertThat(cookies.size(), is(0)); }
@Test public void shouldReturnZeroSizeForInvalidValue() throws MalformedInputException { Map<String, Cookie> cookies = cookieParser.parse("name"); assertThat(cookies.size(), is(0)); }
@Test public void shouldReturnZeroSizeForInvalidKey() throws MalformedInputException { Map<String, Cookie> cookies = cookieParser.parse(" = value"); assertThat(cookies.size(), is(0)); }
@Test public void shouldParseMalformedEmptyValue() throws MalformedInputException { Map<String, Cookie> cookies = cookieParser.parse(" ; "); assertThat(cookies.size(), is(0)); }