public static String encodeSetCookie(HttpCookie cookie)
  {
    if (cookie == null)
    {
      return null;
    }
    StringBuilder sb = new StringBuilder();

    sb.append(cookie.getName()).append("=").append(cookie.getValue());

    if (cookie.getPath() != null)
    {
      sb.append(";Path=").append(cookie.getPath());
    }
    if (cookie.getDomain() != null)
    {
      sb.append(";Domain=").append(cookie.getDomain());
    }
    if (cookie.getPortlist() != null)
    {
      // Port value should be quoted according to RFC 2965 Section 3.2.2.
      sb.append(";Port=\"").append(cookie.getPortlist()).append('"');
    }

    sb.append(";Max-Age=").append(Long.toString(cookie.getMaxAge()));
    sb.append(";Version=").append(Integer.toString(cookie.getVersion()));
    if (cookie.getDiscard())
    {
      sb.append(";Discard");
    }
    if (cookie.getSecure())
    {
      sb.append(";Secure");
    }
    if (cookie.isHttpOnly())
    {
      sb.append(";HttpOnly");
    }

    if (cookie.getComment() != null)
    {
      sb.append(";Comment=").append(cookie.getComment());
    }
    if (cookie.getCommentURL() != null)
    {
      // CommentURL value should be quoted according to RFC 2965 Section 3.2.2.
      sb.append(";CommentURL=\"").append(cookie.getCommentURL()).append('"');
    }

    return sb.toString();
  }
@Test
  public void testCookieAttributeEncoding()
  {
    String encodedCookie = CookieUtil.encodeSetCookie(cookieA);

    Assert.assertTrue(encodedCookie.contains("Domain=.android.com"));
    Assert.assertTrue(encodedCookie.contains("Path=/source/"));
    Assert.assertTrue(encodedCookie.contains("Max-Age=125"));
    Assert.assertTrue(encodedCookie.contains("HttpOnly"));
  }