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:
parent
c82c17782c
commit
5179e53294
@ -15,9 +15,9 @@
|
||||
*/
|
||||
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
|
||||
|
@ -26,7 +26,8 @@ import io.netty.handler.codec.DecoderResult;
|
||||
import io.netty.handler.codec.EncoderException;
|
||||
import io.netty.handler.codec.MessageToByteEncoder;
|
||||
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;
|
||||
|
||||
@ -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.assertNull;
|
||||
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.fail;
|
||||
|
||||
public class HttpContentEncoderTest {
|
||||
|
||||
@ -413,7 +414,7 @@ public class HttpContentEncoderTest {
|
||||
};
|
||||
|
||||
final AtomicBoolean channelInactiveCalled = new AtomicBoolean();
|
||||
EmbeddedChannel channel = new EmbeddedChannel(encoder, new ChannelInboundHandlerAdapter() {
|
||||
final EmbeddedChannel channel = new EmbeddedChannel(encoder, new ChannelInboundHandlerAdapter() {
|
||||
@Override
|
||||
public void channelInactive(ChannelHandlerContext ctx) throws Exception {
|
||||
assertTrue(channelInactiveCalled.compareAndSet(false, true));
|
||||
@ -425,12 +426,13 @@ public class HttpContentEncoderTest {
|
||||
HttpContent content = new DefaultHttpContent(Unpooled.buffer().writeZero(10));
|
||||
assertTrue(channel.writeOutbound(content));
|
||||
assertEquals(1, content.refCnt());
|
||||
try {
|
||||
channel.finishAndReleaseAll();
|
||||
fail();
|
||||
} catch (CodecException expected) {
|
||||
// expected
|
||||
}
|
||||
assertThrows(CodecException.class, new Executable() {
|
||||
@Override
|
||||
public void execute() {
|
||||
channel.finishAndReleaseAll();
|
||||
}
|
||||
});
|
||||
|
||||
assertTrue(channelInactiveCalled.get());
|
||||
assertEquals(0, content.refCnt());
|
||||
}
|
||||
|
@ -15,12 +15,13 @@
|
||||
*/
|
||||
package io.netty.handler.codec.http;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.text.ParseException;
|
||||
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 {
|
||||
/**
|
||||
|
@ -20,7 +20,7 @@ import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.embedded.EmbeddedChannel;
|
||||
import io.netty.handler.codec.DecoderResult;
|
||||
import io.netty.util.CharsetUtil;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Random;
|
||||
|
||||
|
@ -27,7 +27,8 @@ import io.netty.util.AsciiString;
|
||||
import io.netty.util.CharsetUtil;
|
||||
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 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.assertNull;
|
||||
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.fail;
|
||||
|
||||
public class HttpObjectAggregatorTest {
|
||||
|
||||
@ -119,11 +120,11 @@ public class HttpObjectAggregatorTest {
|
||||
|
||||
@Test
|
||||
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");
|
||||
HttpContent chunk1 = new DefaultHttpContent(Unpooled.copiedBuffer("test", 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(chunk1));
|
||||
@ -134,12 +135,12 @@ public class HttpObjectAggregatorTest {
|
||||
assertEquals("0", response.headers().get(HttpHeaderNames.CONTENT_LENGTH));
|
||||
assertFalse(embedder.isOpen());
|
||||
|
||||
try {
|
||||
assertFalse(embedder.writeInbound(chunk3));
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertTrue(e instanceof ClosedChannelException);
|
||||
}
|
||||
assertThrows(ClosedChannelException.class, new Executable() {
|
||||
@Override
|
||||
public void execute() {
|
||||
embedder.writeInbound(chunk3);
|
||||
}
|
||||
});
|
||||
|
||||
assertFalse(embedder.finish());
|
||||
}
|
||||
@ -211,7 +212,7 @@ public class HttpObjectAggregatorTest {
|
||||
}
|
||||
|
||||
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));
|
||||
HttpResponse response = embedder.readOutbound();
|
||||
@ -224,13 +225,13 @@ public class HttpObjectAggregatorTest {
|
||||
if (serverShouldCloseConnection(message, response)) {
|
||||
assertFalse(embedder.isOpen());
|
||||
|
||||
try {
|
||||
embedder.writeInbound(new DefaultHttpContent(Unpooled.EMPTY_BUFFER));
|
||||
fail();
|
||||
} catch (Exception e) {
|
||||
assertThat(e, instanceOf(ClosedChannelException.class));
|
||||
// expected
|
||||
}
|
||||
assertThrows(ClosedChannelException.class, new Executable() {
|
||||
@Override
|
||||
public void execute() {
|
||||
embedder.writeInbound(new DefaultHttpContent(Unpooled.EMPTY_BUFFER));
|
||||
}
|
||||
});
|
||||
|
||||
assertFalse(embedder.finish());
|
||||
} else {
|
||||
assertTrue(embedder.isOpen());
|
||||
@ -281,43 +282,58 @@ public class HttpObjectAggregatorTest {
|
||||
|
||||
@Test
|
||||
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);
|
||||
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(chunk1));
|
||||
|
||||
try {
|
||||
embedder.writeInbound(chunk2);
|
||||
fail();
|
||||
} catch (TooLongFrameException expected) {
|
||||
// Expected
|
||||
}
|
||||
assertThrows(TooLongFrameException.class, new Executable() {
|
||||
@Override
|
||||
public void execute() {
|
||||
embedder.writeInbound(chunk2);
|
||||
}
|
||||
});
|
||||
|
||||
assertFalse(embedder.isOpen());
|
||||
assertFalse(embedder.finish());
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@Test
|
||||
public void testInvalidConstructorUsage() {
|
||||
new HttpObjectAggregator(-1);
|
||||
assertThrows(IllegalArgumentException.class, new Executable() {
|
||||
@Override
|
||||
public void execute() {
|
||||
new HttpObjectAggregator(-1);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test(expected = IllegalArgumentException.class)
|
||||
@Test
|
||||
public void testInvalidMaxCumulationBufferComponents() {
|
||||
HttpObjectAggregator aggr = new HttpObjectAggregator(Integer.MAX_VALUE);
|
||||
aggr.setMaxCumulationBufferComponents(1);
|
||||
final HttpObjectAggregator aggr = new HttpObjectAggregator(Integer.MAX_VALUE);
|
||||
assertThrows(IllegalArgumentException.class, new Executable() {
|
||||
@Override
|
||||
public void execute() {
|
||||
aggr.setMaxCumulationBufferComponents(1);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test(expected = IllegalStateException.class)
|
||||
@Test
|
||||
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);
|
||||
aggr.handlerAdded(ctx);
|
||||
Mockito.verifyNoMoreInteractions(ctx);
|
||||
aggr.setMaxCumulationBufferComponents(10);
|
||||
assertThrows(IllegalStateException.class, new Executable() {
|
||||
@Override
|
||||
public void execute() {
|
||||
aggr.setMaxCumulationBufferComponents(10);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
@Test
|
||||
|
@ -20,8 +20,7 @@ import io.netty.channel.embedded.EmbeddedChannel;
|
||||
import io.netty.handler.codec.TooLongFrameException;
|
||||
import io.netty.util.AsciiString;
|
||||
import io.netty.util.CharsetUtil;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
|
@ -21,7 +21,8 @@ import io.netty.channel.embedded.EmbeddedChannel;
|
||||
import io.netty.handler.codec.DecoderResult;
|
||||
import io.netty.util.CharsetUtil;
|
||||
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.ByteOrder;
|
||||
@ -33,8 +34,8 @@ import static org.hamcrest.Matchers.instanceOf;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
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.fail;
|
||||
|
||||
/**
|
||||
*/
|
||||
@ -137,15 +138,17 @@ public class HttpRequestEncoderTest {
|
||||
@Test
|
||||
public void testEmptyReleasedBufferShouldNotWriteEmptyBufferToChannel() throws Exception {
|
||||
HttpRequestEncoder encoder = new HttpRequestEncoder();
|
||||
EmbeddedChannel channel = new EmbeddedChannel(encoder);
|
||||
ByteBuf buf = Unpooled.buffer();
|
||||
final EmbeddedChannel channel = new EmbeddedChannel(encoder);
|
||||
final ByteBuf buf = Unpooled.buffer();
|
||||
buf.release();
|
||||
try {
|
||||
channel.writeAndFlush(buf).get();
|
||||
fail();
|
||||
} catch (ExecutionException e) {
|
||||
assertThat(e.getCause().getCause(), is(instanceOf(IllegalReferenceCountException.class)));
|
||||
}
|
||||
ExecutionException e = assertThrows(ExecutionException.class, new Executable() {
|
||||
@Override
|
||||
public void execute() throws Throwable {
|
||||
channel.writeAndFlush(buf).get();
|
||||
}
|
||||
});
|
||||
assertThat(e.getCause().getCause(), is(instanceOf(IllegalReferenceCountException.class)));
|
||||
|
||||
channel.finishAndReleaseAll();
|
||||
}
|
||||
|
||||
|
@ -17,7 +17,7 @@ package io.netty.handler.codec.http;
|
||||
|
||||
import io.netty.channel.embedded.EmbeddedChannel;
|
||||
import io.netty.util.ReferenceCountUtil;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.hamcrest.MatcherAssert.assertThat;
|
||||
|
@ -16,13 +16,11 @@
|
||||
package io.netty.handler.codec.http;
|
||||
|
||||
import io.netty.channel.embedded.EmbeddedChannel;
|
||||
import io.netty.util.AsciiString;
|
||||
import io.netty.util.ReferenceCountUtil;
|
||||
import io.netty.util.internal.StringUtil;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import java.util.Arrays;
|
||||
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.assertTrue;
|
||||
|
||||
@RunWith(Parameterized.class)
|
||||
public class HttpServerKeepAliveHandlerTest {
|
||||
private static final String REQUEST_KEEP_ALIVE = "REQUEST_KEEP_ALIVE";
|
||||
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_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;
|
||||
|
||||
@Parameters
|
||||
public static Collection<Object[]> keepAliveProvider() {
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
channel = new EmbeddedChannel(new HttpServerKeepAliveHandler());
|
||||
}
|
||||
|
||||
static Collection<Object[]> keepAliveProvider() {
|
||||
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_MULTIPART, KEEP_ALIVE }, // 1
|
||||
@ -78,31 +73,19 @@ public class HttpServerKeepAliveHandlerTest {
|
||||
});
|
||||
}
|
||||
|
||||
public HttpServerKeepAliveHandlerTest(boolean isKeepAliveResponseExpected, HttpVersion httpVersion,
|
||||
HttpResponseStatus responseStatus, String sendKeepAlive,
|
||||
int setSelfDefinedMessageLength, CharSequence setResponseConnection) {
|
||||
this.isKeepAliveResponseExpected = isKeepAliveResponseExpected;
|
||||
this.httpVersion = httpVersion;
|
||||
this.responseStatus = responseStatus;
|
||||
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 {
|
||||
@ParameterizedTest
|
||||
@MethodSource("keepAliveProvider")
|
||||
public void test_KeepAlive(boolean isKeepAliveResponseExpected, HttpVersion httpVersion,
|
||||
HttpResponseStatus responseStatus,
|
||||
String sendKeepAlive, int setSelfDefinedMessageLength,
|
||||
AsciiString setResponseConnection) throws Exception {
|
||||
FullHttpRequest request = new DefaultFullHttpRequest(httpVersion, HttpMethod.GET, "/v1/foo/bar");
|
||||
setKeepAlive(request, REQUEST_KEEP_ALIVE.equals(sendKeepAlive));
|
||||
HttpResponse response = new DefaultFullHttpResponse(httpVersion, responseStatus);
|
||||
if (!StringUtil.isNullOrEmpty(setResponseConnection)) {
|
||||
if (setResponseConnection != null) {
|
||||
response.headers().set(HttpHeaderNames.CONNECTION, setResponseConnection);
|
||||
}
|
||||
setupMessageLength(response);
|
||||
setupMessageLength(response, setSelfDefinedMessageLength);
|
||||
|
||||
assertTrue(channel.writeInbound(request));
|
||||
Object requestForwarded = channel.readInbound();
|
||||
@ -117,11 +100,27 @@ public class HttpServerKeepAliveHandlerTest {
|
||||
assertFalse(channel.finishAndReleaseAll());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConnectionCloseHeaderHandledCorrectly() throws Exception {
|
||||
static Collection<Object[]> connectionCloseProvider() {
|
||||
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);
|
||||
response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
|
||||
setupMessageLength(response);
|
||||
setupMessageLength(response, setSelfDefinedMessageLength);
|
||||
|
||||
channel.writeAndFlush(response);
|
||||
HttpResponse writtenResponse = channel.readOutbound();
|
||||
@ -131,11 +130,13 @@ public class HttpServerKeepAliveHandlerTest {
|
||||
assertFalse(channel.finishAndReleaseAll());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testConnectionCloseHeaderHandledCorrectlyForVoidPromise() throws Exception {
|
||||
@ParameterizedTest
|
||||
@MethodSource("connectionCloseProvider")
|
||||
public void testConnectionCloseHeaderHandledCorrectlyForVoidPromise(
|
||||
HttpVersion httpVersion, HttpResponseStatus responseStatus, int setSelfDefinedMessageLength) {
|
||||
HttpResponse response = new DefaultFullHttpResponse(httpVersion, responseStatus);
|
||||
response.headers().set(HttpHeaderNames.CONNECTION, HttpHeaderValues.CLOSE);
|
||||
setupMessageLength(response);
|
||||
setupMessageLength(response, setSelfDefinedMessageLength);
|
||||
|
||||
channel.writeAndFlush(response, channel.voidPromise());
|
||||
HttpResponse writtenResponse = channel.readOutbound();
|
||||
@ -145,8 +146,12 @@ public class HttpServerKeepAliveHandlerTest {
|
||||
assertFalse(channel.finishAndReleaseAll());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void test_PipelineKeepAlive() {
|
||||
@ParameterizedTest
|
||||
@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");
|
||||
setKeepAlive(firstRequest, true);
|
||||
FullHttpRequest secondRequest = new DefaultFullHttpRequest(httpVersion, HttpMethod.GET, "/v1/foo/bar");
|
||||
@ -181,12 +186,12 @@ public class HttpServerKeepAliveHandlerTest {
|
||||
assertTrue(isKeepAlive(writtenInfoResp), "response keep-alive");
|
||||
ReferenceCountUtil.release(writtenInfoResp);
|
||||
|
||||
if (!StringUtil.isNullOrEmpty(setResponseConnection)) {
|
||||
if (setResponseConnection != null) {
|
||||
response.headers().set(HttpHeaderNames.CONNECTION, setResponseConnection);
|
||||
} else {
|
||||
response.headers().remove(HttpHeaderNames.CONNECTION);
|
||||
}
|
||||
setupMessageLength(response);
|
||||
setupMessageLength(response, setSelfDefinedMessageLength);
|
||||
channel.writeAndFlush(response.retainedDuplicate());
|
||||
HttpResponse secondResponse = channel.readOutbound();
|
||||
assertEquals(isKeepAliveResponseExpected, channel.isOpen(), "channel.isOpen");
|
||||
@ -207,7 +212,7 @@ public class HttpServerKeepAliveHandlerTest {
|
||||
assertFalse(channel.finishAndReleaseAll());
|
||||
}
|
||||
|
||||
private void setupMessageLength(HttpResponse response) {
|
||||
private static void setupMessageLength(HttpResponse response, int setSelfDefinedMessageLength) {
|
||||
switch (setSelfDefinedMessageLength) {
|
||||
case NOT_SELF_DEFINED_MSG_LENGTH:
|
||||
if (isContentLengthSet(response)) {
|
||||
|
@ -18,8 +18,6 @@ package io.netty.handler.codec.http;
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
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.UpgradeCodecFactory;
|
||||
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.assertFalse;
|
||||
@ -43,7 +42,7 @@ import static org.junit.jupiter.api.Assertions.fail;
|
||||
|
||||
public class HttpServerUpgradeHandlerTest {
|
||||
|
||||
private class TestUpgradeCodec implements UpgradeCodec {
|
||||
private static class TestUpgradeCodec implements UpgradeCodec {
|
||||
@Override
|
||||
public Collection<CharSequence> requiredUpgradeHeaders() {
|
||||
return Collections.<CharSequence>emptyList();
|
||||
|
@ -17,7 +17,7 @@ package io.netty.handler.codec.http;
|
||||
|
||||
import io.netty.util.CharsetUtil;
|
||||
import io.netty.util.ReferenceCountUtil;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.function.Executable;
|
||||
|
||||
import java.net.InetAddress;
|
||||
|
@ -18,11 +18,9 @@ package io.netty.handler.codec.http;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import io.netty.channel.embedded.EmbeddedChannel;
|
||||
import io.netty.util.CharsetUtil;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
import org.junit.runners.Parameterized.Parameters;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.params.ParameterizedTest;
|
||||
import org.junit.jupiter.params.provider.MethodSource;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
@ -39,17 +37,9 @@ import static org.hamcrest.Matchers.containsString;
|
||||
import static org.hamcrest.Matchers.is;
|
||||
import static org.hamcrest.core.IsInstanceOf.instanceOf;
|
||||
|
||||
@RunWith(Parameterized.class)
|
||||
public class MultipleContentLengthHeadersTest {
|
||||
|
||||
private final boolean allowDuplicateContentLengths;
|
||||
private final boolean sameValue;
|
||||
private final boolean singleField;
|
||||
|
||||
private EmbeddedChannel channel;
|
||||
|
||||
@Parameters
|
||||
public static Collection<Object[]> parameters() {
|
||||
static Collection<Object[]> parameters() {
|
||||
return Arrays.asList(new Object[][] {
|
||||
{ false, false, false },
|
||||
{ false, false, true },
|
||||
@ -62,15 +52,7 @@ public class MultipleContentLengthHeadersTest {
|
||||
});
|
||||
}
|
||||
|
||||
public MultipleContentLengthHeadersTest(
|
||||
boolean allowDuplicateContentLengths, boolean sameValue, boolean singleField) {
|
||||
this.allowDuplicateContentLengths = allowDuplicateContentLengths;
|
||||
this.sameValue = sameValue;
|
||||
this.singleField = singleField;
|
||||
}
|
||||
|
||||
@Before
|
||||
public void setUp() {
|
||||
private static EmbeddedChannel newChannel(boolean allowDuplicateContentLengths) {
|
||||
HttpRequestDecoder decoder = new HttpRequestDecoder(
|
||||
DEFAULT_MAX_INITIAL_LINE_LENGTH,
|
||||
DEFAULT_MAX_HEADER_SIZE,
|
||||
@ -78,12 +60,15 @@ public class MultipleContentLengthHeadersTest {
|
||||
DEFAULT_VALIDATE_HEADERS,
|
||||
DEFAULT_INITIAL_BUFFER_SIZE,
|
||||
allowDuplicateContentLengths);
|
||||
channel = new EmbeddedChannel(decoder);
|
||||
return new EmbeddedChannel(decoder);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testMultipleContentLengthHeadersBehavior() {
|
||||
String requestStr = setupRequestString();
|
||||
@ParameterizedTest
|
||||
@MethodSource("parameters")
|
||||
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));
|
||||
HttpRequest request = channel.readInbound();
|
||||
|
||||
@ -104,7 +89,7 @@ public class MultipleContentLengthHeadersTest {
|
||||
assertThat(channel.finish(), is(false));
|
||||
}
|
||||
|
||||
private String setupRequestString() {
|
||||
private static String setupRequestString(boolean sameValue, boolean singleField) {
|
||||
String firstValue = "1";
|
||||
String secondValue = sameValue ? firstValue : "2";
|
||||
String contentLength;
|
||||
@ -121,6 +106,7 @@ public class MultipleContentLengthHeadersTest {
|
||||
|
||||
@Test
|
||||
public void testDanglingComma() {
|
||||
EmbeddedChannel channel = newChannel(false);
|
||||
String requestStr = "GET /some/path HTTP/1.1\r\n" +
|
||||
"Content-Length: 1,\r\n" +
|
||||
"Connection: close\n\n" +
|
||||
|
@ -15,16 +15,17 @@
|
||||
*/
|
||||
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.channel.embedded.EmbeddedChannel;
|
||||
import io.netty.handler.codec.http.FullHttpRequest;
|
||||
import io.netty.handler.codec.http.FullHttpResponse;
|
||||
import io.netty.handler.codec.http.HttpObject;
|
||||
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.
|
||||
|
@ -15,7 +15,6 @@
|
||||
*/
|
||||
package io.netty.handler.codec.rtsp;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.channel.embedded.EmbeddedChannel;
|
||||
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.HttpResponse;
|
||||
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.
|
||||
|
@ -15,10 +15,10 @@
|
||||
*/
|
||||
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.Assert.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
public class DefaultSpdyHeadersTest {
|
||||
|
||||
|
@ -17,17 +17,23 @@ package io.netty.handler.codec.spdy;
|
||||
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
|
||||
import java.util.ArrayDeque;
|
||||
import java.util.Queue;
|
||||
import java.util.Random;
|
||||
|
||||
import static io.netty.handler.codec.spdy.SpdyCodecUtil.SPDY_HEADER_SIZE;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
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 {
|
||||
|
||||
@ -37,12 +43,12 @@ public class SpdyFrameDecoderTest {
|
||||
private final TestSpdyFrameDecoderDelegate testDelegate = new TestSpdyFrameDecoderDelegate();
|
||||
private SpdyFrameDecoder decoder;
|
||||
|
||||
@Before
|
||||
@BeforeEach
|
||||
public void createDecoder() {
|
||||
decoder = new SpdyFrameDecoder(SpdyVersion.SPDY_3_1, testDelegate);
|
||||
}
|
||||
|
||||
@After
|
||||
@AfterEach
|
||||
public void releaseBuffers() {
|
||||
testDelegate.releaseAll();
|
||||
}
|
||||
|
@ -18,13 +18,13 @@ package io.netty.handler.codec.spdy;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.ByteBufAllocator;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class SpdyHeaderBlockRawDecoderTest {
|
||||
|
||||
@ -38,13 +38,13 @@ public class SpdyHeaderBlockRawDecoderTest {
|
||||
private SpdyHeaderBlockRawDecoder decoder;
|
||||
private SpdyHeadersFrame frame;
|
||||
|
||||
@Before
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
decoder = new SpdyHeaderBlockRawDecoder(SpdyVersion.SPDY_3_1, maxHeaderSize);
|
||||
frame = new DefaultSpdyHeadersFrame(1);
|
||||
}
|
||||
|
||||
@After
|
||||
@AfterEach
|
||||
public void tearDown() {
|
||||
decoder.end();
|
||||
}
|
||||
|
@ -18,13 +18,15 @@ package io.netty.handler.codec.spdy;
|
||||
import io.netty.buffer.ByteBuf;
|
||||
import io.netty.buffer.ByteBufAllocator;
|
||||
import io.netty.buffer.Unpooled;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.AfterEach;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.function.Executable;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class SpdyHeaderBlockZlibDecoderTest {
|
||||
|
||||
@ -42,13 +44,13 @@ public class SpdyHeaderBlockZlibDecoderTest {
|
||||
private SpdyHeaderBlockZlibDecoder decoder;
|
||||
private SpdyHeadersFrame frame;
|
||||
|
||||
@Before
|
||||
@BeforeEach
|
||||
public void setUp() {
|
||||
decoder = new SpdyHeaderBlockZlibDecoder(SpdyVersion.SPDY_3_1, maxHeaderSize);
|
||||
frame = new DefaultSpdyHeadersFrame(1);
|
||||
}
|
||||
|
||||
@After
|
||||
@AfterEach
|
||||
public void tearDown() {
|
||||
decoder.end();
|
||||
}
|
||||
@ -170,9 +172,9 @@ public class SpdyHeaderBlockZlibDecoderTest {
|
||||
headerBlock.release();
|
||||
}
|
||||
|
||||
@Test(expected = SpdyProtocolException.class)
|
||||
@Test
|
||||
public void testHeaderBlockExtraData() throws Exception {
|
||||
ByteBuf headerBlock = Unpooled.buffer(37);
|
||||
final ByteBuf headerBlock = Unpooled.buffer(37);
|
||||
headerBlock.writeBytes(zlibHeader);
|
||||
headerBlock.writeByte(0); // Non-compressed block
|
||||
headerBlock.writeByte(0x15); // little-endian length (21)
|
||||
@ -189,14 +191,20 @@ public class SpdyHeaderBlockZlibDecoderTest {
|
||||
headerBlock.writeByte(0x03); // adler-32 checksum
|
||||
headerBlock.writeByte(0xc9); // adler-32 checksum
|
||||
headerBlock.writeByte(0); // Data following zlib stream
|
||||
decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
|
||||
|
||||
assertThrows(SpdyProtocolException.class, new Executable() {
|
||||
@Override
|
||||
public void execute() throws Throwable {
|
||||
decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
|
||||
}
|
||||
});
|
||||
|
||||
headerBlock.release();
|
||||
}
|
||||
|
||||
@Test(expected = SpdyProtocolException.class)
|
||||
@Test
|
||||
public void testHeaderBlockInvalidDictionary() throws Exception {
|
||||
ByteBuf headerBlock = Unpooled.buffer(7);
|
||||
final ByteBuf headerBlock = Unpooled.buffer(7);
|
||||
headerBlock.writeByte(0x78);
|
||||
headerBlock.writeByte(0x3f);
|
||||
headerBlock.writeByte(0x01); // Unknown dictionary
|
||||
@ -204,21 +212,33 @@ public class SpdyHeaderBlockZlibDecoderTest {
|
||||
headerBlock.writeByte(0x03); // Unknown dictionary
|
||||
headerBlock.writeByte(0x04); // Unknown dictionary
|
||||
headerBlock.writeByte(0); // Non-compressed block
|
||||
decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
|
||||
|
||||
assertThrows(SpdyProtocolException.class, new Executable() {
|
||||
@Override
|
||||
public void execute() throws Throwable {
|
||||
decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
|
||||
}
|
||||
});
|
||||
|
||||
headerBlock.release();
|
||||
}
|
||||
|
||||
@Test(expected = SpdyProtocolException.class)
|
||||
@Test
|
||||
public void testHeaderBlockInvalidDeflateBlock() throws Exception {
|
||||
ByteBuf headerBlock = Unpooled.buffer(11);
|
||||
final ByteBuf headerBlock = Unpooled.buffer(11);
|
||||
headerBlock.writeBytes(zlibHeader);
|
||||
headerBlock.writeByte(0); // Non-compressed block
|
||||
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
|
||||
decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
|
||||
|
||||
assertThrows(SpdyProtocolException.class, new Executable() {
|
||||
@Override
|
||||
public void execute() throws Throwable {
|
||||
decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
|
||||
}
|
||||
});
|
||||
|
||||
headerBlock.release();
|
||||
}
|
||||
|
@ -20,12 +20,15 @@ import io.netty.channel.ChannelInboundHandlerAdapter;
|
||||
import io.netty.channel.embedded.EmbeddedChannel;
|
||||
import io.netty.util.internal.logging.InternalLogger;
|
||||
import io.netty.util.internal.logging.InternalLoggerFactory;
|
||||
import org.junit.Test;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.List;
|
||||
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 {
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user