Add headerDoesNotExist() to MockRestRequestMatchers

Prior to this commit, one could not test for the absence of a specific
HTTP header in a request.

This commit adds a headerDoesNotExist() method in MockRestRequestMatchers.

Closes gh-23721
This commit is contained in:
Sam Brannen
2019-09-27 17:38:11 +02:00
parent 4dc966b04b
commit 06563d8b4b
2 changed files with 28 additions and 0 deletions

View File

@@ -19,6 +19,7 @@ package org.springframework.test.web.client.match;
import java.net.URI;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.Test;
@@ -93,6 +94,19 @@ public class MockRestRequestMatchersTests {
MockRestRequestMatchers.header("foo", "bar", "baz").match(this.request);
}
@Test
public void headerDoesNotExist() throws Exception {
MockRestRequestMatchers.headerDoesNotExist(null).match(this.request);
MockRestRequestMatchers.headerDoesNotExist("").match(this.request);
MockRestRequestMatchers.headerDoesNotExist("foo").match(this.request);
List<String> values = Arrays.asList("bar", "baz");
this.request.getHeaders().put("foo", values);
assertThatThrownBy(() -> MockRestRequestMatchers.headerDoesNotExist("foo").match(this.request))
.isInstanceOf(AssertionError.class)
.hasMessage("Expected header <foo> not to exist, but it exists with values: " + values);
}
@Test
public void headerMissing() throws Exception {
assertThatThrownBy(() -> MockRestRequestMatchers.header("foo", "bar").match(this.request))