Introduce getOrEmpty(String) convenience method in HttpHeaders

This commit introduces a getOrEmpty(String) method in HttpHeaders that
returns an immutable, empty list if no values are present for the
specified header name. This is provided as a convenience over the
existing get(String) method which returns null in such cases.

Closes gh-22949
This commit is contained in:
Sam Brannen
2019-05-22 15:26:45 +02:00
parent 9ca7354146
commit f806594e60
2 changed files with 29 additions and 0 deletions

View File

@@ -39,8 +39,10 @@ import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.emptyCollectionOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.nullValue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
@@ -61,6 +63,21 @@ public class HttpHeadersTests {
private final HttpHeaders headers = new HttpHeaders();
@Test
public void getOrEmpty() {
String key = "FOO";
assertThat(headers.get(key), is(nullValue()));
assertThat(headers.getOrEmpty(key), is(empty()));
headers.add(key, "bar");
assertThat(headers.getOrEmpty(key), is(Arrays.asList("bar")));
headers.remove(key);
assertThat(headers.get(key), is(nullValue()));
assertThat(headers.getOrEmpty(key), is(empty()));
}
@Test
public void getFirst() {
headers.add(HttpHeaders.CACHE_CONTROL, "max-age=1000, public");