From 4bf5a0234c6c5c5d6591eb0fa52db3bf09573d9f Mon Sep 17 00:00:00 2001 From: Brian Clozel Date: Fri, 4 Oct 2013 17:12:39 +0200 Subject: [PATCH] Add doesNotExist match to HeaderResultMatchers Prior to this commit, one could not test for the absence of a specific HTTP header in a response. Using header("X-Custom-Header", Matchers.nullValue()) would not work because it tests for an empty value of an existing header. This commit adds a doesNotExist method on the HeaderResultMatcher. Issue: SPR-10771 --- .../servlet/result/HeaderResultMatchers.java | 21 +++++++++++++++---- .../resultmatchers/HeaderAssertionTests.java | 18 +++++++++++++++- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java index 0fef4e43fd5..5b2dd9454c4 100644 --- a/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java +++ b/spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java @@ -16,14 +16,13 @@ package org.springframework.test.web.servlet.result; -import static org.springframework.test.util.AssertionErrors.assertEquals; -import static org.springframework.test.util.AssertionErrors.assertTrue; -import static org.springframework.test.util.MatcherAssertionErrors.assertThat; - import org.hamcrest.Matcher; import org.springframework.test.web.servlet.MvcResult; import org.springframework.test.web.servlet.ResultMatcher; +import static org.springframework.test.util.AssertionErrors.*; +import static org.springframework.test.util.MatcherAssertionErrors.*; + /** * Factory for response header assertions. An instance of this * class is usually accessed via {@link MockMvcResultMatchers#header()}. @@ -69,6 +68,20 @@ public class HeaderResultMatchers { }; } + /** + * Assert that the named response header does not exist. + * @since 4.0 + */ + public ResultMatcher doesNotExist(final String name) { + return new ResultMatcher() { + + @Override + public void match(MvcResult result) { + assertTrue("Response should not contain header " + name, !result.getResponse().containsHeader(name)); + } + }; + } + /** * Assert the primary value of the named response header as a {@code long}. * diff --git a/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/HeaderAssertionTests.java b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/HeaderAssertionTests.java index 1a3f56771c1..df5aacef9ce 100644 --- a/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/HeaderAssertionTests.java +++ b/spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/HeaderAssertionTests.java @@ -35,7 +35,7 @@ import static org.springframework.test.web.servlet.setup.MockMvcBuilders.*; /** * Examples of expectations on response header values. - * + * * @author Rossen Stoyanchev * @author Sam Brannen */ @@ -110,6 +110,22 @@ public class HeaderAssertionTests { } } + // SPR-10771 + + @Test + public void doesNotExist() throws Exception { + this.mockMvc.perform(get("/persons/1")) + .andExpect(header().doesNotExist("X-Custom-Header")); + } + + // SPR-10771 + + @Test(expected = AssertionError.class) + public void doesNotExistFail() throws Exception { + this.mockMvc.perform(get("/persons/1")) + .andExpect(header().doesNotExist(LAST_MODIFIED)); + } + @Test public void stringWithIncorrectResponseHeaderValue() throws Exception { long unexpected = currentTime + 1;