Skip to content

Commit 6e587d5

Browse files
committed
Add new WebTestClient header assertions
This commit adds new header assertions for `WebTestClient`. `doesNotExist` tests that a given header is not present: .expectHeader().doesNotExist("Cache-Control"); `contentTypeCompatibleWith` tests for MediaType compatibility: .expectHeader().contentTypeCompatibleWith("text/*"); Issue: SPR-16285
1 parent 7035ee7 commit 6e587d5

File tree

2 files changed

+83
-1
lines changed

2 files changed

+83
-1
lines changed

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

Lines changed: 41 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,15 @@
2525
import org.springframework.http.MediaType;
2626
import org.springframework.lang.Nullable;
2727

28-
import static org.springframework.test.util.AssertionErrors.*;
28+
import static org.springframework.test.util.AssertionErrors.assertEquals;
29+
import static org.springframework.test.util.AssertionErrors.assertTrue;
30+
import static org.springframework.test.util.AssertionErrors.fail;
2931

3032
/**
3133
* Assertions on headers of the response.
3234
*
3335
* @author Rossen Stoyanchev
36+
* @author Brian Clozel
3437
* @since 5.0
3538
* @see WebTestClient.ResponseSpec#expectHeader()
3639
*/
@@ -71,6 +74,17 @@ public WebTestClient.ResponseSpec valueMatches(String name, String pattern) {
7174
return this.responseSpec;
7275
}
7376

77+
/**
78+
* Expect that the header with the given name is not present.
79+
*/
80+
public WebTestClient.ResponseSpec doesNotExist(String name) {
81+
if (getHeaders().containsKey(name)) {
82+
String message = getMessage(name) + " exists with value=[" + getHeaders().getFirst(name) + "]";
83+
this.exchangeResult.assertWithDiagnostics(() -> fail(message));
84+
}
85+
return this.responseSpec;
86+
}
87+
7488
/**
7589
* Expect a "Cache-Control" header with the given value.
7690
*/
@@ -99,6 +113,32 @@ public WebTestClient.ResponseSpec contentType(MediaType mediaType) {
99113
return assertHeader("Content-Type", mediaType, getHeaders().getContentType());
100114
}
101115

116+
/**
117+
* Expect a "Content-Type" header with the given value.
118+
*/
119+
public WebTestClient.ResponseSpec contentType(String mediaType) {
120+
return contentType(MediaType.parseMediaType(mediaType));
121+
}
122+
123+
/**
124+
* Expect a "Content-Type" header compatible with the given value.
125+
*/
126+
public WebTestClient.ResponseSpec contentTypeCompatibleWith(MediaType mediaType) {
127+
MediaType actual = getHeaders().getContentType();
128+
String message = getMessage("Content-Type") + "=[" + actual.toString() + "]"
129+
+ " is not compatible with [" + mediaType.toString() + "]";
130+
this.exchangeResult.assertWithDiagnostics(() ->
131+
assertTrue(message, actual.isCompatibleWith(mediaType)));
132+
return this.responseSpec;
133+
}
134+
135+
/**
136+
* Expect a "Content-Type" header compatible with the given value.
137+
*/
138+
public WebTestClient.ResponseSpec contentTypeCompatibleWith(String mediaType) {
139+
return contentTypeCompatibleWith(MediaType.parseMediaType(mediaType));
140+
}
141+
102142
/**
103143
* Expect an "Expires" header with the given value.
104144
*/

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

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,48 @@ public void valueMatches() {
124124
}
125125
}
126126

127+
@Test
128+
public void doesNotExist() {
129+
HttpHeaders headers = new HttpHeaders();
130+
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
131+
HeaderAssertions assertions = headerAssertions(headers);
132+
133+
// Success
134+
assertions.doesNotExist("Framework");
135+
136+
try {
137+
assertions.doesNotExist("Content-Type");
138+
fail("Existing header expected");
139+
}
140+
catch (AssertionError error) {
141+
Throwable cause = error.getCause();
142+
assertNotNull(cause);
143+
assertEquals("Response header 'Content-Type' exists with " +
144+
"value=[application/json;charset=UTF-8]", cause.getMessage());
145+
}
146+
}
147+
148+
@Test
149+
public void contentTypeCompatibleWith() {
150+
HttpHeaders headers = new HttpHeaders();
151+
headers.setContentType(MediaType.APPLICATION_XML);
152+
HeaderAssertions assertions = headerAssertions(headers);
153+
154+
// Success
155+
assertions.contentTypeCompatibleWith(MediaType.parseMediaType("application/*"));
156+
157+
try {
158+
assertions.contentTypeCompatibleWith(MediaType.TEXT_XML);
159+
fail("MediaTypes not compatible expected");
160+
}
161+
catch (AssertionError error) {
162+
Throwable cause = error.getCause();
163+
assertNotNull(cause);
164+
assertEquals("Response header 'Content-Type'=[application/xml] " +
165+
"is not compatible with [text/xml]", cause.getMessage());
166+
}
167+
}
168+
127169
@Test
128170
public void cacheControl() {
129171
CacheControl control = CacheControl.maxAge(1, TimeUnit.HOURS).noTransform();

0 commit comments

Comments
 (0)