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:
parent
9ca7354146
commit
f806594e60
|
|
@ -71,6 +71,7 @@ import org.springframework.util.StringUtils;
|
||||||
* @author Brian Clozel
|
* @author Brian Clozel
|
||||||
* @author Juergen Hoeller
|
* @author Juergen Hoeller
|
||||||
* @author Josh Long
|
* @author Josh Long
|
||||||
|
* @author Sam Brannen
|
||||||
* @since 3.0
|
* @since 3.0
|
||||||
*/
|
*/
|
||||||
public class HttpHeaders implements MultiValueMap<String, String>, Serializable {
|
public class HttpHeaders implements MultiValueMap<String, String>, Serializable {
|
||||||
|
|
@ -437,6 +438,17 @@ public class HttpHeaders implements MultiValueMap<String, String>, Serializable
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the list of header values for the given header name, if any.
|
||||||
|
* @param headerName the header name
|
||||||
|
* @return the list of header values, or an empty list
|
||||||
|
* @since 5.2
|
||||||
|
*/
|
||||||
|
public List<String> getOrEmpty(Object headerName) {
|
||||||
|
List<String> values = get(headerName);
|
||||||
|
return (values != null ? values : Collections.emptyList());
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Set the list of acceptable {@linkplain MediaType media types},
|
* Set the list of acceptable {@linkplain MediaType media types},
|
||||||
* as specified by the {@code Accept} header.
|
* as specified by the {@code Accept} header.
|
||||||
|
|
|
||||||
|
|
@ -39,8 +39,10 @@ import org.junit.Test;
|
||||||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
|
||||||
import static org.hamcrest.MatcherAssert.assertThat;
|
import static org.hamcrest.MatcherAssert.assertThat;
|
||||||
import static org.hamcrest.Matchers.contains;
|
import static org.hamcrest.Matchers.contains;
|
||||||
|
import static org.hamcrest.Matchers.empty;
|
||||||
import static org.hamcrest.Matchers.emptyCollectionOf;
|
import static org.hamcrest.Matchers.emptyCollectionOf;
|
||||||
import static org.hamcrest.Matchers.is;
|
import static org.hamcrest.Matchers.is;
|
||||||
|
import static org.hamcrest.Matchers.nullValue;
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
import static org.junit.Assert.assertNotNull;
|
import static org.junit.Assert.assertNotNull;
|
||||||
|
|
@ -61,6 +63,21 @@ public class HttpHeadersTests {
|
||||||
private final HttpHeaders headers = new HttpHeaders();
|
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
|
@Test
|
||||||
public void getFirst() {
|
public void getFirst() {
|
||||||
headers.add(HttpHeaders.CACHE_CONTROL, "max-age=1000, public");
|
headers.add(HttpHeaders.CACHE_CONTROL, "max-age=1000, public");
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue