Skip to content

Commit 4072649

Browse files
committed
Refactor *Assertions classes
Fixes gh-31275 Signed-off-by: Rob Worsnop <[email protected]>
1 parent d65f5d4 commit 4072649

17 files changed

+1279
-2081
lines changed

spring-test/src/main/java/org/springframework/test/web/reactive/server/CookieAssertions.java

Lines changed: 11 additions & 207 deletions
Original file line numberDiff line numberDiff line change
@@ -16,226 +16,30 @@
1616

1717
package org.springframework.test.web.reactive.server;
1818

19-
import java.time.Duration;
20-
import java.util.function.Consumer;
21-
22-
import org.hamcrest.Matcher;
23-
import org.hamcrest.MatcherAssert;
24-
2519
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;
3022

3123
/**
3224
* Assertions on cookies of the response.
3325
*
3426
* @author Rossen Stoyanchev
27+
* @author Rob Worsnop
3528
* @since 5.3
3629
*/
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> {
4331

4432
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);
9034
}
9135

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);
10239
}
10340

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();
11444
}
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-
24145
}

0 commit comments

Comments
 (0)