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
This commit is contained in:
@@ -124,6 +124,48 @@ public class HeaderAssertionTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void doesNotExist() {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
|
||||
HeaderAssertions assertions = headerAssertions(headers);
|
||||
|
||||
// Success
|
||||
assertions.doesNotExist("Framework");
|
||||
|
||||
try {
|
||||
assertions.doesNotExist("Content-Type");
|
||||
fail("Existing header expected");
|
||||
}
|
||||
catch (AssertionError error) {
|
||||
Throwable cause = error.getCause();
|
||||
assertNotNull(cause);
|
||||
assertEquals("Response header 'Content-Type' exists with " +
|
||||
"value=[application/json;charset=UTF-8]", cause.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void contentTypeCompatibleWith() {
|
||||
HttpHeaders headers = new HttpHeaders();
|
||||
headers.setContentType(MediaType.APPLICATION_XML);
|
||||
HeaderAssertions assertions = headerAssertions(headers);
|
||||
|
||||
// Success
|
||||
assertions.contentTypeCompatibleWith(MediaType.parseMediaType("application/*"));
|
||||
|
||||
try {
|
||||
assertions.contentTypeCompatibleWith(MediaType.TEXT_XML);
|
||||
fail("MediaTypes not compatible expected");
|
||||
}
|
||||
catch (AssertionError error) {
|
||||
Throwable cause = error.getCause();
|
||||
assertNotNull(cause);
|
||||
assertEquals("Response header 'Content-Type'=[application/xml] " +
|
||||
"is not compatible with [text/xml]", cause.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void cacheControl() {
|
||||
CacheControl control = CacheControl.maxAge(1, TimeUnit.HOURS).noTransform();
|
||||
|
||||
Reference in New Issue
Block a user