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:
parent
7035ee7ebb
commit
6e587d5c57
|
|
@ -25,12 +25,15 @@ import org.springframework.http.HttpHeaders;
|
|||
import org.springframework.http.MediaType;
|
||||
import org.springframework.lang.Nullable;
|
||||
|
||||
import static org.springframework.test.util.AssertionErrors.*;
|
||||
import static org.springframework.test.util.AssertionErrors.assertEquals;
|
||||
import static org.springframework.test.util.AssertionErrors.assertTrue;
|
||||
import static org.springframework.test.util.AssertionErrors.fail;
|
||||
|
||||
/**
|
||||
* Assertions on headers of the response.
|
||||
*
|
||||
* @author Rossen Stoyanchev
|
||||
* @author Brian Clozel
|
||||
* @since 5.0
|
||||
* @see WebTestClient.ResponseSpec#expectHeader()
|
||||
*/
|
||||
|
|
@ -71,6 +74,17 @@ public class HeaderAssertions {
|
|||
return this.responseSpec;
|
||||
}
|
||||
|
||||
/**
|
||||
* Expect that the header with the given name is not present.
|
||||
*/
|
||||
public WebTestClient.ResponseSpec doesNotExist(String name) {
|
||||
if (getHeaders().containsKey(name)) {
|
||||
String message = getMessage(name) + " exists with value=[" + getHeaders().getFirst(name) + "]";
|
||||
this.exchangeResult.assertWithDiagnostics(() -> fail(message));
|
||||
}
|
||||
return this.responseSpec;
|
||||
}
|
||||
|
||||
/**
|
||||
* Expect a "Cache-Control" header with the given value.
|
||||
*/
|
||||
|
|
@ -99,6 +113,32 @@ public class HeaderAssertions {
|
|||
return assertHeader("Content-Type", mediaType, getHeaders().getContentType());
|
||||
}
|
||||
|
||||
/**
|
||||
* Expect a "Content-Type" header with the given value.
|
||||
*/
|
||||
public WebTestClient.ResponseSpec contentType(String mediaType) {
|
||||
return contentType(MediaType.parseMediaType(mediaType));
|
||||
}
|
||||
|
||||
/**
|
||||
* Expect a "Content-Type" header compatible with the given value.
|
||||
*/
|
||||
public WebTestClient.ResponseSpec contentTypeCompatibleWith(MediaType mediaType) {
|
||||
MediaType actual = getHeaders().getContentType();
|
||||
String message = getMessage("Content-Type") + "=[" + actual.toString() + "]"
|
||||
+ " is not compatible with [" + mediaType.toString() + "]";
|
||||
this.exchangeResult.assertWithDiagnostics(() ->
|
||||
assertTrue(message, actual.isCompatibleWith(mediaType)));
|
||||
return this.responseSpec;
|
||||
}
|
||||
|
||||
/**
|
||||
* Expect a "Content-Type" header compatible with the given value.
|
||||
*/
|
||||
public WebTestClient.ResponseSpec contentTypeCompatibleWith(String mediaType) {
|
||||
return contentTypeCompatibleWith(MediaType.parseMediaType(mediaType));
|
||||
}
|
||||
|
||||
/**
|
||||
* Expect an "Expires" header with the given value.
|
||||
*/
|
||||
|
|
|
|||
|
|
@ -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();
|
||||
|
|
|
|||
Loading…
Reference in New Issue