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:
Brian Clozel
2017-12-21 20:27:36 +01:00
parent 7035ee7ebb
commit 6e587d5c57
2 changed files with 83 additions and 1 deletions

View File

@@ -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();