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 committed by GitHub
parent c82c17782c
commit 5179e53294
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
19 changed files with 222 additions and 181 deletions

View File

@ -15,9 +15,9 @@
*/ */
package io.netty.handler.codec.http; package io.netty.handler.codec.http;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.assertNotNull;
/** /**
* A test to validate that either order of initialization of the {@link EmptyHttpHeaders#INSTANCE} and * A test to validate that either order of initialization of the {@link EmptyHttpHeaders#INSTANCE} and

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 {
@ -413,7 +414,7 @@ public class HttpContentEncoderTest {
}; };
final AtomicBoolean channelInactiveCalled = new AtomicBoolean(); final AtomicBoolean channelInactiveCalled = new AtomicBoolean();
EmbeddedChannel channel = new EmbeddedChannel(encoder, new ChannelInboundHandlerAdapter() { final EmbeddedChannel channel = new EmbeddedChannel(encoder, new ChannelInboundHandlerAdapter() {
@Override @Override
public void channelInactive(ChannelHandlerContext ctx) throws Exception { public void channelInactive(ChannelHandlerContext ctx) throws Exception {
assertTrue(channelInactiveCalled.compareAndSet(false, true)); assertTrue(channelInactiveCalled.compareAndSet(false, true));
@ -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

@ -15,12 +15,13 @@
*/ */
package io.netty.handler.codec.http; package io.netty.handler.codec.http;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.text.ParseException; import java.text.ParseException;
import java.util.Date; import java.util.Date;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
public class HttpHeaderDateFormatTest { public class HttpHeaderDateFormatTest {
/** /**

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,11 +130,13 @@ public class HttpServerKeepAliveHandlerTest {
assertFalse(channel.finishAndReleaseAll()); assertFalse(channel.finishAndReleaseAll());
} }
@Test @ParameterizedTest
public void testConnectionCloseHeaderHandledCorrectlyForVoidPromise() throws Exception { @MethodSource("connectionCloseProvider")
public void testConnectionCloseHeaderHandledCorrectlyForVoidPromise(
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.voidPromise()); channel.writeAndFlush(response, channel.voidPromise());
HttpResponse writtenResponse = channel.readOutbound(); HttpResponse writtenResponse = channel.readOutbound();
@ -145,8 +146,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");
@ -181,12 +186,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");
@ -207,7 +212,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.ChannelDuplexHandler; import io.netty.channel.ChannelDuplexHandler;
@ -33,6 +31,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;
@ -43,7 +42,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.<CharSequence>emptyList(); return Collections.<CharSequence>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;
@ -39,17 +37,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 },
@ -62,15 +52,7 @@ 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,
@ -78,12 +60,15 @@ public class MultipleContentLengthHeadersTest {
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();
@ -104,7 +89,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;
@ -121,6 +106,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.

View File

@ -15,10 +15,10 @@
*/ */
package io.netty.handler.codec.spdy; package io.netty.handler.codec.spdy;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
public class DefaultSpdyHeadersTest { public class DefaultSpdyHeadersTest {

View File

@ -17,17 +17,23 @@ package io.netty.handler.codec.spdy;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.util.ArrayDeque; import java.util.ArrayDeque;
import java.util.Queue; import java.util.Queue;
import java.util.Random; import java.util.Random;
import static io.netty.handler.codec.spdy.SpdyCodecUtil.SPDY_HEADER_SIZE; import static io.netty.handler.codec.spdy.SpdyCodecUtil.SPDY_HEADER_SIZE;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.mockito.Mockito.*; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
public class SpdyFrameDecoderTest { public class SpdyFrameDecoderTest {
@ -37,12 +43,12 @@ public class SpdyFrameDecoderTest {
private final TestSpdyFrameDecoderDelegate testDelegate = new TestSpdyFrameDecoderDelegate(); private final TestSpdyFrameDecoderDelegate testDelegate = new TestSpdyFrameDecoderDelegate();
private SpdyFrameDecoder decoder; private SpdyFrameDecoder decoder;
@Before @BeforeEach
public void createDecoder() { public void createDecoder() {
decoder = new SpdyFrameDecoder(SpdyVersion.SPDY_3_1, testDelegate); decoder = new SpdyFrameDecoder(SpdyVersion.SPDY_3_1, testDelegate);
} }
@After @AfterEach
public void releaseBuffers() { public void releaseBuffers() {
testDelegate.releaseAll(); testDelegate.releaseAll();
} }

View File

@ -18,13 +18,13 @@ package io.netty.handler.codec.spdy;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator; import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
public class SpdyHeaderBlockRawDecoderTest { public class SpdyHeaderBlockRawDecoderTest {
@ -38,13 +38,13 @@ public class SpdyHeaderBlockRawDecoderTest {
private SpdyHeaderBlockRawDecoder decoder; private SpdyHeaderBlockRawDecoder decoder;
private SpdyHeadersFrame frame; private SpdyHeadersFrame frame;
@Before @BeforeEach
public void setUp() { public void setUp() {
decoder = new SpdyHeaderBlockRawDecoder(SpdyVersion.SPDY_3_1, maxHeaderSize); decoder = new SpdyHeaderBlockRawDecoder(SpdyVersion.SPDY_3_1, maxHeaderSize);
frame = new DefaultSpdyHeadersFrame(1); frame = new DefaultSpdyHeadersFrame(1);
} }
@After @AfterEach
public void tearDown() { public void tearDown() {
decoder.end(); decoder.end();
} }

View File

@ -18,13 +18,15 @@ package io.netty.handler.codec.spdy;
import io.netty.buffer.ByteBuf; import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator; import io.netty.buffer.ByteBufAllocator;
import io.netty.buffer.Unpooled; import io.netty.buffer.Unpooled;
import org.junit.After; import org.junit.jupiter.api.AfterEach;
import org.junit.Before; import org.junit.jupiter.api.BeforeEach;
import org.junit.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.function.Executable;
import static org.junit.Assert.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class SpdyHeaderBlockZlibDecoderTest { public class SpdyHeaderBlockZlibDecoderTest {
@ -42,13 +44,13 @@ public class SpdyHeaderBlockZlibDecoderTest {
private SpdyHeaderBlockZlibDecoder decoder; private SpdyHeaderBlockZlibDecoder decoder;
private SpdyHeadersFrame frame; private SpdyHeadersFrame frame;
@Before @BeforeEach
public void setUp() { public void setUp() {
decoder = new SpdyHeaderBlockZlibDecoder(SpdyVersion.SPDY_3_1, maxHeaderSize); decoder = new SpdyHeaderBlockZlibDecoder(SpdyVersion.SPDY_3_1, maxHeaderSize);
frame = new DefaultSpdyHeadersFrame(1); frame = new DefaultSpdyHeadersFrame(1);
} }
@After @AfterEach
public void tearDown() { public void tearDown() {
decoder.end(); decoder.end();
} }
@ -170,9 +172,9 @@ public class SpdyHeaderBlockZlibDecoderTest {
headerBlock.release(); headerBlock.release();
} }
@Test(expected = SpdyProtocolException.class) @Test
public void testHeaderBlockExtraData() throws Exception { public void testHeaderBlockExtraData() throws Exception {
ByteBuf headerBlock = Unpooled.buffer(37); final ByteBuf headerBlock = Unpooled.buffer(37);
headerBlock.writeBytes(zlibHeader); headerBlock.writeBytes(zlibHeader);
headerBlock.writeByte(0); // Non-compressed block headerBlock.writeByte(0); // Non-compressed block
headerBlock.writeByte(0x15); // little-endian length (21) headerBlock.writeByte(0x15); // little-endian length (21)
@ -189,14 +191,20 @@ public class SpdyHeaderBlockZlibDecoderTest {
headerBlock.writeByte(0x03); // adler-32 checksum headerBlock.writeByte(0x03); // adler-32 checksum
headerBlock.writeByte(0xc9); // adler-32 checksum headerBlock.writeByte(0xc9); // adler-32 checksum
headerBlock.writeByte(0); // Data following zlib stream headerBlock.writeByte(0); // Data following zlib stream
assertThrows(SpdyProtocolException.class, new Executable() {
@Override
public void execute() throws Throwable {
decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame); decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
}
});
headerBlock.release(); headerBlock.release();
} }
@Test(expected = SpdyProtocolException.class) @Test
public void testHeaderBlockInvalidDictionary() throws Exception { public void testHeaderBlockInvalidDictionary() throws Exception {
ByteBuf headerBlock = Unpooled.buffer(7); final ByteBuf headerBlock = Unpooled.buffer(7);
headerBlock.writeByte(0x78); headerBlock.writeByte(0x78);
headerBlock.writeByte(0x3f); headerBlock.writeByte(0x3f);
headerBlock.writeByte(0x01); // Unknown dictionary headerBlock.writeByte(0x01); // Unknown dictionary
@ -204,21 +212,33 @@ public class SpdyHeaderBlockZlibDecoderTest {
headerBlock.writeByte(0x03); // Unknown dictionary headerBlock.writeByte(0x03); // Unknown dictionary
headerBlock.writeByte(0x04); // Unknown dictionary headerBlock.writeByte(0x04); // Unknown dictionary
headerBlock.writeByte(0); // Non-compressed block headerBlock.writeByte(0); // Non-compressed block
assertThrows(SpdyProtocolException.class, new Executable() {
@Override
public void execute() throws Throwable {
decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame); decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
}
});
headerBlock.release(); headerBlock.release();
} }
@Test(expected = SpdyProtocolException.class) @Test
public void testHeaderBlockInvalidDeflateBlock() throws Exception { public void testHeaderBlockInvalidDeflateBlock() throws Exception {
ByteBuf headerBlock = Unpooled.buffer(11); final ByteBuf headerBlock = Unpooled.buffer(11);
headerBlock.writeBytes(zlibHeader); headerBlock.writeBytes(zlibHeader);
headerBlock.writeByte(0); // Non-compressed block headerBlock.writeByte(0); // Non-compressed block
headerBlock.writeByte(0x00); // little-endian length (0) headerBlock.writeByte(0x00); // little-endian length (0)
headerBlock.writeByte(0x00); // little-endian length (0) headerBlock.writeByte(0x00); // little-endian length (0)
headerBlock.writeByte(0x00); // invalid one's compliment headerBlock.writeByte(0x00); // invalid one's compliment
headerBlock.writeByte(0x00); // invalid one's compliment headerBlock.writeByte(0x00); // invalid one's compliment
assertThrows(SpdyProtocolException.class, new Executable() {
@Override
public void execute() throws Throwable {
decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame); decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
}
});
headerBlock.release(); headerBlock.release();
} }

View File

@ -20,12 +20,15 @@ import io.netty.channel.ChannelInboundHandlerAdapter;
import io.netty.channel.embedded.EmbeddedChannel; import io.netty.channel.embedded.EmbeddedChannel;
import io.netty.util.internal.logging.InternalLogger; import io.netty.util.internal.logging.InternalLogger;
import io.netty.util.internal.logging.InternalLoggerFactory; import io.netty.util.internal.logging.InternalLoggerFactory;
import org.junit.Test; import org.junit.jupiter.api.Test;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import static org.junit.Assert.*; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class SpdySessionHandlerTest { public class SpdySessionHandlerTest {