本文整理匯總了Java中io.netty.channel.embedded.EmbeddedChannel.finish方法的典型用法代碼示例。如果您正苦於以下問題:Java EmbeddedChannel.finish方法的具體用法?Java EmbeddedChannel.finish怎麽用?Java EmbeddedChannel.finish使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類io.netty.channel.embedded.EmbeddedChannel
的用法示例。
在下文中一共展示了EmbeddedChannel.finish方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Java代碼示例。
示例1: testPrematureClosureWithChunkedEncoding1
import io.netty.channel.embedded.EmbeddedChannel; //導入方法依賴的package包/類
@Test
public void testPrematureClosureWithChunkedEncoding1() throws Exception {
EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder());
ch.writeInbound(
Unpooled.copiedBuffer("HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n", CharsetUtil.US_ASCII));
// Read the response headers.
HttpResponse res = (HttpResponse) ch.readInbound();
assertThat(res.getProtocolVersion(), sameInstance(HttpVersion.HTTP_1_1));
assertThat(res.getStatus(), is(HttpResponseStatus.OK));
assertThat(res.headers().get(Names.TRANSFER_ENCODING), is("chunked"));
assertThat(ch.readInbound(), is(nullValue()));
// Close the connection without sending anything.
ch.finish();
// The decoder should not generate the last chunk because it's closed prematurely.
assertThat(ch.readInbound(), is(nullValue()));
}
示例2: testFailsOnIncompleteChunkedResponse
import io.netty.channel.embedded.EmbeddedChannel; //導入方法依賴的package包/類
@Test
public void testFailsOnIncompleteChunkedResponse() {
HttpClientCodec codec = new HttpClientCodec(4096, 8192, 8192, true);
EmbeddedChannel ch = new EmbeddedChannel(codec);
ch.writeOutbound(releaseLater(
new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, "http://localhost/")));
assertNotNull(releaseLater(ch.readOutbound()));
assertNull(ch.readInbound());
ch.writeInbound(releaseLater(
Unpooled.copiedBuffer(INCOMPLETE_CHUNKED_RESPONSE, CharsetUtil.ISO_8859_1)));
assertThat(releaseLater(ch.readInbound()), instanceOf(HttpResponse.class));
assertThat(releaseLater(ch.readInbound()), instanceOf(HttpContent.class)); // Chunk 'first'
assertThat(releaseLater(ch.readInbound()), instanceOf(HttpContent.class)); // Chunk 'second'
assertNull(ch.readInbound());
try {
ch.finish();
fail();
} catch (CodecException e) {
assertTrue(e instanceof PrematureChannelClosureException);
}
}
示例3: testNonByteBufPassthrough
import io.netty.channel.embedded.EmbeddedChannel; //導入方法依賴的package包/類
@Test
public void testNonByteBufPassthrough() throws Exception {
SSLEngine engine = SSLContext.getDefault().createSSLEngine();
engine.setUseClientMode(false);
EmbeddedChannel ch = new EmbeddedChannel(new SslHandler(engine));
Object msg1 = new Object();
ch.writeOutbound(msg1);
assertThat(ch.readOutbound(), is(sameInstance(msg1)));
Object msg2 = new Object();
ch.writeInbound(msg2);
assertThat(ch.readInbound(), is(sameInstance(msg2)));
ch.finish();
}
示例4: testLineProtocol
import io.netty.channel.embedded.EmbeddedChannel; //導入方法依賴的package包/類
@Test
public void testLineProtocol() {
EmbeddedChannel ch = new EmbeddedChannel(new LineDecoder());
// Ordinary input
ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 'A' }));
assertNull(ch.readInbound());
ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 'B' }));
assertNull(ch.readInbound());
ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 'C' }));
assertNull(ch.readInbound());
ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { '\n' }));
assertEquals(Unpooled.wrappedBuffer(new byte[] { 'A', 'B', 'C' }), ch.readInbound());
// Truncated input
ch.writeInbound(Unpooled.wrappedBuffer(new byte[] { 'A' }));
assertNull(ch.readInbound());
ch.finish();
assertNull(ch.readInbound());
}
示例5: testReplacement
import io.netty.channel.embedded.EmbeddedChannel; //導入方法依賴的package包/類
@Test
public void testReplacement() throws Exception {
EmbeddedChannel ch = new EmbeddedChannel(new BloatedLineDecoder());
// "AB" should be forwarded to LineDecoder by BloatedLineDecoder.
ch.writeInbound(Unpooled.wrappedBuffer(new byte[]{'A', 'B'}));
assertNull(ch.readInbound());
// "C\n" should be appended to "AB" so that LineDecoder decodes it correctly.
ch.writeInbound(Unpooled.wrappedBuffer(new byte[]{'C', '\n'}));
assertEquals(releaseLater(Unpooled.wrappedBuffer(new byte[] { 'A', 'B', 'C' })),
releaseLater(ch.readInbound()));
ch.finish();
assertNull(ch.readInbound());
}
示例6: testSingleDecode
import io.netty.channel.embedded.EmbeddedChannel; //導入方法依賴的package包/類
@Test
public void testSingleDecode() throws Exception {
LineDecoder decoder = new LineDecoder();
decoder.setSingleDecode(true);
EmbeddedChannel ch = new EmbeddedChannel(decoder);
// "C\n" should be appended to "AB" so that LineDecoder decodes it correctly.
ch.writeInbound(Unpooled.wrappedBuffer(new byte[]{'C', '\n' , 'B', '\n'}));
assertEquals(releaseLater(Unpooled.wrappedBuffer(new byte[] {'C' })), releaseLater(ch.readInbound()));
assertNull("Must be null as it must only decode one frame", ch.readInbound());
ch.read();
ch.finish();
assertEquals(releaseLater(Unpooled.wrappedBuffer(new byte[] {'B' })), releaseLater(ch.readInbound()));
assertNull(ch.readInbound());
}
示例7: testUnfinishedChunkedHttpRequestIsLastFlag
import io.netty.channel.embedded.EmbeddedChannel; //導入方法依賴的package包/類
/**
* Testcase for https://github.com/netty/netty/issues/433
*/
@Test
public void testUnfinishedChunkedHttpRequestIsLastFlag() throws Exception {
int maxChunkSize = 2000;
HttpServerCodec httpServerCodec = new HttpServerCodec(1000, 1000, maxChunkSize);
EmbeddedChannel decoderEmbedder = new EmbeddedChannel(httpServerCodec);
int totalContentLength = maxChunkSize * 5;
decoderEmbedder.writeInbound(Unpooled.copiedBuffer(
"PUT /test HTTP/1.1\r\n" +
"Content-Length: " + totalContentLength + "\r\n" +
"\r\n", CharsetUtil.UTF_8));
int offeredContentLength = (int) (maxChunkSize * 2.5);
decoderEmbedder.writeInbound(prepareDataChunk(offeredContentLength));
decoderEmbedder.finish();
HttpMessage httpMessage = (HttpMessage) decoderEmbedder.readInbound();
assertNotNull(httpMessage);
boolean empty = true;
int totalBytesPolled = 0;
for (;;) {
HttpContent httpChunk = (HttpContent) decoderEmbedder.readInbound();
if (httpChunk == null) {
break;
}
empty = false;
totalBytesPolled += httpChunk.content().readableBytes();
assertFalse(httpChunk instanceof LastHttpContent);
httpChunk.release();
}
assertFalse(empty);
assertEquals(offeredContentLength, totalBytesPolled);
}
示例8: testBadRequest
import io.netty.channel.embedded.EmbeddedChannel; //導入方法依賴的package包/類
@Test
public void testBadRequest() {
EmbeddedChannel ch = new EmbeddedChannel(new HttpRequestDecoder(), new HttpObjectAggregator(1024 * 1024));
ch.writeInbound(Unpooled.copiedBuffer("GET / HTTP/1.0 with extra\r\n", CharsetUtil.UTF_8));
Object inbound = ch.readInbound();
assertThat(inbound, is(instanceOf(FullHttpRequest.class)));
assertTrue(((HttpObject) inbound).getDecoderResult().isFailure());
assertNull(ch.readInbound());
ch.finish();
}
示例9: testConnect
import io.netty.channel.embedded.EmbeddedChannel; //導入方法依賴的package包/類
@Test
public void testConnect() {
StringBuilder builder = new StringBuilder();
try {
builder.append("환영합니다. ")
.append(InetAddress.getLocalHost().getHostName())
.append("에 접속하셨습니다!\r\n")
.append("현재 시간은 ")
.append(new Date().toString()).append(" 입니다.\r\n");
}
catch (UnknownHostException e) {
fail();
e.printStackTrace();
}
EmbeddedChannel embeddedChannel =
new EmbeddedChannel(new TelnetServerHandlerV3());
String expected = (String) embeddedChannel.readOutbound();
assertNotNull(expected);
assertEquals(builder.toString(), (String) expected);
String request = "hello";
expected = "입력하신 명령이 '" + request + "' 입니까?\r\n";
embeddedChannel.writeInbound(request);
String response = (String) embeddedChannel.readOutbound();
assertEquals(expected, response);
embeddedChannel.finish();
}
示例10: testPrematureClosureWithChunkedEncoding2
import io.netty.channel.embedded.EmbeddedChannel; //導入方法依賴的package包/類
@Test
public void testPrematureClosureWithChunkedEncoding2() throws Exception {
EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder());
// Write the partial response.
ch.writeInbound(Unpooled.copiedBuffer(
"HTTP/1.1 200 OK\r\nTransfer-Encoding: chunked\r\n\r\n8\r\n12345678", CharsetUtil.US_ASCII));
// Read the response headers.
HttpResponse res = (HttpResponse) ch.readInbound();
assertThat(res.getProtocolVersion(), sameInstance(HttpVersion.HTTP_1_1));
assertThat(res.getStatus(), is(HttpResponseStatus.OK));
assertThat(res.headers().get(Names.TRANSFER_ENCODING), is("chunked"));
// Read the partial content.
HttpContent content = (HttpContent) ch.readInbound();
assertThat(content.content().toString(CharsetUtil.US_ASCII), is("12345678"));
assertThat(content, is(not(instanceOf(LastHttpContent.class))));
content.release();
assertThat(ch.readInbound(), is(nullValue()));
// Close the connection.
ch.finish();
// The decoder should not generate the last chunk because it's closed prematurely.
assertThat(ch.readInbound(), is(nullValue()));
}
示例11: testGarbageHeaders
import io.netty.channel.embedded.EmbeddedChannel; //導入方法依賴的package包/類
@Test
public void testGarbageHeaders() {
// A response without headers - from https://github.com/netty/netty/issues/2103
byte[] data = ("<html>\r\n" +
"<head><title>400 Bad Request</title></head>\r\n" +
"<body bgcolor=\"white\">\r\n" +
"<center><h1>400 Bad Request</h1></center>\r\n" +
"<hr><center>nginx/1.1.19</center>\r\n" +
"</body>\r\n" +
"</html>\r\n").getBytes();
EmbeddedChannel ch = new EmbeddedChannel(new HttpResponseDecoder());
ch.writeInbound(Unpooled.wrappedBuffer(data));
// Garbage input should generate the 999 Unknown response.
HttpResponse res = (HttpResponse) ch.readInbound();
assertThat(res.getProtocolVersion(), sameInstance(HttpVersion.HTTP_1_0));
assertThat(res.getStatus().code(), is(999));
assertThat(res.getDecoderResult().isFailure(), is(true));
assertThat(res.getDecoderResult().isFinished(), is(true));
assertThat(ch.readInbound(), is(nullValue()));
// More garbage should not generate anything (i.e. the decoder discards anything beyond this point.)
ch.writeInbound(Unpooled.wrappedBuffer(data));
assertThat(ch.readInbound(), is(nullValue()));
// Closing the connection should not generate anything since the protocol has been violated.
ch.finish();
assertThat(ch.readInbound(), is(nullValue()));
}
示例12: testFailsOnMissingResponse
import io.netty.channel.embedded.EmbeddedChannel; //導入方法依賴的package包/類
@Test
public void testFailsOnMissingResponse() {
HttpClientCodec codec = new HttpClientCodec(4096, 8192, 8192, true);
EmbeddedChannel ch = new EmbeddedChannel(codec);
assertTrue(ch.writeOutbound(new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET,
"http://localhost/")));
assertNotNull(releaseLater(ch.readOutbound()));
try {
ch.finish();
fail();
} catch (CodecException e) {
assertTrue(e instanceof PrematureChannelClosureException);
}
}
示例13: testDecodeWithStrip
import io.netty.channel.embedded.EmbeddedChannel; //導入方法依賴的package包/類
@Test
public void testDecodeWithStrip() throws Exception {
EmbeddedChannel ch = new EmbeddedChannel(new LineBasedFrameDecoder(8192, true, false));
ch.writeInbound(copiedBuffer("first\r\nsecond\nthird", CharsetUtil.US_ASCII));
assertEquals("first", releaseLater((ByteBuf) ch.readInbound()).toString(CharsetUtil.US_ASCII));
assertEquals("second", releaseLater((ByteBuf) ch.readInbound()).toString(CharsetUtil.US_ASCII));
assertNull(ch.readInbound());
ch.finish();
ReferenceCountUtil.release(ch.readInbound());
}
示例14: testDecode
import io.netty.channel.embedded.EmbeddedChannel; //導入方法依賴的package包/類
@Test
public void testDecode() throws Exception {
EmbeddedChannel ch = new EmbeddedChannel(
new DelimiterBasedFrameDecoder(8192, true, Delimiters.lineDelimiter()));
ch.writeInbound(Unpooled.copiedBuffer("first\r\nsecond\nthird", CharsetUtil.US_ASCII));
assertEquals("first", releaseLater((ByteBuf) ch.readInbound()).toString(CharsetUtil.US_ASCII));
assertEquals("second", releaseLater((ByteBuf) ch.readInbound()).toString(CharsetUtil.US_ASCII));
assertNull(ch.readInbound());
ch.finish();
ReferenceCountUtil.release(ch.readInbound());
}
示例15: testMultipleLinesStrippedDelimiters
import io.netty.channel.embedded.EmbeddedChannel; //導入方法依賴的package包/類
@Test
public void testMultipleLinesStrippedDelimiters() {
EmbeddedChannel ch = new EmbeddedChannel(new DelimiterBasedFrameDecoder(8192, true,
Delimiters.lineDelimiter()));
ch.writeInbound(Unpooled.copiedBuffer("TestLine\r\ng\r\n", Charset.defaultCharset()));
assertEquals("TestLine", releaseLater((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset()));
assertEquals("g", releaseLater((ByteBuf) ch.readInbound()).toString(Charset.defaultCharset()));
assertNull(ch.readInbound());
ch.finish();
}