Make HttpHeaders.set(self) a no-op consistently rather than having some implementations throw and others not

This commit is contained in:
Louis Ryan 2015-11-05 13:23:06 -08:00 committed by nmittler
parent fd810e7178
commit 4af06799ea
3 changed files with 18 additions and 14 deletions

View File

@ -77,15 +77,14 @@ public class DefaultHttpHeaders extends HttpHeaders {
@Override
public HttpHeaders set(HttpHeaders headers) {
if (headers instanceof DefaultHttpHeaders) {
if (headers == this) {
throw new IllegalArgumentException("can't add to itself.");
}
clear();
DefaultHttpHeaders defaultHttpHeaders = (DefaultHttpHeaders) headers;
HeaderEntry e = defaultHttpHeaders.head.after;
while (e != defaultHttpHeaders.head) {
add(e.key, e.value);
e = e.after;
if (headers != this) {
clear();
DefaultHttpHeaders defaultHttpHeaders = (DefaultHttpHeaders) headers;
HeaderEntry e = defaultHttpHeaders.head.after;
while (e != defaultHttpHeaders.head) {
add(e.key, e.value);
e = e.after;
}
}
return this;
} else {

View File

@ -1617,9 +1617,11 @@ public abstract class HttpHeaders implements Iterable<Map.Entry<String, String>>
if (headers == null) {
throw new NullPointerException("headers");
}
clear();
for (Map.Entry<String, String> e: headers) {
add(e.getKey(), e.getValue());
if (headers != this) {
clear();
for (Map.Entry<String, String> e : headers) {
add(e.getKey(), e.getValue());
}
}
return this;
}

View File

@ -74,9 +74,12 @@ public class HttpHeadersTest {
headers.add(headers);
}
@Test(expected = IllegalArgumentException.class)
public void testSetSelf() {
@Test
public void testSetSelfIsNoOp() {
HttpHeaders headers = new DefaultHttpHeaders(false);
headers.add("some", "thing");
headers.set(headers);
Assert.assertEquals(1, headers.entries().size());
Assert.assertEquals("thing", headers.get("some"));
}
}