3131import org .apache .http .client .config .CookieSpecs ;
3232import org .junit .jupiter .api .Test ;
3333
34+ import java .time .LocalDateTime ;
35+ import java .time .Month ;
36+ import java .time .ZoneId ;
37+ import java .time .ZonedDateTime ;
38+ import java .time .format .DateTimeFormatter ;
3439import java .util .Arrays ;
3540
41+ import static java .time .LocalDateTime .of ;
3642import static org .junit .jupiter .api .Assertions .*;
3743
3844class CookieTest extends BddTest {
@@ -114,13 +120,7 @@ void canGetValuesWithBadCharacters() {
114120
115121 @ Test
116122 void complicatedCookies (){
117- javax .servlet .http .Cookie cookie = new javax .servlet .http .Cookie ("color" , "blue" );
118- cookie .setDomain ("localhost" );
119- cookie .setPath ("/get" );
120- cookie .setHttpOnly (true );
121- cookie .setSecure (false );
122- cookie .setMaxAge (42 );
123- MockServer .expectCookie (cookie );
123+ expectCoookie (42 );
124124
125125 HttpResponse response = Unirest .get (MockServer .GET ).asEmpty ();
126126
@@ -131,7 +131,34 @@ void complicatedCookies(){
131131 assertTrue (back .isHttpOnly ());
132132 assertFalse (back .isSecure ());
133133 assertEquals (42 , back .getMaxAge ());
134+ }
135+
136+ @ Test
137+ void cookieLifeCycle () {
138+ expectCoookie (42 );
139+ HttpResponse r1 = Unirest .get (MockServer .GET ).asObject (RequestCapture .class );
140+ assertNotNull (r1 .getCookies ().getNamed ("color" ));
134141
142+ MockServer .clearCookies ();
143+ expectCoookie (0 );
144+
145+ HttpResponse <RequestCapture > r2 = Unirest .get (MockServer .GET ).asObject (RequestCapture .class );
146+ assertNotNull (r1 .getCookies ().getNamed ("color" ));
147+ r2 .getBody ().assertCookie ("color" , "blue" );
148+
149+ MockServer .clearCookies ();
150+ HttpResponse <RequestCapture > r3 = Unirest .get (MockServer .GET ).asObject (RequestCapture .class );
151+ r3 .getBody ().assertNoCookie ("color" );
152+ }
153+
154+ private void expectCoookie (int expiry ) {
155+ javax .servlet .http .Cookie cookie = new javax .servlet .http .Cookie ("color" , "blue" );
156+ cookie .setDomain ("localhost" );
157+ cookie .setPath ("/get" );
158+ cookie .setHttpOnly (true );
159+ cookie .setSecure (false );
160+ cookie .setMaxAge (expiry );
161+ MockServer .expectCookie (cookie );
135162 }
136163
137164 @ Test
@@ -197,4 +224,40 @@ void canSetDefaultCookieAsFullCookieObj() {
197224 .assertCookie ("flavor" , "snickerdoodle" )
198225 .assertCookie ("size" , "large" );
199226 }
227+
228+ @ Test
229+ void stringCookieParsing () {
230+ MockServer .addResponseHeader ("Set-Cookie" , getCookieValue (of (2140 , Month .APRIL , 2 , 4 , 20 , 0 ).atZone (ZoneId .of ("UTC" ))));
231+
232+ HttpResponse r1 = Unirest .get (MockServer .GET ).asObject (RequestCapture .class );
233+ assertNotNull (r1 .getCookies ().getNamed ("color" ));
234+
235+ MockServer .clearHeaders ();
236+ MockServer .addResponseHeader ("Set-Cookie" , getCookieValue (of (1985 , Month .APRIL , 2 , 4 , 20 , 0 ).atZone (ZoneId .of ("UTC" ))));
237+
238+
239+ HttpResponse <RequestCapture > r2 = Unirest .get (MockServer .GET ).asObject (RequestCapture .class );
240+ assertNotNull (r1 .getCookies ().getNamed ("color" ));
241+ r2 .getBody ().assertCookie ("color" , "blue" );
242+
243+ MockServer .clearHeaders ();
244+ HttpResponse <RequestCapture > r3 = Unirest .get (MockServer .GET ).asObject (RequestCapture .class );
245+ r3 .getBody ().assertNoCookie ("color" );
246+ }
247+
248+ @ Test
249+ void newAgeCookies () {
250+ Unirest .config ().cookieSpec ("standard" );
251+ MockServer .addResponseHeader ("Set-Cookie" , "cookie_name=blah;Max-Age=86400;Expires=Wed, 9 Dec 2220 20:26:05 GMT;Path=/;Domain=localhost;HTTPOnly" );
252+ HttpResponse response = Unirest .get (MockServer .GET ).asObject (RequestCapture .class );
253+ assertNotNull (response .getCookies ().getNamed ("cookie_name" ));
254+ HttpResponse <RequestCapture > r2 = Unirest .get (MockServer .GET ).asObject (RequestCapture .class );
255+ r2 .getBody ().assertCookie ("cookie_name" ,"blah" );
256+
257+ }
258+
259+ private String getCookieValue (ZonedDateTime dt ) {
260+ String date = dt .format (DateTimeFormatter .ofPattern ("EEE, dd-MMM-yyyy HH:mm:ss zzz" ));
261+ return String .format ("color=blue; Path=/get; Domain=localhost; Expires=%s; HttpOnly" , date );
262+ }
200263}
0 commit comments