@NonNull
    private String encodeCookie(@NonNull Cookie cookie) {
        StringBuilder sb = new StringBuilder();
        sb.append(cookie.getName()).append(EQUAL).append(cookie.getValue());
        if (isMaxAgeSet(cookie)) {
            sb.append(SEMICOLON).append(SPACE).append(Cookie.ATTRIBUTE_MAX_AGE).append(EQUAL).append(cookie.getMaxAge());
            sb.append(SEMICOLON).append(SPACE).append(Cookie.ATTRIBUTE_EXPIRES).append(EQUAL).append(expires(cookie.getMaxAge()));
        }
        if (StringUtils.isNotEmpty(cookie.getPath())) {
            sb.append(SEMICOLON).append(SPACE).append(Cookie.ATTRIBUTE_PATH).append(EQUAL).append(cookie.getPath());
        }
        if (StringUtils.isNotEmpty(cookie.getDomain())) {
            sb.append(SEMICOLON).append(SPACE).append(Cookie.ATTRIBUTE_DOMAIN).append(EQUAL).append(cookie.getDomain());
        }
        if (cookie.isSecure()) {
            sb.append(SEMICOLON).append(SPACE).append(Cookie.ATTRIBUTE_SECURE);
        }
        if (cookie.isHttpOnly()) {
            sb.append(SEMICOLON).append(SPACE).append(Cookie.ATTRIBUTE_HTTP_ONLY);
        }
        cookie.getSameSite().ifPresent(sameSite ->
                sb.append(SEMICOLON).append(SPACE).append(Cookie.ATTRIBUTE_SAME_SITE).append(EQUAL).append(sameSite));
        return sb.toString();
    }
@Test
    void encodeCookie() {
        ServerCookieEncoder cookieEncoder = new DefaultServerCookieEncoder();
        Cookie cookie = Cookie.of("SID", "31d4d96e407aad42").path("/").domain("example.com");
        assertEquals("SID=31d4d96e407aad42; Path=/; Domain=example.com", cookieEncoder.encode(cookie).get(0));

        cookie = Cookie.of("SID", "31d4d96e407aad42").path("/").domain("example.com").sameSite(SameSite.Strict);
        assertEquals("SID=31d4d96e407aad42; Path=/; Domain=example.com; SameSite=Strict", cookieEncoder.encode(cookie).get(0));

        cookie = Cookie.of("SID", "31d4d96e407aad42").path("/").secure().httpOnly();
        assertEquals("SID=31d4d96e407aad42; Path=/; Secure; HttpOnly", cookieEncoder.encode(cookie).get(0));

        long maxAge = 2592000;
        String expected = "id=a3fWa; Max-Age=2592000; " + Cookie.ATTRIBUTE_EXPIRES + "=" + expires(maxAge);
        String expected2 = "id=a3fWa; Max-Age=2592000; " + Cookie.ATTRIBUTE_EXPIRES + "=" + expires(maxAge + 1); // To prevent flakiness
        cookie = Cookie.of("id", "a3fWa").maxAge(maxAge);
        String result = cookieEncoder.encode(cookie).get(0);
        assertTrue(expected.equals(result) || expected2.equals(result));
    }