@Test public void testWriteUtf8() { String usAscii = "Some UTF-8 like äÄ∏ŒŒ"; ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(16)); buf.writeBytes(usAscii.getBytes(CharsetUtil.UTF_8)); ByteBuf buf2 = ReferenceCountUtil.releaseLater(Unpooled.buffer(16)); ByteBufUtil.writeUtf8(buf2, usAscii); Assert.assertEquals(buf, buf2); }
@Test public void testWriteUsAsciiString() { AsciiString usAscii = new AsciiString("NettyRocks"); ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(16)); buf.writeBytes(usAscii.toString().getBytes(CharsetUtil.US_ASCII)); ByteBuf buf2 = ReferenceCountUtil.releaseLater(Unpooled.buffer(16)); ByteBufUtil.writeAscii(buf2, usAscii); Assert.assertEquals(buf, buf2); }
@Test public void testWriteUsAsciiWrapped() { String usAscii = "NettyRocks"; ByteBuf buf = Unpooled.unreleasableBuffer(ReferenceCountUtil.releaseLater(Unpooled.buffer(16))); assertWrapped(buf); buf.writeBytes(usAscii.getBytes(CharsetUtil.US_ASCII)); ByteBuf buf2 = Unpooled.unreleasableBuffer(ReferenceCountUtil.releaseLater(Unpooled.buffer(16))); assertWrapped(buf2); ByteBufUtil.writeAscii(buf2, usAscii); Assert.assertEquals(buf, buf2); }
@Test public void echo() { String m = "echo test\n"; EmbeddedChannel ch = new EmbeddedChannel(new EchoServerHandler()); ByteBuf in = Unpooled.copiedBuffer(m, CharsetUtil.UTF_8); ch.writeInbound(in); ByteBuf r = (ByteBuf) ch.readOutbound(); releaseLater(r); assertThat("응답이 없습니다", r, notNullValue()); assertThat("참조수는 1이어야 합니다", r.refCnt(), is(1)); assertThat("수신한 텍스트가 결과로 와야합니다", r.toString(CharsetUtil.UTF_8), equalTo(m)); }
private static void testPerformOpeningHandshake0(boolean subProtocol) { EmbeddedChannel ch = new EmbeddedChannel( new HttpObjectAggregator(42), new HttpRequestDecoder(), new HttpResponseEncoder()); FullHttpRequest req = ReferenceCountUtil.releaseLater( new DefaultFullHttpRequest( HTTP_1_1, HttpMethod.GET, "/chat", Unpooled.copiedBuffer("^n:ds[4U", CharsetUtil.US_ASCII))); req.headers().set(HttpHeaderNames.HOST, "server.example.com"); req.headers().set(HttpHeaderNames.UPGRADE, HttpHeaderValues.WEBSOCKET); req.headers().set(HttpHeaderNames.CONNECTION, "Upgrade"); req.headers().set(HttpHeaderNames.ORIGIN, "http://example.com"); req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_KEY1, "4 @1 46546xW%0l 1 5"); req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_KEY2, "12998 5 Y3 1 .P00"); req.headers().set(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL, "chat, superchat"); if (subProtocol) { new WebSocketServerHandshaker00("ws://example.com/chat", "chat", Integer.MAX_VALUE) .handshake(ch, req); } else { new WebSocketServerHandshaker00("ws://example.com/chat", null, Integer.MAX_VALUE) .handshake(ch, req); } EmbeddedChannel ch2 = new EmbeddedChannel(new HttpResponseDecoder()); ch2.writeInbound(ch.readOutbound()); HttpResponse res = ch2.readInbound(); Assert.assertEquals( "ws://example.com/chat", res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_LOCATION)); if (subProtocol) { Assert.assertEquals("chat", res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL)); } else { Assert.assertNull(res.headers().get(HttpHeaderNames.SEC_WEBSOCKET_PROTOCOL)); } LastHttpContent content = ch2.readInbound(); Assert.assertEquals("8jKS'y:G*Co,Wxa-", content.content().toString(CharsetUtil.US_ASCII)); content.release(); }