CookieHeaderProvider implements RuntimeDelegate.HeaderDelegate<Cookie> { @Override public String toString(Cookie cookie) { StringBuilder sb = new StringBuilder(); if (cookie.getVersion() != Cookie.DEFAULT_VERSION) { sb.append(VERSION).append('=').append(cookie.getVersion()).append(';'); } sb.append(cookie.getName()).append('=').append(cookie.getValue()); if (cookie.getPath() != null) { sb.append(';').append(PATH).append('=').append(cookie.getPath()); } if (cookie.getDomain() != null) { sb.append(';').append(DOMAIN).append('=').append(cookie.getDomain()); } if (cookie instanceof NewCookie) { NewCookie newCookie = (NewCookie) cookie; if (newCookie.getMaxAge() != NewCookie.DEFAULT_MAX_AGE) { sb.append(';').append(MAX_AGE).append('=').append(newCookie.getMaxAge()); } if (newCookie.getComment() != null) { sb.append(';').append(COMMENT).append('=').append(newCookie.getComment()); } if (newCookie.getExpiry() != null) { dateFormat.setTimeZone(TimeZone.getTimeZone(GMT_TIMEZONE)); sb.append(';').append(EXPIRES).append('=').append(dateFormat.format(newCookie.getExpiry())); } if (newCookie.isSecure()) { sb.append(';').append(SECURE); } if (newCookie.isHttpOnly()) { sb.append(';').append(HTTP_ONLY); } } return sb.toString(); }  @Override Cookie fromString(String cookieValue); @Override String toString(Cookie cookie);  }
@Test public void testToString() throws Exception { String expectedString = "Version=2;Application=msf4j;Path=/carbon;Domain=wso2;MaxAge=50;Comment=TestOnly;" + "Expires=Sun, 06 Nov 1994 08:49:37 GMT;Secure;HttpOnly"; Cookie cookie = new NewCookie("Application", "msf4j", "/carbon", "wso2", 2, "TestOnly", 50, new Date (784111777000L), true, true); CookieHeaderProvider cookieHeaderProvider = new CookieHeaderProvider(); String cookieString = cookieHeaderProvider.toString(cookie); assertNotNull(cookieString); assertEquals(cookieString, expectedString); }