Make headers.set(self) a no-op instead of throwing. Makes it consistent with setAll

Motivation:

Makes the API contract of headers more consistent and simpler.

Modifications:

If self is passed to set then simply return

Result:

set and setAll will be consistent
This commit is contained in:
Louis Ryan 2015-11-05 14:28:31 -08:00 committed by nmittler
parent 6504d52b94
commit 3eb65797ed
3 changed files with 9 additions and 5 deletions

View File

@ -79,9 +79,11 @@ public class HttpHeadersTest {
headers.add(headers); headers.add(headers);
} }
@Test(expected = IllegalArgumentException.class) @Test
public void testSetSelf() { public void testSetSelfIsNoOp() {
HttpHeaders headers = new DefaultHttpHeaders(false); HttpHeaders headers = new DefaultHttpHeaders(false);
headers.add("name", "value");
headers.set(headers); headers.set(headers);
assertEquals(1, headers.size());
} }
} }

View File

@ -543,7 +543,7 @@ public class DefaultHeaders<K, V, T extends Headers<K, V, T>> implements Headers
public T set(Headers<? extends K, ? extends V, ?> headers) { public T set(Headers<? extends K, ? extends V, ?> headers) {
checkNotNull(headers, "headers"); checkNotNull(headers, "headers");
if (headers == this) { if (headers == this) {
throw new IllegalArgumentException("can't add to itself."); return thisT();
} }
clear(); clear();
if (headers instanceof DefaultHeaders) { if (headers instanceof DefaultHeaders) {

View File

@ -390,9 +390,11 @@ public class DefaultHeadersTest {
headers.add(headers); headers.add(headers);
} }
@Test(expected = IllegalArgumentException.class) @Test
public void testSetSelf() { public void testSetSelfIsNoOp() {
TestDefaultHeaders headers = newInstance(); TestDefaultHeaders headers = newInstance();
headers.add("name", "value");
headers.set(headers); headers.set(headers);
assertEquals(1, headers.size());
} }
} }