20
20
21
21
import org .hamcrest .Matcher ;
22
22
23
+ import org .springframework .test .web .servlet .MvcResult ;
23
24
import org .springframework .test .web .servlet .ResultMatcher ;
24
25
25
26
import static org .hamcrest .MatcherAssert .*;
@@ -50,8 +51,7 @@ protected CookieResultMatchers() {
50
51
*/
51
52
public ResultMatcher value (final String name , final Matcher <? super String > matcher ) {
52
53
return result -> {
53
- Cookie cookie = result .getResponse ().getCookie (name );
54
- assertTrue ("Response cookie '" + name + "' not found" , cookie != null );
54
+ Cookie cookie = getCookie (result , name );
55
55
assertThat ("Response cookie '" + name + "'" , cookie .getValue (), matcher );
56
56
};
57
57
}
@@ -61,8 +61,7 @@ public ResultMatcher value(final String name, final Matcher<? super String> matc
61
61
*/
62
62
public ResultMatcher value (final String name , final String expectedValue ) {
63
63
return result -> {
64
- Cookie cookie = result .getResponse ().getCookie (name );
65
- assertTrue ("Response cookie '" + name + "' not found" , cookie != null );
64
+ Cookie cookie = getCookie (result , name );
66
65
assertEquals ("Response cookie" , expectedValue , cookie .getValue ());
67
66
};
68
67
}
@@ -72,10 +71,7 @@ public ResultMatcher value(final String name, final String expectedValue) {
72
71
* max age is 0 (i.e. expired).
73
72
*/
74
73
public ResultMatcher exists (final String name ) {
75
- return result -> {
76
- Cookie cookie = result .getResponse ().getCookie (name );
77
- assertTrue ("No cookie with name '" + name + "'" , cookie != null );
78
- };
74
+ return result -> getCookie (result , name );
79
75
}
80
76
81
77
/**
@@ -94,8 +90,7 @@ public ResultMatcher doesNotExist(final String name) {
94
90
*/
95
91
public ResultMatcher maxAge (final String name , final Matcher <? super Integer > matcher ) {
96
92
return result -> {
97
- Cookie cookie = result .getResponse ().getCookie (name );
98
- assertTrue ("No cookie with name '" + name + "'" , cookie != null );
93
+ Cookie cookie = getCookie (result , name );
99
94
assertThat ("Response cookie '" + name + "' maxAge" , cookie .getMaxAge (), matcher );
100
95
};
101
96
}
@@ -105,8 +100,7 @@ public ResultMatcher maxAge(final String name, final Matcher<? super Integer> ma
105
100
*/
106
101
public ResultMatcher maxAge (final String name , final int maxAge ) {
107
102
return result -> {
108
- Cookie cookie = result .getResponse ().getCookie (name );
109
- assertTrue ("No cookie with name: " + name , cookie != null );
103
+ Cookie cookie = getCookie (result , name );
110
104
assertEquals ("Response cookie '" + name + "' maxAge" , maxAge , cookie .getMaxAge ());
111
105
};
112
106
}
@@ -116,14 +110,14 @@ public ResultMatcher maxAge(final String name, final int maxAge) {
116
110
*/
117
111
public ResultMatcher path (final String name , final Matcher <? super String > matcher ) {
118
112
return result -> {
119
- Cookie cookie = result . getResponse (). getCookie (name );
113
+ Cookie cookie = getCookie (result , name );
120
114
assertThat ("Response cookie '" + name + "' path" , cookie .getPath (), matcher );
121
115
};
122
116
}
123
117
124
118
public ResultMatcher path (final String name , final String path ) {
125
119
return result -> {
126
- Cookie cookie = result . getResponse (). getCookie (name );
120
+ Cookie cookie = getCookie (result , name );
127
121
assertEquals ("Response cookie '" + name + "' path" , path , cookie .getPath ());
128
122
};
129
123
}
@@ -133,7 +127,7 @@ public ResultMatcher path(final String name, final String path) {
133
127
*/
134
128
public ResultMatcher domain (final String name , final Matcher <? super String > matcher ) {
135
129
return result -> {
136
- Cookie cookie = result . getResponse (). getCookie (name );
130
+ Cookie cookie = getCookie (result , name );
137
131
assertThat ("Response cookie '" + name + "' domain" , cookie .getDomain (), matcher );
138
132
};
139
133
}
@@ -143,7 +137,7 @@ public ResultMatcher domain(final String name, final Matcher<? super String> mat
143
137
*/
144
138
public ResultMatcher domain (final String name , final String domain ) {
145
139
return result -> {
146
- Cookie cookie = result . getResponse (). getCookie (name );
140
+ Cookie cookie = getCookie (result , name );
147
141
assertEquals ("Response cookie '" + name + "' domain" , domain , cookie .getDomain ());
148
142
};
149
143
}
@@ -153,7 +147,7 @@ public ResultMatcher domain(final String name, final String domain) {
153
147
*/
154
148
public ResultMatcher comment (final String name , final Matcher <? super String > matcher ) {
155
149
return result -> {
156
- Cookie cookie = result . getResponse (). getCookie (name );
150
+ Cookie cookie = getCookie (result , name );
157
151
assertThat ("Response cookie '" + name + "' comment" , cookie .getComment (), matcher );
158
152
};
159
153
}
@@ -163,7 +157,7 @@ public ResultMatcher comment(final String name, final Matcher<? super String> ma
163
157
*/
164
158
public ResultMatcher comment (final String name , final String comment ) {
165
159
return result -> {
166
- Cookie cookie = result . getResponse (). getCookie (name );
160
+ Cookie cookie = getCookie (result , name );
167
161
assertEquals ("Response cookie '" + name + "' comment" , comment , cookie .getComment ());
168
162
};
169
163
}
@@ -173,7 +167,7 @@ public ResultMatcher comment(final String name, final String comment) {
173
167
*/
174
168
public ResultMatcher version (final String name , final Matcher <? super Integer > matcher ) {
175
169
return result -> {
176
- Cookie cookie = result . getResponse (). getCookie (name );
170
+ Cookie cookie = getCookie (result , name );
177
171
assertThat ("Response cookie '" + name + "' version" , cookie .getVersion (), matcher );
178
172
};
179
173
}
@@ -183,7 +177,7 @@ public ResultMatcher version(final String name, final Matcher<? super Integer> m
183
177
*/
184
178
public ResultMatcher version (final String name , final int version ) {
185
179
return result -> {
186
- Cookie cookie = result . getResponse (). getCookie (name );
180
+ Cookie cookie = getCookie (result , name );
187
181
assertEquals ("Response cookie '" + name + "' version" , version , cookie .getVersion ());
188
182
};
189
183
}
@@ -193,7 +187,7 @@ public ResultMatcher version(final String name, final int version) {
193
187
*/
194
188
public ResultMatcher secure (final String name , final boolean secure ) {
195
189
return result -> {
196
- Cookie cookie = result . getResponse (). getCookie (name );
190
+ Cookie cookie = getCookie (result , name );
197
191
assertEquals ("Response cookie '" + name + "' secure" , secure , cookie .getSecure ());
198
192
};
199
193
}
@@ -204,9 +198,16 @@ public ResultMatcher secure(final String name, final boolean secure) {
204
198
*/
205
199
public ResultMatcher httpOnly (final String name , final boolean httpOnly ) {
206
200
return result -> {
207
- Cookie cookie = result . getResponse (). getCookie (name );
201
+ Cookie cookie = getCookie (result , name );
208
202
assertEquals ("Response cookie '" + name + "' httpOnly" , httpOnly , cookie .isHttpOnly ());
209
203
};
210
204
}
211
205
206
+
207
+ private static Cookie getCookie (MvcResult result , String name ) {
208
+ Cookie cookie = result .getResponse ().getCookie (name );
209
+ assertTrue ("No cookie with name '" + name + "'" , cookie != null );
210
+ return cookie ;
211
+ }
212
+
212
213
}
0 commit comments