1
1
/*
2
- * Copyright 2002-2021 the original author or authors.
2
+ * Copyright 2002-2024 the original author or authors.
3
3
*
4
4
* Licensed under the Apache License, Version 2.0 (the "License");
5
5
* you may not use this file except in compliance with the License.
24
24
import org .hamcrest .MatcherAssert ;
25
25
26
26
import org .springframework .http .ResponseCookie ;
27
- import org .springframework .test .util .AssertionErrors ;
28
27
29
28
import static org .hamcrest .MatcherAssert .assertThat ;
29
+ import static org .springframework .test .util .AssertionErrors .assertEquals ;
30
+ import static org .springframework .test .util .AssertionErrors .fail ;
30
31
31
32
/**
32
33
* Assertions on cookies of the response.
@@ -48,18 +49,20 @@ public CookieAssertions(ExchangeResult exchangeResult, WebTestClient.ResponseSpe
48
49
49
50
50
51
/**
51
- * Expect a header with the given name to match the specified values .
52
+ * Expect a response cookie with the given name to match the specified value .
52
53
*/
53
54
public WebTestClient .ResponseSpec valueEquals (String name , String value ) {
55
+ String cookieValue = getCookie (name ).getValue ();
54
56
this .exchangeResult .assertWithDiagnostics (() -> {
55
57
String message = getMessage (name );
56
- AssertionErrors . assertEquals (message , value , getCookie ( name ). getValue () );
58
+ assertEquals (message , value , cookieValue );
57
59
});
58
60
return this .responseSpec ;
59
61
}
60
62
61
63
/**
62
- * Assert the first value of the response cookie with a Hamcrest {@link Matcher}.
64
+ * Assert the value of the response cookie with the given name with a Hamcrest
65
+ * {@link Matcher}.
63
66
*/
64
67
public WebTestClient .ResponseSpec value (String name , Matcher <? super String > matcher ) {
65
68
String value = getCookie (name ).getValue ();
@@ -71,7 +74,7 @@ public WebTestClient.ResponseSpec value(String name, Matcher<? super String> mat
71
74
}
72
75
73
76
/**
74
- * Consume the value of the response cookie.
77
+ * Consume the value of the response cookie with the given name .
75
78
*/
76
79
public WebTestClient .ResponseSpec value (String name , Consumer <String > consumer ) {
77
80
String value = getCookie (name ).getValue ();
@@ -94,25 +97,25 @@ public WebTestClient.ResponseSpec doesNotExist(String name) {
94
97
ResponseCookie cookie = this .exchangeResult .getResponseCookies ().getFirst (name );
95
98
if (cookie != null ) {
96
99
String message = getMessage (name ) + " exists with value=[" + cookie .getValue () + "]" ;
97
- this .exchangeResult .assertWithDiagnostics (() -> AssertionErrors . fail (message ));
100
+ this .exchangeResult .assertWithDiagnostics (() -> fail (message ));
98
101
}
99
102
return this .responseSpec ;
100
103
}
101
104
102
105
/**
103
- * Assert a cookie's maxAge attribute.
106
+ * Assert a cookie's "Max-Age" attribute.
104
107
*/
105
108
public WebTestClient .ResponseSpec maxAge (String name , Duration expected ) {
106
109
Duration maxAge = getCookie (name ).getMaxAge ();
107
110
this .exchangeResult .assertWithDiagnostics (() -> {
108
111
String message = getMessage (name ) + " maxAge" ;
109
- AssertionErrors . assertEquals (message , expected , maxAge );
112
+ assertEquals (message , expected , maxAge );
110
113
});
111
114
return this .responseSpec ;
112
115
}
113
116
114
117
/**
115
- * Assert a cookie's maxAge attribute with a Hamcrest {@link Matcher}.
118
+ * Assert a cookie's "Max-Age" attribute with a Hamcrest {@link Matcher}.
116
119
*/
117
120
public WebTestClient .ResponseSpec maxAge (String name , Matcher <? super Long > matcher ) {
118
121
long maxAge = getCookie (name ).getMaxAge ().getSeconds ();
@@ -124,19 +127,19 @@ public WebTestClient.ResponseSpec maxAge(String name, Matcher<? super Long> matc
124
127
}
125
128
126
129
/**
127
- * Assert a cookie's path attribute.
130
+ * Assert a cookie's "Path" attribute.
128
131
*/
129
132
public WebTestClient .ResponseSpec path (String name , String expected ) {
130
133
String path = getCookie (name ).getPath ();
131
134
this .exchangeResult .assertWithDiagnostics (() -> {
132
135
String message = getMessage (name ) + " path" ;
133
- AssertionErrors . assertEquals (message , expected , path );
136
+ assertEquals (message , expected , path );
134
137
});
135
138
return this .responseSpec ;
136
139
}
137
140
138
141
/**
139
- * Assert a cookie's path attribute with a Hamcrest {@link Matcher}.
142
+ * Assert a cookie's "Path" attribute with a Hamcrest {@link Matcher}.
140
143
*/
141
144
public WebTestClient .ResponseSpec path (String name , Matcher <? super String > matcher ) {
142
145
String path = getCookie (name ).getPath ();
@@ -148,19 +151,19 @@ public WebTestClient.ResponseSpec path(String name, Matcher<? super String> matc
148
151
}
149
152
150
153
/**
151
- * Assert a cookie's domain attribute.
154
+ * Assert a cookie's "Domain" attribute.
152
155
*/
153
156
public WebTestClient .ResponseSpec domain (String name , String expected ) {
154
157
String path = getCookie (name ).getDomain ();
155
158
this .exchangeResult .assertWithDiagnostics (() -> {
156
159
String message = getMessage (name ) + " domain" ;
157
- AssertionErrors . assertEquals (message , expected , path );
160
+ assertEquals (message , expected , path );
158
161
});
159
162
return this .responseSpec ;
160
163
}
161
164
162
165
/**
163
- * Assert a cookie's domain attribute with a Hamcrest {@link Matcher}.
166
+ * Assert a cookie's "Domain" attribute with a Hamcrest {@link Matcher}.
164
167
*/
165
168
public WebTestClient .ResponseSpec domain (String name , Matcher <? super String > matcher ) {
166
169
String domain = getCookie (name ).getDomain ();
@@ -172,37 +175,37 @@ public WebTestClient.ResponseSpec domain(String name, Matcher<? super String> ma
172
175
}
173
176
174
177
/**
175
- * Assert a cookie's secure attribute.
178
+ * Assert a cookie's "Secure" attribute.
176
179
*/
177
180
public WebTestClient .ResponseSpec secure (String name , boolean expected ) {
178
181
boolean isSecure = getCookie (name ).isSecure ();
179
182
this .exchangeResult .assertWithDiagnostics (() -> {
180
183
String message = getMessage (name ) + " secure" ;
181
- AssertionErrors . assertEquals (message , expected , isSecure );
184
+ assertEquals (message , expected , isSecure );
182
185
});
183
186
return this .responseSpec ;
184
187
}
185
188
186
189
/**
187
- * Assert a cookie's httpOnly attribute.
190
+ * Assert a cookie's "HttpOnly" attribute.
188
191
*/
189
192
public WebTestClient .ResponseSpec httpOnly (String name , boolean expected ) {
190
193
boolean isHttpOnly = getCookie (name ).isHttpOnly ();
191
194
this .exchangeResult .assertWithDiagnostics (() -> {
192
195
String message = getMessage (name ) + " httpOnly" ;
193
- AssertionErrors . assertEquals (message , expected , isHttpOnly );
196
+ assertEquals (message , expected , isHttpOnly );
194
197
});
195
198
return this .responseSpec ;
196
199
}
197
200
198
201
/**
199
- * Assert a cookie's sameSite attribute.
202
+ * Assert a cookie's "SameSite" attribute.
200
203
*/
201
204
public WebTestClient .ResponseSpec sameSite (String name , String expected ) {
202
205
String sameSite = getCookie (name ).getSameSite ();
203
206
this .exchangeResult .assertWithDiagnostics (() -> {
204
207
String message = getMessage (name ) + " sameSite" ;
205
- AssertionErrors . assertEquals (message , expected , sameSite );
208
+ assertEquals (message , expected , sameSite );
206
209
});
207
210
return this .responseSpec ;
208
211
}
@@ -211,13 +214,12 @@ public WebTestClient.ResponseSpec sameSite(String name, String expected) {
211
214
private ResponseCookie getCookie (String name ) {
212
215
ResponseCookie cookie = this .exchangeResult .getResponseCookies ().getFirst (name );
213
216
if (cookie == null ) {
214
- this .exchangeResult .assertWithDiagnostics (() ->
215
- AssertionErrors .fail ("No cookie with name '" + name + "'" ));
217
+ this .exchangeResult .assertWithDiagnostics (() -> fail ("No cookie with name '" + name + "'" ));
216
218
}
217
219
return Objects .requireNonNull (cookie );
218
220
}
219
221
220
- private String getMessage (String cookie ) {
222
+ private static String getMessage (String cookie ) {
221
223
return "Response cookie '" + cookie + "'" ;
222
224
}
223
225
0 commit comments