CookieParser { public String getSpecialCookie(String cookieHTTPHeaderString, String cookieName) { String regEx = ".*(((" + cookieName + "=[^;]*)|(" + cookieName + "=\"[\";]*))|(" + cookieName + "=.*$)).*"; Pattern searchPattern = Pattern.compile(regEx); Matcher matcher = searchPattern.matcher(cookieHTTPHeaderString); return matcher.matches() ? matcher.group(1) : null; } private  CookieParser(); static CookieParser getInstance(); String getSpecialCookie(String cookieHTTPHeaderString, String cookieName);  }
@Test public void shouldReturnACookieByGivenName() { assertEquals("JSESSIONID=07067BCF03EB91C40061E665BADAC3EF", cookieParser.getSpecialCookie("JSESSIONID=07067BCF03EB91C40061E665BADAC3EF; Path=/", "JSESSIONID")); }
@Test public void shouldReturnACookieFromTheEndOfTheCookieString() { assertEquals("Path=/", cookieParser.getSpecialCookie("JSESSIONID=07067BCF03EB91C40061E665BADAC3EF; Path=/", "Path")); }
@Test public void shouldFailIfTheCookieIsNotFound() { assertNull(cookieParser.getSpecialCookie("JSESSIONID=07067BCF03EB91C40061E665BADAC3EF; Path=/","NoCookie")); }