19
19
import java .time .Clock ;
20
20
import java .time .Instant ;
21
21
import java .time .ZoneOffset ;
22
+ import java .time .ZonedDateTime ;
23
+ import java .time .format .DateTimeFormatter ;
22
24
import java .util .Base64 ;
23
25
24
26
import javax .servlet .http .Cookie ;
28
30
import org .junit .jupiter .params .ParameterizedTest ;
29
31
import org .junit .jupiter .params .provider .ValueSource ;
30
32
31
- import org .springframework .http .HttpHeaders ;
32
33
import org .springframework .mock .web .MockCookie ;
33
34
import org .springframework .mock .web .MockHttpServletRequest ;
34
35
import org .springframework .mock .web .MockHttpServletResponse ;
@@ -52,7 +53,7 @@ class DefaultCookieSerializerTests {
52
53
53
54
private MockHttpServletRequest request ;
54
55
55
- private CookiePreservingMockHttpServletResponse response ;
56
+ private MockHttpServletResponse response ;
56
57
57
58
private DefaultCookieSerializer serializer ;
58
59
@@ -62,7 +63,7 @@ class DefaultCookieSerializerTests {
62
63
void setup () {
63
64
this .cookieName = "SESSION" ;
64
65
this .request = new MockHttpServletRequest ();
65
- this .response = new CookiePreservingMockHttpServletResponse ();
66
+ this .response = new MockHttpServletResponse ();
66
67
this .sessionId = "sessionId" ;
67
68
this .serializer = new DefaultCookieSerializer ();
68
69
}
@@ -213,14 +214,14 @@ void writeCookieDomainNamePattern() {
213
214
this .request .setServerName (domain );
214
215
this .serializer .writeCookieValue (cookieValue (this .sessionId ));
215
216
assertThat (getCookie ().getDomain ()).isEqualTo ("example.com" );
216
- this .response = new CookiePreservingMockHttpServletResponse ();
217
+ this .response = new MockHttpServletResponse ();
217
218
}
218
219
String [] notMatchingDomains = { "example.com" , "localhost" , "127.0.0.1" };
219
220
for (String domain : notMatchingDomains ) {
220
221
this .request .setServerName (domain );
221
222
this .serializer .writeCookieValue (cookieValue (this .sessionId ));
222
223
assertThat (getCookie ().getDomain ()).isNull ();
223
- this .response = new CookiePreservingMockHttpServletResponse ();
224
+ this .response = new MockHttpServletResponse ();
224
225
}
225
226
}
226
227
@@ -291,25 +292,31 @@ void writeCookieCookiePathExplicitCookiePath() {
291
292
void writeCookieCookieMaxAgeDefault () {
292
293
this .serializer .writeCookieValue (cookieValue (this .sessionId ));
293
294
assertThat (getCookie ().getMaxAge ()).isEqualTo (-1 );
294
- assertThat (this . response . rawCookie ). doesNotContain ( "Expires" );
295
+ assertThat (getCookie (). getExpires ()). isNull ( );
295
296
}
296
297
297
298
@ Test
298
299
void writeCookieCookieMaxAgeExplicit () {
299
300
this .serializer .setClock (Clock .fixed (Instant .parse ("2019-10-07T20:10:00Z" ), ZoneOffset .UTC ));
300
301
this .serializer .setCookieMaxAge (100 );
301
302
this .serializer .writeCookieValue (cookieValue (this .sessionId ));
302
- assertThat (getCookie ().getMaxAge ()).isEqualTo (100 );
303
- assertThat (this .response .rawCookie ).contains ("Expires=Mon, 7 Oct 2019 20:11:40 GMT" );
303
+ MockCookie cookie = getCookie ();
304
+ assertThat (cookie .getMaxAge ()).isEqualTo (100 );
305
+ ZonedDateTime expires = cookie .getExpires ();
306
+ assertThat (expires ).isNotNull ();
307
+ assertThat (expires .format (DateTimeFormatter .RFC_1123_DATE_TIME )).isEqualTo ("Mon, 7 Oct 2019 20:11:40 GMT" );
304
308
}
305
309
306
310
@ Test
307
311
void writeCookieCookieMaxAgeExplicitEmptyCookie () {
308
312
this .serializer .setClock (Clock .fixed (Instant .parse ("2019-10-07T20:10:00Z" ), ZoneOffset .UTC ));
309
313
this .serializer .setCookieMaxAge (100 );
310
314
this .serializer .writeCookieValue (cookieValue ("" ));
311
- assertThat (getCookie ().getMaxAge ()).isEqualTo (0 );
312
- assertThat (this .response .rawCookie ).contains ("Expires=Thu, 1 Jan 1970 00:00:00 GMT" );
315
+ MockCookie cookie = getCookie ();
316
+ assertThat (cookie .getMaxAge ()).isEqualTo (0 );
317
+ ZonedDateTime expires = cookie .getExpires ();
318
+ assertThat (expires ).isNotNull ();
319
+ assertThat (expires .format (DateTimeFormatter .RFC_1123_DATE_TIME )).isEqualTo ("Thu, 1 Jan 1970 00:00:00 GMT" );
313
320
}
314
321
315
322
@ Test
@@ -318,8 +325,11 @@ void writeCookieCookieMaxAgeExplicitCookieValue() {
318
325
CookieValue cookieValue = cookieValue (this .sessionId );
319
326
cookieValue .setCookieMaxAge (100 );
320
327
this .serializer .writeCookieValue (cookieValue );
321
- assertThat (getCookie ().getMaxAge ()).isEqualTo (100 );
322
- assertThat (this .response .rawCookie ).contains ("Expires=Mon, 7 Oct 2019 20:11:40 GMT" );
328
+ MockCookie cookie = getCookie ();
329
+ assertThat (cookie .getMaxAge ()).isEqualTo (100 );
330
+ ZonedDateTime expires = cookie .getExpires ();
331
+ assertThat (expires ).isNotNull ();
332
+ assertThat (expires .format (DateTimeFormatter .RFC_1123_DATE_TIME )).isEqualTo ("Mon, 7 Oct 2019 20:11:40 GMT" );
323
333
}
324
334
325
335
// --- secure ---
@@ -482,18 +492,4 @@ private CookieValue cookieValue(String cookieValue) {
482
492
return new CookieValue (this .request , this .response , cookieValue );
483
493
}
484
494
485
- private static class CookiePreservingMockHttpServletResponse extends MockHttpServletResponse {
486
-
487
- private String rawCookie ;
488
-
489
- @ Override
490
- public void addHeader (String name , String value ) {
491
- if (HttpHeaders .SET_COOKIE .equals (name )) {
492
- this .rawCookie = value ;
493
- }
494
- super .addHeader (name , value );
495
- }
496
-
497
- }
498
-
499
495
}
0 commit comments