Issue #4057 - NPE in HttpFields.containsKey

+ Preventing HttpField from having a null name.

Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
Joakim Erdfelt 2019-09-05 10:04:25 -05:00
parent 49ba6d1acb
commit 91d3ddced6
2 changed files with 46 additions and 3 deletions

View File

@ -37,7 +37,14 @@ public class HttpField
public HttpField(HttpHeader header, String name, String value)
{
_header = header;
_name = name;
if (_header != null && name == null)
{
_name = _header.asString();
}
else
{
_name = Objects.requireNonNull(name);
}
_value = value;
}
@ -325,8 +332,6 @@ public class HttpField
return false;
if (!_name.equalsIgnoreCase(field.getName()))
return false;
if (_value == null && field.getValue() != null)
return false;
return Objects.equals(_value, field.getValue());
}

View File

@ -30,6 +30,8 @@ import java.util.NoSuchElementException;
import org.eclipse.jetty.util.BufferUtil;
import org.hamcrest.Matchers;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.containsString;
@ -666,6 +668,42 @@ public class HttpFieldsTest
assertFalse(header.containsKey("n11"));
}
@ParameterizedTest
@ValueSource(strings = {"Host", "host", "HOST", "HoSt", "Connection", "CONNECTION", "connection", "CoNnEcTiOn"})
public void testContainsKeyTrue(String keyName)
{
HttpFields fields = new HttpFields();
fields.put("Host", "localhost");
HttpField namelessField = new HttpField(HttpHeader.CONNECTION, null, "bogus");
fields.put(namelessField);
assertTrue(fields.containsKey(keyName), "containsKey('" + keyName + "')");
}
@ParameterizedTest
@ValueSource(strings = {"Content-Type", "Content-Length", "X-Bogus", ""})
public void testContainsKeyFalse(String keyName)
{
HttpFields fields = new HttpFields();
fields.add("Host", "localhost");
HttpField namelessField = new HttpField(HttpHeader.CONNECTION, null, "bogus");
fields.put(namelessField);
System.out.println(fields);
assertFalse(fields.containsKey(keyName), "containsKey('" + keyName + "')");
}
@Test
public void testPreventNullField()
{
HttpFields fields = new HttpFields();
assertThrows(NullPointerException.class, () ->
{
HttpField nullNullField = new HttpField(null, null, "bogus");
fields.put(nullNullField);
});
}
@Test
public void testIteration() throws Exception
{