### Question:
RFC6265CookieSpec implements CookieSpec { @Override public List<Header> formatCookies(final List<Cookie> cookies) { Args.notEmpty(cookies, "List of cookies"); final List<? extends Cookie> sortedCookies; if (cookies.size() > 1) { sortedCookies = new ArrayList<>(cookies); Collections.sort(sortedCookies, CookiePriorityComparator.INSTANCE); } else { sortedCookies = cookies; } final CharArrayBuffer buffer = new CharArrayBuffer(20 * sortedCookies.size()); buffer.append("Cookie"); buffer.append(": "); for (int n = 0; n < sortedCookies.size(); n++) { final Cookie cookie = sortedCookies.get(n); if (n > 0) { buffer.append(PARAM_DELIMITER); buffer.append(' '); } buffer.append(cookie.getName()); final String s = cookie.getValue(); if (s != null) { buffer.append(EQUAL_CHAR); if (containsSpecialChar(s)) { buffer.append(DQUOTE_CHAR); for (int i = 0; i < s.length(); i++) { final char ch = s.charAt(i); if (ch == DQUOTE_CHAR || ch == ESCAPE_CHAR) { buffer.append(ESCAPE_CHAR); } buffer.append(ch); } buffer.append(DQUOTE_CHAR); } else { buffer.append(s); } } } final List<Header> headers = new ArrayList<>(1); try { headers.add(new BufferedHeader(buffer)); } catch (final ParseException ignore) { } return headers; } protected  RFC6265CookieSpec(final CommonCookieAttributeHandler... handlers); @Override final List<Cookie> parse(final Header header, final CookieOrigin origin); @Override final void validate(final Cookie cookie, final CookieOrigin origin); @Override final boolean match(final Cookie cookie, final CookieOrigin origin); @Override List<Header> formatCookies(final List<Cookie> cookies);  }### Answer:
@Test public void testFormatCookiesBasics() throws Exception { final Cookie cookie1 = new BasicClientCookie("name1", "value"); final RFC6265CookieSpec cookiespec = new RFC6265CookieSpec(); final List<Header> headers = cookiespec.formatCookies(Arrays.asList(cookie1)); Assert.assertNotNull(headers); Assert.assertEquals(1, headers.size()); final Header header = headers.get(0); Assert.assertEquals("Cookie", header.getName()); Assert.assertEquals("name1=value", header.getValue()); }
@Test public void testFormatCookiesIllegalCharsInValue() throws Exception { final Cookie cookie1 = new BasicClientCookie("name1", "value"); final Cookie cookie2 = new BasicClientCookie("name2", "some value"); final Cookie cookie3 = new BasicClientCookie("name3", "\"\\\""); final RFC6265CookieSpec cookiespec = new RFC6265CookieSpec(); final List<Header> headers = cookiespec.formatCookies(Arrays.asList(cookie1, cookie2, cookie3)); Assert.assertNotNull(headers); Assert.assertEquals(1, headers.size()); final Header header = headers.get(0); Assert.assertEquals("Cookie", header.getName()); Assert.assertEquals("name1=value; name2=\"some value\"; name3=\"\\\"\\\\\\\"\"", header.getValue()); }