Migrate codec-http to junit5 (#11440)

Motivation:

We should update to use junit5 in all modules.

Modifications:

Adjust codec-http tests to use junit5

Result:

Part of https://github.com/netty/netty/issues/10757
This commit is contained in:
Norman Maurer 2021-07-01 18:53:41 +02:00
parent 26a0efcb93
commit f84bfd2dd2
12 changed files with 145 additions and 136 deletions

View File

@ -26,7 +26,8 @@ import io.netty.handler.codec.DecoderResult;
import io.netty.handler.codec.EncoderException; import io.netty.handler.codec.EncoderException;
import io.netty.handler.codec.MessageToByteEncoder; import io.netty.handler.codec.MessageToByteEncoder;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import java.util.concurrent.atomic.AtomicBoolean; import java.util.concurrent.atomic.AtomicBoolean;
@ -39,8 +40,8 @@ import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
public class HttpContentEncoderTest { public class HttpContentEncoderTest {
@ -425,12 +426,13 @@ public class HttpContentEncoderTest {
HttpContent content = new DefaultHttpContent(Unpooled.buffer().writeZero(10)); HttpContent content = new DefaultHttpContent(Unpooled.buffer().writeZero(10));
assertTrue(channel.writeOutbound(content)); assertTrue(channel.writeOutbound(content));
assertEquals(1, content.refCnt()); assertEquals(1, content.refCnt());
try { assertThrows(CodecException.class, new Executable() {
@Override
public void execute() {
channel.finishAndReleaseAll(); channel.finishAndReleaseAll();
fail();
} catch (CodecException expected) {
// expected
} }
});
assertTrue(channelInactiveCalled.get()); assertTrue(channelInactiveCalled.get());
assertEquals(0, content.refCnt()); assertEquals(0, content.refCnt());
} }

View File

@ -20,7 +20,7 @@ import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.DecoderResult; import io.netty.handler.codec.DecoderResult;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.util.Random; import java.util.Random;

View File

@ -27,7 +27,8 @@ import io.netty.util.AsciiString;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil; import io.netty.util.ReferenceCountUtil;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import org.mockito.Mockito; import org.mockito.Mockito;
import java.nio.channels.ClosedChannelException; import java.nio.channels.ClosedChannelException;
@ -43,8 +44,8 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertSame; import static org.junit.jupiter.api.Assertions.assertSame;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
public class HttpObjectAggregatorTest { public class HttpObjectAggregatorTest {
@ -119,11 +120,11 @@ public class HttpObjectAggregatorTest {
@Test @Test
public void testOversizedRequest() { public void testOversizedRequest() {
EmbeddedChannel embedder = new EmbeddedChannel(new HttpObjectAggregator(4)); final EmbeddedChannel embedder = new EmbeddedChannel(new HttpObjectAggregator(4));
HttpRequest message = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.PUT, "http://localhost"); HttpRequest message = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.PUT, "http://localhost");
HttpContent chunk1 = new DefaultHttpContent(Unpooled.copiedBuffer("test", CharsetUtil.US_ASCII)); HttpContent chunk1 = new DefaultHttpContent(Unpooled.copiedBuffer("test", CharsetUtil.US_ASCII));
HttpContent chunk2 = new DefaultHttpContent(Unpooled.copiedBuffer("test2", CharsetUtil.US_ASCII)); HttpContent chunk2 = new DefaultHttpContent(Unpooled.copiedBuffer("test2", CharsetUtil.US_ASCII));
HttpContent chunk3 = LastHttpContent.EMPTY_LAST_CONTENT; final HttpContent chunk3 = LastHttpContent.EMPTY_LAST_CONTENT;
assertFalse(embedder.writeInbound(message)); assertFalse(embedder.writeInbound(message));
assertFalse(embedder.writeInbound(chunk1)); assertFalse(embedder.writeInbound(chunk1));
@ -134,12 +135,12 @@ public class HttpObjectAggregatorTest {
assertEquals("0", response.headers().get(HttpHeaderNames.CONTENT_LENGTH)); assertEquals("0", response.headers().get(HttpHeaderNames.CONTENT_LENGTH));
assertFalse(embedder.isOpen()); assertFalse(embedder.isOpen());
try { assertThrows(ClosedChannelException.class, new Executable() {
assertFalse(embedder.writeInbound(chunk3)); @Override
fail(); public void execute() {
} catch (Exception e) { embedder.writeInbound(chunk3);
assertTrue(e instanceof ClosedChannelException);
} }
});
assertFalse(embedder.finish()); assertFalse(embedder.finish());
} }
@ -211,7 +212,7 @@ public class HttpObjectAggregatorTest {
} }
private static void checkOversizedRequest(HttpRequest message) { private static void checkOversizedRequest(HttpRequest message) {
EmbeddedChannel embedder = new EmbeddedChannel(new HttpObjectAggregator(4)); final EmbeddedChannel embedder = new EmbeddedChannel(new HttpObjectAggregator(4));
assertFalse(embedder.writeInbound(message)); assertFalse(embedder.writeInbound(message));
HttpResponse response = embedder.readOutbound(); HttpResponse response = embedder.readOutbound();
@ -224,13 +225,13 @@ public class HttpObjectAggregatorTest {
if (serverShouldCloseConnection(message, response)) { if (serverShouldCloseConnection(message, response)) {
assertFalse(embedder.isOpen()); assertFalse(embedder.isOpen());
try { assertThrows(ClosedChannelException.class, new Executable() {
@Override
public void execute() {
embedder.writeInbound(new DefaultHttpContent(Unpooled.EMPTY_BUFFER)); embedder.writeInbound(new DefaultHttpContent(Unpooled.EMPTY_BUFFER));
fail();
} catch (Exception e) {
assertThat(e, instanceOf(ClosedChannelException.class));
// expected
} }
});
assertFalse(embedder.finish()); assertFalse(embedder.finish());
} else { } else {
assertTrue(embedder.isOpen()); assertTrue(embedder.isOpen());
@ -281,44 +282,59 @@ public class HttpObjectAggregatorTest {
@Test @Test
public void testOversizedResponse() { public void testOversizedResponse() {
EmbeddedChannel embedder = new EmbeddedChannel(new HttpObjectAggregator(4)); final EmbeddedChannel embedder = new EmbeddedChannel(new HttpObjectAggregator(4));
HttpResponse message = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK); HttpResponse message = new DefaultHttpResponse(HttpVersion.HTTP_1_1, HttpResponseStatus.OK);
HttpContent chunk1 = new DefaultHttpContent(Unpooled.copiedBuffer("test", CharsetUtil.US_ASCII)); HttpContent chunk1 = new DefaultHttpContent(Unpooled.copiedBuffer("test", CharsetUtil.US_ASCII));
HttpContent chunk2 = new DefaultHttpContent(Unpooled.copiedBuffer("test2", CharsetUtil.US_ASCII)); final HttpContent chunk2 = new DefaultHttpContent(Unpooled.copiedBuffer("test2", CharsetUtil.US_ASCII));
assertFalse(embedder.writeInbound(message)); assertFalse(embedder.writeInbound(message));
assertFalse(embedder.writeInbound(chunk1)); assertFalse(embedder.writeInbound(chunk1));
try { assertThrows(TooLongFrameException.class, new Executable() {
@Override
public void execute() {
embedder.writeInbound(chunk2); embedder.writeInbound(chunk2);
fail();
} catch (TooLongFrameException expected) {
// Expected
} }
});
assertFalse(embedder.isOpen()); assertFalse(embedder.isOpen());
assertFalse(embedder.finish()); assertFalse(embedder.finish());
} }
@Test(expected = IllegalArgumentException.class) @Test
public void testInvalidConstructorUsage() { public void testInvalidConstructorUsage() {
assertThrows(IllegalArgumentException.class, new Executable() {
@Override
public void execute() {
new HttpObjectAggregator(-1); new HttpObjectAggregator(-1);
} }
});
@Test(expected = IllegalArgumentException.class)
public void testInvalidMaxCumulationBufferComponents() {
HttpObjectAggregator aggr = new HttpObjectAggregator(Integer.MAX_VALUE);
aggr.setMaxCumulationBufferComponents(1);
} }
@Test(expected = IllegalStateException.class) @Test
public void testInvalidMaxCumulationBufferComponents() {
final HttpObjectAggregator aggr = new HttpObjectAggregator(Integer.MAX_VALUE);
assertThrows(IllegalArgumentException.class, new Executable() {
@Override
public void execute() {
aggr.setMaxCumulationBufferComponents(1);
}
});
}
@Test
public void testSetMaxCumulationBufferComponentsAfterInit() throws Exception { public void testSetMaxCumulationBufferComponentsAfterInit() throws Exception {
HttpObjectAggregator aggr = new HttpObjectAggregator(Integer.MAX_VALUE); final HttpObjectAggregator aggr = new HttpObjectAggregator(Integer.MAX_VALUE);
ChannelHandlerContext ctx = Mockito.mock(ChannelHandlerContext.class); ChannelHandlerContext ctx = Mockito.mock(ChannelHandlerContext.class);
aggr.handlerAdded(ctx); aggr.handlerAdded(ctx);
Mockito.verifyNoMoreInteractions(ctx); Mockito.verifyNoMoreInteractions(ctx);
assertThrows(IllegalStateException.class, new Executable() {
@Override
public void execute() {
aggr.setMaxCumulationBufferComponents(10); aggr.setMaxCumulationBufferComponents(10);
} }
});
}
@Test @Test
public void testAggregateTransferEncodingChunked() { public void testAggregateTransferEncodingChunked() {

View File

@ -20,8 +20,7 @@ import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.TooLongFrameException; import io.netty.handler.codec.TooLongFrameException;
import io.netty.util.AsciiString; import io.netty.util.AsciiString;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import org.junit.jupiter.api.Test;
import org.junit.Test;
import java.util.List; import java.util.List;

View File

@ -21,7 +21,8 @@ import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.DecoderResult; import io.netty.handler.codec.DecoderResult;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import io.netty.util.IllegalReferenceCountException; import io.netty.util.IllegalReferenceCountException;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
@ -33,8 +34,8 @@ import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;
/** /**
*/ */
@ -137,15 +138,17 @@ public class HttpRequestEncoderTest {
@Test @Test
public void testEmptyReleasedBufferShouldNotWriteEmptyBufferToChannel() throws Exception { public void testEmptyReleasedBufferShouldNotWriteEmptyBufferToChannel() throws Exception {
HttpRequestEncoder encoder = new HttpRequestEncoder(); HttpRequestEncoder encoder = new HttpRequestEncoder();
EmbeddedChannel channel = new EmbeddedChannel(encoder); final EmbeddedChannel channel = new EmbeddedChannel(encoder);
ByteBuf buf = Unpooled.buffer(); final ByteBuf buf = Unpooled.buffer();
buf.release(); buf.release();
try { ExecutionException e = assertThrows(ExecutionException.class, new Executable() {
@Override
public void execute() throws Throwable {
channel.writeAndFlush(buf).get(); channel.writeAndFlush(buf).get();
fail();
} catch (ExecutionException e) {
assertThat(e.getCause().getCause(), is(instanceOf(IllegalReferenceCountException.class)));
} }
});
assertThat(e.getCause().getCause(), is(instanceOf(IllegalReferenceCountException.class)));
channel.finishAndReleaseAll(); channel.finishAndReleaseAll();
} }

View File

@ -17,7 +17,7 @@ package io.netty.handler.codec.http;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.ReferenceCountUtil; import io.netty.util.ReferenceCountUtil;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.*; import static org.hamcrest.CoreMatchers.*;
import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.MatcherAssert.assertThat;

View File

@ -16,13 +16,11 @@
package io.netty.handler.codec.http; package io.netty.handler.codec.http;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.AsciiString;
import io.netty.util.ReferenceCountUtil; import io.netty.util.ReferenceCountUtil;
import io.netty.util.internal.StringUtil; import org.junit.jupiter.api.BeforeEach;
import org.junit.Before; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.Test; import org.junit.jupiter.params.provider.MethodSource;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
@ -41,7 +39,6 @@ import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@RunWith(Parameterized.class)
public class HttpServerKeepAliveHandlerTest { public class HttpServerKeepAliveHandlerTest {
private static final String REQUEST_KEEP_ALIVE = "REQUEST_KEEP_ALIVE"; private static final String REQUEST_KEEP_ALIVE = "REQUEST_KEEP_ALIVE";
private static final int NOT_SELF_DEFINED_MSG_LENGTH = 0; private static final int NOT_SELF_DEFINED_MSG_LENGTH = 0;
@ -49,16 +46,14 @@ public class HttpServerKeepAliveHandlerTest {
private static final int SET_MULTIPART = 2; private static final int SET_MULTIPART = 2;
private static final int SET_CHUNKED = 4; private static final int SET_CHUNKED = 4;
private final boolean isKeepAliveResponseExpected;
private final HttpVersion httpVersion;
private final HttpResponseStatus responseStatus;
private final String sendKeepAlive;
private final int setSelfDefinedMessageLength;
private final String setResponseConnection;
private EmbeddedChannel channel; private EmbeddedChannel channel;
@Parameters @BeforeEach
public static Collection<Object[]> keepAliveProvider() { public void setUp() {
channel = new EmbeddedChannel(new HttpServerKeepAliveHandler());
}
static Collection<Object[]> keepAliveProvider() {
return Arrays.asList(new Object[][] { return Arrays.asList(new Object[][] {
{ true, HttpVersion.HTTP_1_0, OK, REQUEST_KEEP_ALIVE, SET_RESPONSE_LENGTH, KEEP_ALIVE }, // 0 { true, HttpVersion.HTTP_1_0, OK, REQUEST_KEEP_ALIVE, SET_RESPONSE_LENGTH, KEEP_ALIVE }, // 0
{ true, HttpVersion.HTTP_1_0, OK, REQUEST_KEEP_ALIVE, SET_MULTIPART, KEEP_ALIVE }, // 1 { true, HttpVersion.HTTP_1_0, OK, REQUEST_KEEP_ALIVE, SET_MULTIPART, KEEP_ALIVE }, // 1
@ -78,31 +73,19 @@ public class HttpServerKeepAliveHandlerTest {
}); });
} }
public HttpServerKeepAliveHandlerTest(boolean isKeepAliveResponseExpected, HttpVersion httpVersion, @ParameterizedTest
HttpResponseStatus responseStatus, String sendKeepAlive, @MethodSource("keepAliveProvider")
int setSelfDefinedMessageLength, CharSequence setResponseConnection) { public void test_KeepAlive(boolean isKeepAliveResponseExpected, HttpVersion httpVersion,
this.isKeepAliveResponseExpected = isKeepAliveResponseExpected; HttpResponseStatus responseStatus,
this.httpVersion = httpVersion; String sendKeepAlive, int setSelfDefinedMessageLength,
this.responseStatus = responseStatus; AsciiString setResponseConnection) throws Exception {
this.sendKeepAlive = sendKeepAlive;
this.setSelfDefinedMessageLength = setSelfDefinedMessageLength;
this.setResponseConnection = setResponseConnection == null? null : setResponseConnection.toString();
}
@Before
public void setUp() {
channel = new EmbeddedChannel(new HttpServerKeepAliveHandler());
}
@Test
public void test_KeepAlive() throws Exception {
FullHttpRequest request = new DefaultFullHttpRequest(httpVersion, HttpMethod.GET, "/v1/foo/bar"); FullHttpRequest request = new DefaultFullHttpRequest(httpVersion, HttpMethod.GET, "/v1/foo/bar");
setKeepAlive(request, REQUEST_KEEP_ALIVE.equals(sendKeepAlive)); setKeepAlive(request, REQUEST_KEEP_ALIVE.equals(sendKeepAlive));
HttpResponse response = new DefaultFullHttpResponse(httpVersion, responseStatus); HttpResponse response = new DefaultFullHttpResponse(httpVersion, responseStatus);
if (!StringUtil.isNullOrEmpty(setResponseConnection)) { if (setResponseConnection != null) {
response.headers().set(HttpHeaderNames.CONNECTION, setResponseConnection); response.headers().set(HttpHeaderNames.CONNECTION, setResponseConnection);
} }
setupMessageLength(response); setupMessageLength(response, setSelfDefinedMessageLength);
assertTrue(channel.writeInbound(request)); assertTrue(channel.writeInbound(request));
Object requestForwarded = channel.readInbound(); Object requestForwarded = channel.readInbound();
@ -117,11 +100,27 @@ public class HttpServerKeepAliveHandlerTest {
assertFalse(channel.finishAndReleaseAll()); assertFalse(channel.finishAndReleaseAll());
} }
@Test static Collection<Object[]> connectionCloseProvider() {
public void testConnectionCloseHeaderHandledCorrectly() throws Exception { return Arrays.asList(new Object[][] {
{ HttpVersion.HTTP_1_0, OK, SET_RESPONSE_LENGTH },
{ HttpVersion.HTTP_1_0, OK, SET_MULTIPART },
{ HttpVersion.HTTP_1_0, OK, NOT_SELF_DEFINED_MSG_LENGTH },
{ HttpVersion.HTTP_1_0, NO_CONTENT, NOT_SELF_DEFINED_MSG_LENGTH },
{ HttpVersion.HTTP_1_1, OK, SET_RESPONSE_LENGTH },
{ HttpVersion.HTTP_1_1, OK, SET_MULTIPART },
{ HttpVersion.HTTP_1_1, OK, NOT_SELF_DEFINED_MSG_LENGTH },
{ HttpVersion.HTTP_1_1, OK, SET_CHUNKED },
{ HttpVersion.HTTP_1_1, NO_CONTENT, NOT_SELF_DEFINED_MSG_LENGTH }
});
}
@ParameterizedTest
@MethodSource("connectionCloseProvider")
public void testConnectionCloseHeaderHandledCorrectly(
HttpVersion httpVersion, HttpResponseStatus responseStatus, int setSelfDefinedMessageLength) {
HttpResponse response = new DefaultFullHttpResponse(httpVersion, responseStatus); HttpResponse response = new DefaultFullHttpResponse(httpVersion, responseStatus);
response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE); response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
setupMessageLength(response); setupMessageLength(response, setSelfDefinedMessageLength);
channel.writeAndFlush(response); channel.writeAndFlush(response);
HttpResponse writtenResponse = channel.readOutbound(); HttpResponse writtenResponse = channel.readOutbound();
@ -131,8 +130,12 @@ public class HttpServerKeepAliveHandlerTest {
assertFalse(channel.finishAndReleaseAll()); assertFalse(channel.finishAndReleaseAll());
} }
@Test @ParameterizedTest
public void test_PipelineKeepAlive() { @MethodSource("keepAliveProvider")
public void testPipelineKeepAlive(boolean isKeepAliveResponseExpected, HttpVersion httpVersion,
HttpResponseStatus responseStatus,
String sendKeepAlive, int setSelfDefinedMessageLength,
AsciiString setResponseConnection) {
FullHttpRequest firstRequest = new DefaultFullHttpRequest(httpVersion, HttpMethod.GET, "/v1/foo/bar"); FullHttpRequest firstRequest = new DefaultFullHttpRequest(httpVersion, HttpMethod.GET, "/v1/foo/bar");
setKeepAlive(firstRequest, true); setKeepAlive(firstRequest, true);
FullHttpRequest secondRequest = new DefaultFullHttpRequest(httpVersion, HttpMethod.GET, "/v1/foo/bar"); FullHttpRequest secondRequest = new DefaultFullHttpRequest(httpVersion, HttpMethod.GET, "/v1/foo/bar");
@ -167,12 +170,12 @@ public class HttpServerKeepAliveHandlerTest {
assertTrue(isKeepAlive(writtenInfoResp), "response keep-alive"); assertTrue(isKeepAlive(writtenInfoResp), "response keep-alive");
ReferenceCountUtil.release(writtenInfoResp); ReferenceCountUtil.release(writtenInfoResp);
if (!StringUtil.isNullOrEmpty(setResponseConnection)) { if (setResponseConnection != null) {
response.headers().set(HttpHeaderNames.CONNECTION, setResponseConnection); response.headers().set(HttpHeaderNames.CONNECTION, setResponseConnection);
} else { } else {
response.headers().remove(HttpHeaderNames.CONNECTION); response.headers().remove(HttpHeaderNames.CONNECTION);
} }
setupMessageLength(response); setupMessageLength(response, setSelfDefinedMessageLength);
channel.writeAndFlush(response.retainedDuplicate()); channel.writeAndFlush(response.retainedDuplicate());
HttpResponse secondResponse = channel.readOutbound(); HttpResponse secondResponse = channel.readOutbound();
assertEquals(isKeepAliveResponseExpected, channel.isOpen(), "channel.isOpen"); assertEquals(isKeepAliveResponseExpected, channel.isOpen(), "channel.isOpen");
@ -193,7 +196,7 @@ public class HttpServerKeepAliveHandlerTest {
assertFalse(channel.finishAndReleaseAll()); assertFalse(channel.finishAndReleaseAll());
} }
private void setupMessageLength(HttpResponse response) { private static void setupMessageLength(HttpResponse response, int setSelfDefinedMessageLength) {
switch (setSelfDefinedMessageLength) { switch (setSelfDefinedMessageLength) {
case NOT_SELF_DEFINED_MSG_LENGTH: case NOT_SELF_DEFINED_MSG_LENGTH:
if (isContentLengthSet(response)) { if (isContentLengthSet(response)) {

View File

@ -18,8 +18,6 @@ package io.netty.handler.codec.http;
import java.util.Collection; import java.util.Collection;
import java.util.Collections; import java.util.Collections;
import org.junit.Test;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFutureListener; import io.netty.channel.ChannelFutureListener;
@ -30,6 +28,7 @@ import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeCodec; import io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeCodec;
import io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeCodecFactory; import io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeCodecFactory;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
@ -40,7 +39,7 @@ import static org.junit.jupiter.api.Assertions.fail;
public class HttpServerUpgradeHandlerTest { public class HttpServerUpgradeHandlerTest {
private class TestUpgradeCodec implements UpgradeCodec { private static class TestUpgradeCodec implements UpgradeCodec {
@Override @Override
public Collection<CharSequence> requiredUpgradeHeaders() { public Collection<CharSequence> requiredUpgradeHeaders() {
return Collections.emptyList(); return Collections.emptyList();

View File

@ -17,7 +17,7 @@ package io.netty.handler.codec.http;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import io.netty.util.ReferenceCountUtil; import io.netty.util.ReferenceCountUtil;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable; import org.junit.jupiter.api.function.Executable;
import java.net.InetAddress; import java.net.InetAddress;

View File

@ -18,11 +18,9 @@ package io.netty.handler.codec.http;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import org.junit.Before; import org.junit.jupiter.api.Test;
import org.junit.Test; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.runner.RunWith; import org.junit.jupiter.params.provider.MethodSource;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collection; import java.util.Collection;
@ -38,17 +36,9 @@ import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.is;
import static org.hamcrest.core.IsInstanceOf.instanceOf; import static org.hamcrest.core.IsInstanceOf.instanceOf;
@RunWith(Parameterized.class)
public class MultipleContentLengthHeadersTest { public class MultipleContentLengthHeadersTest {
private final boolean allowDuplicateContentLengths; static Collection<Object[]> parameters() {
private final boolean sameValue;
private final boolean singleField;
private EmbeddedChannel channel;
@Parameters
public static Collection<Object[]> parameters() {
return Arrays.asList(new Object[][] { return Arrays.asList(new Object[][] {
{ false, false, false }, { false, false, false },
{ false, false, true }, { false, false, true },
@ -61,27 +51,22 @@ public class MultipleContentLengthHeadersTest {
}); });
} }
public MultipleContentLengthHeadersTest( private static EmbeddedChannel newChannel(boolean allowDuplicateContentLengths) {
boolean allowDuplicateContentLengths, boolean sameValue, boolean singleField) {
this.allowDuplicateContentLengths = allowDuplicateContentLengths;
this.sameValue = sameValue;
this.singleField = singleField;
}
@Before
public void setUp() {
HttpRequestDecoder decoder = new HttpRequestDecoder( HttpRequestDecoder decoder = new HttpRequestDecoder(
DEFAULT_MAX_INITIAL_LINE_LENGTH, DEFAULT_MAX_INITIAL_LINE_LENGTH,
DEFAULT_MAX_HEADER_SIZE, DEFAULT_MAX_HEADER_SIZE,
DEFAULT_VALIDATE_HEADERS, DEFAULT_VALIDATE_HEADERS,
DEFAULT_INITIAL_BUFFER_SIZE, DEFAULT_INITIAL_BUFFER_SIZE,
allowDuplicateContentLengths); allowDuplicateContentLengths);
channel = new EmbeddedChannel(decoder); return new EmbeddedChannel(decoder);
} }
@Test @ParameterizedTest
public void testMultipleContentLengthHeadersBehavior() { @MethodSource("parameters")
String requestStr = setupRequestString(); public void testMultipleContentLengthHeadersBehavior(boolean allowDuplicateContentLengths,
boolean sameValue, boolean singleField) {
EmbeddedChannel channel = newChannel(allowDuplicateContentLengths);
String requestStr = setupRequestString(sameValue, singleField);
assertThat(channel.writeInbound(Unpooled.copiedBuffer(requestStr, CharsetUtil.US_ASCII)), is(true)); assertThat(channel.writeInbound(Unpooled.copiedBuffer(requestStr, CharsetUtil.US_ASCII)), is(true));
HttpRequest request = channel.readInbound(); HttpRequest request = channel.readInbound();
@ -102,7 +87,7 @@ public class MultipleContentLengthHeadersTest {
assertThat(channel.finish(), is(false)); assertThat(channel.finish(), is(false));
} }
private String setupRequestString() { private static String setupRequestString(boolean sameValue, boolean singleField) {
String firstValue = "1"; String firstValue = "1";
String secondValue = sameValue ? firstValue : "2"; String secondValue = sameValue ? firstValue : "2";
String contentLength; String contentLength;
@ -119,6 +104,7 @@ public class MultipleContentLengthHeadersTest {
@Test @Test
public void testDanglingComma() { public void testDanglingComma() {
EmbeddedChannel channel = newChannel(false);
String requestStr = "GET /some/path HTTP/1.1\r\n" + String requestStr = "GET /some/path HTTP/1.1\r\n" +
"Content-Length: 1,\r\n" + "Content-Length: 1,\r\n" +
"Connection: close\n\n" + "Connection: close\n\n" +

View File

@ -15,16 +15,17 @@
*/ */
package io.netty.handler.codec.rtsp; package io.netty.handler.codec.rtsp;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.http.FullHttpRequest; import io.netty.handler.codec.http.FullHttpRequest;
import io.netty.handler.codec.http.FullHttpResponse; import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpObject; import io.netty.handler.codec.http.HttpObject;
import io.netty.handler.codec.http.HttpObjectAggregator; import io.netty.handler.codec.http.HttpObjectAggregator;
import org.junit.jupiter.api.Test;
import org.junit.Test;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
/** /**
* Test cases for RTSP decoder. * Test cases for RTSP decoder.

View File

@ -15,7 +15,6 @@
*/ */
package io.netty.handler.codec.rtsp; package io.netty.handler.codec.rtsp;
import static org.junit.Assert.assertEquals;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.handler.codec.http.DefaultFullHttpRequest; import io.netty.handler.codec.http.DefaultFullHttpRequest;
@ -27,8 +26,9 @@ import io.netty.handler.codec.http.FullHttpResponse;
import io.netty.handler.codec.http.HttpRequest; import io.netty.handler.codec.http.HttpRequest;
import io.netty.handler.codec.http.HttpResponse; import io.netty.handler.codec.http.HttpResponse;
import io.netty.util.CharsetUtil; import io.netty.util.CharsetUtil;
import org.junit.jupiter.api.Test;
import org.junit.Test; import static org.junit.jupiter.api.Assertions.assertEquals;
/** /**
* Test cases for RTSP encoder. * Test cases for RTSP encoder.