Skip to content

Commit 4bf5a02

Browse files
bclozelrstoyanchev
authored andcommitted
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
1 parent e4479c8 commit 4bf5a02

File tree

2 files changed

+34
-5
lines changed

2 files changed

+34
-5
lines changed

spring-test-mvc/src/main/java/org/springframework/test/web/servlet/result/HeaderResultMatchers.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@
1616

1717
package org.springframework.test.web.servlet.result;
1818

19-
import static org.springframework.test.util.AssertionErrors.assertEquals;
20-
import static org.springframework.test.util.AssertionErrors.assertTrue;
21-
import static org.springframework.test.util.MatcherAssertionErrors.assertThat;
22-
2319
import org.hamcrest.Matcher;
2420
import org.springframework.test.web.servlet.MvcResult;
2521
import org.springframework.test.web.servlet.ResultMatcher;
2622

23+
import static org.springframework.test.util.AssertionErrors.*;
24+
import static org.springframework.test.util.MatcherAssertionErrors.*;
25+
2726
/**
2827
* Factory for response header assertions. An instance of this
2928
* class is usually accessed via {@link MockMvcResultMatchers#header()}.
@@ -69,6 +68,20 @@ public void match(MvcResult result) {
6968
};
7069
}
7170

71+
/**
72+
* Assert that the named response header does not exist.
73+
* @since 4.0
74+
*/
75+
public ResultMatcher doesNotExist(final String name) {
76+
return new ResultMatcher() {
77+
78+
@Override
79+
public void match(MvcResult result) {
80+
assertTrue("Response should not contain header " + name, !result.getResponse().containsHeader(name));
81+
}
82+
};
83+
}
84+
7285
/**
7386
* Assert the primary value of the named response header as a {@code long}.
7487
*

spring-test-mvc/src/test/java/org/springframework/test/web/servlet/samples/standalone/resultmatchers/HeaderAssertionTests.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535

3636
/**
3737
* Examples of expectations on response header values.
38-
*
38+
*
3939
* @author Rossen Stoyanchev
4040
* @author Sam Brannen
4141
*/
@@ -110,6 +110,22 @@ public void longValueWithMissingResponseHeader() throws Exception {
110110
}
111111
}
112112

113+
// SPR-10771
114+
115+
@Test
116+
public void doesNotExist() throws Exception {
117+
this.mockMvc.perform(get("/persons/1"))
118+
.andExpect(header().doesNotExist("X-Custom-Header"));
119+
}
120+
121+
// SPR-10771
122+
123+
@Test(expected = AssertionError.class)
124+
public void doesNotExistFail() throws Exception {
125+
this.mockMvc.perform(get("/persons/1"))
126+
.andExpect(header().doesNotExist(LAST_MODIFIED));
127+
}
128+
113129
@Test
114130
public void stringWithIncorrectResponseHeaderValue() throws Exception {
115131
long unexpected = currentTime + 1;

0 commit comments

Comments
 (0)