Set the Transfer-Encoding header instead of adding

Motivation:

HttpUtil.setTransferEncodingChunked could add a second Transfer-Encoding
header if one was already present. While this is technically valid, it
does not appear to be the intent of the method.

Result:

Only one Transfer-Encoding header is present after calling this method.
This commit is contained in:
Chris Conroy 2017-01-24 13:56:25 -05:00 committed by Norman Maurer
parent d509365974
commit 9bec25a6eb
2 changed files with 11 additions and 1 deletions

View File

@ -312,7 +312,7 @@ public final class HttpUtil {
*/
public static void setTransferEncodingChunked(HttpMessage m, boolean chunked) {
if (chunked) {
m.headers().add(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
m.headers().set(HttpHeaderNames.TRANSFER_ENCODING, HttpHeaderValues.CHUNKED);
m.headers().remove(HttpHeaderNames.CONTENT_LENGTH);
} else {
List<String> encodings = m.headers().getAll(HttpHeaderNames.TRANSFER_ENCODING);

View File

@ -18,6 +18,7 @@ package io.netty.handler.codec.http;
import io.netty.util.CharsetUtil;
import org.junit.Test;
import java.util.Collections;
import java.util.List;
import static io.netty.handler.codec.http.HttpHeadersTestUtils.of;
@ -108,4 +109,13 @@ public class HttpUtilTest {
assertEquals("bar", message.headers().get(HttpHeaderNames.CONTENT_LENGTH));
assertEquals(1L, HttpUtil.getContentLength(message, 1L));
}
@Test
public void testDoubleChunkedHeader() {
HttpMessage message = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
message.headers().add(HttpHeaderNames.TRANSFER_ENCODING, "chunked");
HttpUtil.setTransferEncodingChunked(message, true);
List<String> expected = Collections.singletonList("chunked");
assertEquals(expected, message.headers().getAll(HttpHeaderNames.TRANSFER_ENCODING));
}
}