|
15 | 15 | */ |
16 | 16 | package io.netty.handler.codec.http; |
17 | 17 |
|
18 | | -import static org.junit.Assert.*; |
| 18 | +import static org.junit.Assert.assertEquals; |
| 19 | +import static org.junit.Assert.assertNotNull; |
| 20 | +import static org.junit.Assert.assertTrue; |
| 21 | +import static org.junit.Assert.fail; |
19 | 22 |
|
20 | 23 | import java.text.DateFormat; |
| 24 | +import java.text.ParseException; |
21 | 25 | import java.util.Date; |
22 | 26 | import java.util.List; |
| 27 | +import java.util.regex.Matcher; |
| 28 | +import java.util.regex.Pattern; |
23 | 29 |
|
24 | 30 | import org.junit.Test; |
25 | 31 |
|
@@ -56,38 +62,49 @@ public void testEncodingSingleCookieV0() { |
56 | 62 | } |
57 | 63 | } |
58 | 64 |
|
| 65 | + private void matchCookie(String cookieValue, String pattern, int maxAge) throws ParseException { |
| 66 | + Matcher matcher = Pattern.compile(pattern).matcher(cookieValue); |
| 67 | + assertTrue(matcher.find()); |
| 68 | + Date expiresDate = HttpHeaderDateFormat.get().parse(matcher.group(1)); |
| 69 | + long diff = (expiresDate.getTime() - System.currentTimeMillis()) / 1000; |
| 70 | + // 1 sec should be fine |
| 71 | + assertTrue(Math.abs(diff - maxAge) <= 1); |
| 72 | + } |
| 73 | + |
59 | 74 | @Test |
60 | | - public void testEncodingSingleCookieV1() { |
61 | | - String result = "myCookie=myValue; Max-Age=50; Path=\"/apathsomewhere\"; " + |
| 75 | + public void testEncodingSingleCookieV1() throws ParseException { |
| 76 | + int maxAge = 50; |
| 77 | + String result = "myCookie=myValue; Max-Age=" + maxAge + "; Expires=(.+?); Path=\"/apathsomewhere\"; " + |
62 | 78 | "Domain=.adomainsomewhere; Secure; Comment=\"this is a Comment\"; Version=1"; |
63 | 79 | Cookie cookie = new DefaultCookie("myCookie", "myValue"); |
64 | 80 | cookie.setVersion(1); |
65 | 81 | cookie.setComment("this is a Comment"); |
66 | 82 | cookie.setDomain(".adomainsomewhere"); |
67 | | - cookie.setMaxAge(50); |
| 83 | + cookie.setMaxAge(maxAge); |
68 | 84 | cookie.setPath("/apathsomewhere"); |
69 | 85 | cookie.setSecure(true); |
70 | 86 | String encodedCookie = ServerCookieEncoder.encode(cookie); |
71 | | - assertEquals(result, encodedCookie); |
| 87 | + matchCookie(encodedCookie, result, maxAge); |
72 | 88 | } |
73 | 89 |
|
74 | 90 | @Test |
75 | | - public void testEncodingSingleCookieV2() { |
76 | | - String result = "myCookie=myValue; Max-Age=50; Path=\"/apathsomewhere\"; Domain=.adomainsomewhere; " + |
77 | | - "Secure; Comment=\"this is a Comment\"; Version=1; CommentURL=\"http://aurl.com\"; " + |
78 | | - "Port=\"80,8080\"; Discard"; |
| 91 | + public void testEncodingSingleCookieV2() throws ParseException { |
| 92 | + int maxAge = 50; |
| 93 | + String result = "myCookie=myValue; Max-Age=" + maxAge + "; Expires=(.+?); Path=\"/apathsomewhere\"; " + |
| 94 | + "Domain=.adomainsomewhere; Secure; Comment=\"this is a Comment\"; Version=1; " + |
| 95 | + "CommentURL=\"http://aurl.com\"; Port=\"80,8080\"; Discard"; |
79 | 96 | Cookie cookie = new DefaultCookie("myCookie", "myValue"); |
80 | 97 | cookie.setVersion(1); |
81 | 98 | cookie.setComment("this is a Comment"); |
82 | 99 | cookie.setCommentUrl("http://aurl.com"); |
83 | 100 | cookie.setDomain(".adomainsomewhere"); |
84 | 101 | cookie.setDiscard(true); |
85 | | - cookie.setMaxAge(50); |
| 102 | + cookie.setMaxAge(maxAge); |
86 | 103 | cookie.setPath("/apathsomewhere"); |
87 | 104 | cookie.setPorts(80, 8080); |
88 | 105 | cookie.setSecure(true); |
89 | 106 | String encodedCookie = ServerCookieEncoder.encode(cookie); |
90 | | - assertEquals(result, encodedCookie); |
| 107 | + matchCookie(encodedCookie, result, maxAge); |
91 | 108 | } |
92 | 109 |
|
93 | 110 | @Test |
|
0 commit comments