[#4265] Not allow to add/set DefaultHttpHeaders to itself.
Motivation: We should prevent to add/set DefaultHttpHeaders to itself to prevent unexpected side-effects. Modifications: Throw IllegalArgumentException if user tries to pass the same instance to set/add. Result: No surprising side-effects.
This commit is contained in:
parent
2adf6e5358
commit
ca44436ce6
@ -72,4 +72,16 @@ public class HttpHeadersTest {
|
||||
HttpHeaders headers = new DefaultHttpHeaders(false);
|
||||
headers.set(of("test"), (CharSequence) null);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testAddSelf() {
|
||||
HttpHeaders headers = new DefaultHttpHeaders(false);
|
||||
headers.add(headers);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testSetSelf() {
|
||||
HttpHeaders headers = new DefaultHttpHeaders(false);
|
||||
headers.set(headers);
|
||||
}
|
||||
}
|
||||
|
@ -540,7 +540,7 @@ public class DefaultHeaders<T> implements Headers<T> {
|
||||
public Headers<T> set(Headers<? extends T> headers) {
|
||||
checkNotNull(headers, "headers");
|
||||
if (headers == this) {
|
||||
return this;
|
||||
throw new IllegalArgumentException("can't add to itself.");
|
||||
}
|
||||
clear();
|
||||
if (headers instanceof DefaultHeaders) {
|
||||
|
@ -379,6 +379,18 @@ public class DefaultHeadersTest {
|
||||
assertEquals(headers1, expected);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testAddSelf() {
|
||||
Headers<ByteString> headers = newInstance();
|
||||
headers.add(headers);
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
public void testSetSelf() {
|
||||
Headers<ByteString> headers = newInstance();
|
||||
headers.set(headers);
|
||||
}
|
||||
|
||||
private ByteString bs(String value) {
|
||||
return new ByteString(value, CharsetUtil.US_ASCII);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user