|
16 | 16 |
|
17 | 17 | package org.springframework.test.web.reactive.server;
|
18 | 18 |
|
19 |
| -import java.time.Duration; |
20 |
| -import java.util.function.Consumer; |
21 |
| - |
22 |
| -import org.hamcrest.Matcher; |
23 |
| -import org.hamcrest.MatcherAssert; |
24 |
| - |
25 | 19 | import org.springframework.http.ResponseCookie;
|
26 |
| - |
27 |
| -import static org.hamcrest.MatcherAssert.assertThat; |
28 |
| -import static org.springframework.test.util.AssertionErrors.assertEquals; |
29 |
| -import static org.springframework.test.util.AssertionErrors.fail; |
| 20 | +import org.springframework.test.web.support.AbstractCookieAssertions; |
| 21 | +import org.springframework.util.MultiValueMap; |
30 | 22 |
|
31 | 23 | /**
|
32 | 24 | * Assertions on cookies of the response.
|
33 | 25 | *
|
34 | 26 | * @author Rossen Stoyanchev
|
| 27 | + * @author Rob Worsnop |
35 | 28 | * @since 5.3
|
36 | 29 | */
|
37 |
| -public class CookieAssertions { |
38 |
| - |
39 |
| - private final ExchangeResult exchangeResult; |
40 |
| - |
41 |
| - private final WebTestClient.ResponseSpec responseSpec; |
42 |
| - |
| 30 | +public class CookieAssertions extends AbstractCookieAssertions<ExchangeResult, WebTestClient.ResponseSpec> { |
43 | 31 |
|
44 | 32 | public CookieAssertions(ExchangeResult exchangeResult, WebTestClient.ResponseSpec responseSpec) {
|
45 |
| - this.exchangeResult = exchangeResult; |
46 |
| - this.responseSpec = responseSpec; |
47 |
| - } |
48 |
| - |
49 |
| - |
50 |
| - /** |
51 |
| - * Expect a response cookie with the given name to match the specified value. |
52 |
| - */ |
53 |
| - public WebTestClient.ResponseSpec valueEquals(String name, String value) { |
54 |
| - String cookieValue = getCookie(name).getValue(); |
55 |
| - this.exchangeResult.assertWithDiagnostics(() -> { |
56 |
| - String message = getMessage(name); |
57 |
| - assertEquals(message, value, cookieValue); |
58 |
| - }); |
59 |
| - return this.responseSpec; |
60 |
| - } |
61 |
| - |
62 |
| - /** |
63 |
| - * Assert the value of the response cookie with the given name with a Hamcrest |
64 |
| - * {@link Matcher}. |
65 |
| - */ |
66 |
| - public WebTestClient.ResponseSpec value(String name, Matcher<? super String> matcher) { |
67 |
| - String value = getCookie(name).getValue(); |
68 |
| - this.exchangeResult.assertWithDiagnostics(() -> { |
69 |
| - String message = getMessage(name); |
70 |
| - MatcherAssert.assertThat(message, value, matcher); |
71 |
| - }); |
72 |
| - return this.responseSpec; |
73 |
| - } |
74 |
| - |
75 |
| - /** |
76 |
| - * Consume the value of the response cookie with the given name. |
77 |
| - */ |
78 |
| - public WebTestClient.ResponseSpec value(String name, Consumer<String> consumer) { |
79 |
| - String value = getCookie(name).getValue(); |
80 |
| - this.exchangeResult.assertWithDiagnostics(() -> consumer.accept(value)); |
81 |
| - return this.responseSpec; |
82 |
| - } |
83 |
| - |
84 |
| - /** |
85 |
| - * Expect that the cookie with the given name is present. |
86 |
| - */ |
87 |
| - public WebTestClient.ResponseSpec exists(String name) { |
88 |
| - getCookie(name); |
89 |
| - return this.responseSpec; |
| 33 | + super(exchangeResult, responseSpec); |
90 | 34 | }
|
91 | 35 |
|
92 |
| - /** |
93 |
| - * Expect that the cookie with the given name is not present. |
94 |
| - */ |
95 |
| - public WebTestClient.ResponseSpec doesNotExist(String name) { |
96 |
| - ResponseCookie cookie = this.exchangeResult.getResponseCookies().getFirst(name); |
97 |
| - if (cookie != null) { |
98 |
| - String message = getMessage(name) + " exists with value=[" + cookie.getValue() + "]"; |
99 |
| - this.exchangeResult.assertWithDiagnostics(() -> fail(message)); |
100 |
| - } |
101 |
| - return this.responseSpec; |
| 36 | + @Override |
| 37 | + protected void assertWithDiagnostics(Runnable assertion) { |
| 38 | + exchangeResult.assertWithDiagnostics(assertion); |
102 | 39 | }
|
103 | 40 |
|
104 |
| - /** |
105 |
| - * Assert a cookie's "Max-Age" attribute. |
106 |
| - */ |
107 |
| - public WebTestClient.ResponseSpec maxAge(String name, Duration expected) { |
108 |
| - Duration maxAge = getCookie(name).getMaxAge(); |
109 |
| - this.exchangeResult.assertWithDiagnostics(() -> { |
110 |
| - String message = getMessage(name) + " maxAge"; |
111 |
| - assertEquals(message, expected, maxAge); |
112 |
| - }); |
113 |
| - return this.responseSpec; |
| 41 | + @Override |
| 42 | + protected MultiValueMap<String, ResponseCookie> getResponseCookies() { |
| 43 | + return exchangeResult.getResponseCookies(); |
114 | 44 | }
|
115 |
| - |
116 |
| - /** |
117 |
| - * Assert a cookie's "Max-Age" attribute with a Hamcrest {@link Matcher}. |
118 |
| - */ |
119 |
| - public WebTestClient.ResponseSpec maxAge(String name, Matcher<? super Long> matcher) { |
120 |
| - long maxAge = getCookie(name).getMaxAge().getSeconds(); |
121 |
| - this.exchangeResult.assertWithDiagnostics(() -> { |
122 |
| - String message = getMessage(name) + " maxAge"; |
123 |
| - assertThat(message, maxAge, matcher); |
124 |
| - }); |
125 |
| - return this.responseSpec; |
126 |
| - } |
127 |
| - |
128 |
| - /** |
129 |
| - * Assert a cookie's "Path" attribute. |
130 |
| - */ |
131 |
| - public WebTestClient.ResponseSpec path(String name, String expected) { |
132 |
| - String path = getCookie(name).getPath(); |
133 |
| - this.exchangeResult.assertWithDiagnostics(() -> { |
134 |
| - String message = getMessage(name) + " path"; |
135 |
| - assertEquals(message, expected, path); |
136 |
| - }); |
137 |
| - return this.responseSpec; |
138 |
| - } |
139 |
| - |
140 |
| - /** |
141 |
| - * Assert a cookie's "Path" attribute with a Hamcrest {@link Matcher}. |
142 |
| - */ |
143 |
| - public WebTestClient.ResponseSpec path(String name, Matcher<? super String> matcher) { |
144 |
| - String path = getCookie(name).getPath(); |
145 |
| - this.exchangeResult.assertWithDiagnostics(() -> { |
146 |
| - String message = getMessage(name) + " path"; |
147 |
| - assertThat(message, path, matcher); |
148 |
| - }); |
149 |
| - return this.responseSpec; |
150 |
| - } |
151 |
| - |
152 |
| - /** |
153 |
| - * Assert a cookie's "Domain" attribute. |
154 |
| - */ |
155 |
| - public WebTestClient.ResponseSpec domain(String name, String expected) { |
156 |
| - String path = getCookie(name).getDomain(); |
157 |
| - this.exchangeResult.assertWithDiagnostics(() -> { |
158 |
| - String message = getMessage(name) + " domain"; |
159 |
| - assertEquals(message, expected, path); |
160 |
| - }); |
161 |
| - return this.responseSpec; |
162 |
| - } |
163 |
| - |
164 |
| - /** |
165 |
| - * Assert a cookie's "Domain" attribute with a Hamcrest {@link Matcher}. |
166 |
| - */ |
167 |
| - public WebTestClient.ResponseSpec domain(String name, Matcher<? super String> matcher) { |
168 |
| - String domain = getCookie(name).getDomain(); |
169 |
| - this.exchangeResult.assertWithDiagnostics(() -> { |
170 |
| - String message = getMessage(name) + " domain"; |
171 |
| - assertThat(message, domain, matcher); |
172 |
| - }); |
173 |
| - return this.responseSpec; |
174 |
| - } |
175 |
| - |
176 |
| - /** |
177 |
| - * Assert a cookie's "Secure" attribute. |
178 |
| - */ |
179 |
| - public WebTestClient.ResponseSpec secure(String name, boolean expected) { |
180 |
| - boolean isSecure = getCookie(name).isSecure(); |
181 |
| - this.exchangeResult.assertWithDiagnostics(() -> { |
182 |
| - String message = getMessage(name) + " secure"; |
183 |
| - assertEquals(message, expected, isSecure); |
184 |
| - }); |
185 |
| - return this.responseSpec; |
186 |
| - } |
187 |
| - |
188 |
| - /** |
189 |
| - * Assert a cookie's "HttpOnly" attribute. |
190 |
| - */ |
191 |
| - public WebTestClient.ResponseSpec httpOnly(String name, boolean expected) { |
192 |
| - boolean isHttpOnly = getCookie(name).isHttpOnly(); |
193 |
| - this.exchangeResult.assertWithDiagnostics(() -> { |
194 |
| - String message = getMessage(name) + " httpOnly"; |
195 |
| - assertEquals(message, expected, isHttpOnly); |
196 |
| - }); |
197 |
| - return this.responseSpec; |
198 |
| - } |
199 |
| - |
200 |
| - /** |
201 |
| - * Assert a cookie's "Partitioned" attribute. |
202 |
| - * @since 6.2 |
203 |
| - */ |
204 |
| - public WebTestClient.ResponseSpec partitioned(String name, boolean expected) { |
205 |
| - boolean isPartitioned = getCookie(name).isPartitioned(); |
206 |
| - this.exchangeResult.assertWithDiagnostics(() -> { |
207 |
| - String message = getMessage(name) + " isPartitioned"; |
208 |
| - assertEquals(message, expected, isPartitioned); |
209 |
| - }); |
210 |
| - return this.responseSpec; |
211 |
| - } |
212 |
| - |
213 |
| - /** |
214 |
| - * Assert a cookie's "SameSite" attribute. |
215 |
| - */ |
216 |
| - public WebTestClient.ResponseSpec sameSite(String name, String expected) { |
217 |
| - String sameSite = getCookie(name).getSameSite(); |
218 |
| - this.exchangeResult.assertWithDiagnostics(() -> { |
219 |
| - String message = getMessage(name) + " sameSite"; |
220 |
| - assertEquals(message, expected, sameSite); |
221 |
| - }); |
222 |
| - return this.responseSpec; |
223 |
| - } |
224 |
| - |
225 |
| - |
226 |
| - private ResponseCookie getCookie(String name) { |
227 |
| - ResponseCookie cookie = this.exchangeResult.getResponseCookies().getFirst(name); |
228 |
| - if (cookie != null) { |
229 |
| - return cookie; |
230 |
| - } |
231 |
| - else { |
232 |
| - this.exchangeResult.assertWithDiagnostics(() -> fail("No cookie with name '" + name + "'")); |
233 |
| - } |
234 |
| - throw new IllegalStateException("This code path should not be reachable"); |
235 |
| - } |
236 |
| - |
237 |
| - private static String getMessage(String cookie) { |
238 |
| - return "Response cookie '" + cookie + "'"; |
239 |
| - } |
240 |
| - |
241 | 45 | }
|
0 commit comments