Add unit test to ensure adding null header values is not allowed.

Motivation:

We need to ensure we never allow to have null values set on headers, otherwise we will see a NPE during encoding them.

Modifications:

Add unit test that shows we correctly handle null values.

Result:

Verify correct implementation.
This commit is contained in:
Norman Maurer 2015-06-08 10:04:07 +02:00
parent 7f889be48b
commit 82e580dc7f

View File

@ -0,0 +1,72 @@
/*
* Copyright 2013 The Netty Project
*
* The Netty Project licenses this file to you under the Apache License,
* version 2.0 (the "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at:
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
package io.netty.handler.codec.http;
import io.netty.util.AsciiString;
import org.junit.Test;
import java.util.List;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.*;
public class HttpHeadersTest {
@Test
public void testRemoveTransferEncodingIgnoreCase() {
HttpMessage message = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
message.headers().set(HttpHeaderNames.TRANSFER_ENCODING, "Chunked");
assertFalse(message.headers().isEmpty());
HttpHeaderUtil.setTransferEncodingChunked(message, false);
assertTrue(message.headers().isEmpty());
}
// Test for https://github.com/netty/netty/issues/1690
@Test
public void testGetOperations() {
HttpHeaders headers = new DefaultHttpHeaders();
headers.add("Foo", "1");
headers.add("Foo", "2");
assertEquals("1", headers.get("Foo"));
List<CharSequence> values = headers.getAll("Foo");
assertEquals(2, values.size());
assertEquals("1", values.get(0));
assertEquals("2", values.get(1));
}
@Test
public void testEquansIgnoreCase() {
assertThat(AsciiString.equalsIgnoreCase(null, null), is(true));
assertThat(AsciiString.equalsIgnoreCase(null, "foo"), is(false));
assertThat(AsciiString.equalsIgnoreCase("bar", null), is(false));
assertThat(AsciiString.equalsIgnoreCase("FoO", "fOo"), is(true));
}
@Test(expected = NullPointerException.class)
public void testSetNullHeaderValueValidate() {
HttpHeaders headers = new DefaultHttpHeaders(true);
headers.set("test", (CharSequence) null);
}
@Test(expected = NullPointerException.class)
public void testSetNullHeaderValueNotValidate() {
HttpHeaders headers = new DefaultHttpHeaders(false);
headers.set("test", (CharSequence) null);
}
}