diff --git a/spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java b/spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java index 593a889842..365f43b24e 100644 --- a/spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java +++ b/spring-test/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java @@ -99,6 +99,15 @@ public class HeaderResultMatchers { !result.getResponse().containsHeader(name)); } + /** + * Assert that the named response header does exist. + * @since 4.3 + */ + public ResultMatcher doesExist(final String name) { + return result -> assertTrue("Response should contain header '" + name + "'", + result.getResponse().containsHeader(name)); + } + /** * Assert the primary value of the named response header as a {@code long}. *
The {@link ResultMatcher} returned by this method throws an diff --git a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/HeaderAssertionTests.java b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/HeaderAssertionTests.java index 55ae488e42..76aed76745 100644 --- a/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/HeaderAssertionTests.java +++ b/spring-test/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/HeaderAssertionTests.java @@ -154,6 +154,16 @@ public class HeaderAssertionTests { this.mockMvc.perform(get("/persons/1")).andExpect(header().doesNotExist(LAST_MODIFIED)); } + @Test + public void doesExist() throws Exception { + this.mockMvc.perform(get("/persons/1")).andExpect(header().doesExist(LAST_MODIFIED)); + } + + @Test(expected = AssertionError.class) // SPR-10771 + public void doesExistFail() throws Exception { + this.mockMvc.perform(get("/persons/1")).andExpect(header().doesExist("X-Custom-Header")); + } + @Test public void stringWithIncorrectResponseHeaderValue() throws Exception { assertIncorrectResponseHeader(header().string(LAST_MODIFIED, secondLater), secondLater);