16
16
17
17
package org .springframework .test .web .servlet .samples .standalone .resultmatchers ;
18
18
19
+ import jakarta .servlet .http .Cookie ;
20
+ import jakarta .servlet .http .HttpServletRequest ;
21
+ import jakarta .servlet .http .HttpServletResponse ;
19
22
import org .junit .jupiter .api .BeforeEach ;
20
23
import org .junit .jupiter .api .Test ;
21
24
22
25
import org .springframework .stereotype .Controller ;
23
26
import org .springframework .test .web .servlet .MockMvc ;
24
27
import org .springframework .web .bind .annotation .RequestMapping ;
28
+ import org .springframework .web .servlet .HandlerInterceptor ;
25
29
import org .springframework .web .servlet .i18n .CookieLocaleResolver ;
26
30
import org .springframework .web .servlet .i18n .LocaleChangeInterceptor ;
27
31
32
+ import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
33
+ import static org .hamcrest .CoreMatchers .anything ;
34
+ import static org .hamcrest .CoreMatchers .is ;
28
35
import static org .hamcrest .Matchers .equalTo ;
29
36
import static org .hamcrest .Matchers .startsWith ;
30
37
import static org .springframework .test .web .servlet .request .MockMvcRequestBuilders .get ;
41
48
public class CookieAssertionTests {
42
49
43
50
private static final String COOKIE_NAME = CookieLocaleResolver .DEFAULT_COOKIE_NAME ;
51
+ private static final String COOKIE_WITH_ATTRIBUTES_NAME = "SecondCookie" ;
52
+ protected static final String SECOND_COOKIE_ATTRIBUTE = "COOKIE_ATTRIBUTE" ;
44
53
45
54
private MockMvc mockMvc ;
46
55
@@ -50,9 +59,21 @@ public void setup() {
50
59
CookieLocaleResolver localeResolver = new CookieLocaleResolver ();
51
60
localeResolver .setCookieDomain ("domain" );
52
61
localeResolver .setCookieHttpOnly (true );
62
+ localeResolver .setCookieSameSite ("foo" );
63
+
64
+ Cookie cookie = new Cookie (COOKIE_WITH_ATTRIBUTES_NAME , "value" );
65
+ cookie .setAttribute ("sameSite" , "Strict" ); //intentionally camelCase
66
+ cookie .setAttribute (SECOND_COOKIE_ATTRIBUTE , "there" );
53
67
54
68
this .mockMvc = standaloneSetup (new SimpleController ())
55
69
.addInterceptors (new LocaleChangeInterceptor ())
70
+ .addInterceptors (new HandlerInterceptor () {
71
+ @ Override
72
+ public boolean preHandle (HttpServletRequest request , HttpServletResponse response , Object handler ) throws Exception {
73
+ response .addCookie (cookie );
74
+ return true ;
75
+ }
76
+ })
56
77
.setLocaleResolver (localeResolver )
57
78
.defaultRequest (get ("/" ).param ("locale" , "en_US" ))
58
79
.alwaysExpect (status ().isOk ())
@@ -91,6 +112,26 @@ public void testDomain() throws Exception {
91
112
this .mockMvc .perform (get ("/" )).andExpect (cookie ().domain (COOKIE_NAME , "domain" ));
92
113
}
93
114
115
+ @ Test
116
+ void testSameSite () throws Exception {
117
+ this .mockMvc .perform (get ("/" )).andExpect (cookie ()
118
+ .sameSite (COOKIE_NAME , "foo" ));
119
+ }
120
+
121
+ @ Test
122
+ void testSameSiteMatcher () throws Exception {
123
+ this .mockMvc .perform (get ("/" )).andExpect (cookie ()
124
+ .sameSite (COOKIE_WITH_ATTRIBUTES_NAME , startsWith ("Str" )));
125
+ }
126
+
127
+ @ Test
128
+ void testSameSiteNotEquals () throws Exception {
129
+ assertThatExceptionOfType (AssertionError .class ).isThrownBy (() ->
130
+ this .mockMvc .perform (get ("/" )).andExpect (cookie ()
131
+ .sameSite (COOKIE_WITH_ATTRIBUTES_NAME , "Str" )))
132
+ .withMessage ("Response cookie 'SecondCookie' attribute 'SameSite' expected:<Str> but was:<Strict>" );
133
+ }
134
+
94
135
@ Test
95
136
public void testVersion () throws Exception {
96
137
this .mockMvc .perform (get ("/" )).andExpect (cookie ().version (COOKIE_NAME , 0 ));
@@ -111,6 +152,32 @@ public void testHttpOnly() throws Exception {
111
152
this .mockMvc .perform (get ("/" )).andExpect (cookie ().httpOnly (COOKIE_NAME , true ));
112
153
}
113
154
155
+ @ Test
156
+ void testAttribute () throws Exception {
157
+ this .mockMvc .perform (get ("/" )).andExpect (cookie ()
158
+ .attribute (COOKIE_WITH_ATTRIBUTES_NAME , SECOND_COOKIE_ATTRIBUTE , "there" ));
159
+ }
160
+
161
+ @ Test
162
+ void testAttributeMatcher () throws Exception {
163
+ this .mockMvc .perform (get ("/" )).andExpect (cookie ()
164
+ .attribute (COOKIE_WITH_ATTRIBUTES_NAME , SECOND_COOKIE_ATTRIBUTE , is ("there" )));
165
+ }
166
+
167
+ @ Test
168
+ void testAttributeNotPresent () {
169
+ assertThatExceptionOfType (AssertionError .class ).isThrownBy (() -> this .mockMvc .perform (get ("/" ))
170
+ .andExpect (cookie ().attribute (COOKIE_WITH_ATTRIBUTES_NAME , "randomAttribute" , anything ())))
171
+ .withMessage ("Response cookie 'SecondCookie' doesn't have attribute 'randomAttribute'" );
172
+ }
173
+
174
+ @ Test
175
+ void testAttributeNotEquals () {
176
+ assertThatExceptionOfType (AssertionError .class ).isThrownBy (() -> this .mockMvc .perform (get ("/" ))
177
+ .andExpect (cookie ().attribute (COOKIE_WITH_ATTRIBUTES_NAME , SECOND_COOKIE_ATTRIBUTE , "foo" )))
178
+ .withMessage ("Response cookie 'SecondCookie' attribute 'COOKIE_ATTRIBUTE' expected:<foo> but was:<there>" );
179
+ }
180
+
114
181
115
182
@ Controller
116
183
private static class SimpleController {
0 commit comments