use of io.netty.handler.codec.http.HttpResponse in project netty by netty.
the class WebSocketServerHandshaker00Test method testPerformOpeningHandshake0.
private static void testPerformOpeningHandshake0(boolean subProtocol) {
EmbeddedChannel ch = new EmbeddedChannel(new HttpObjectAggregator(42), new HttpRequestDecoder(), new HttpResponseEncoder());
FullHttpRequest req = 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();
req.release();
}
use of io.netty.handler.codec.http.HttpResponse in project netty by netty.
the class WebSocketServerCompressionHandlerTest method testServerWindowSizeDisable.
@Test
public void testServerWindowSizeDisable() {
EmbeddedChannel ch = new EmbeddedChannel(new WebSocketServerExtensionHandler(new PerMessageDeflateServerExtensionHandshaker(6, false, 15, false, false)));
HttpRequest req = newUpgradeRequest(PERMESSAGE_DEFLATE_EXTENSION + "; " + SERVER_MAX_WINDOW + "=10");
ch.writeInbound(req);
HttpResponse res = newUpgradeResponse(null);
ch.writeOutbound(res);
HttpResponse res2 = ch.readOutbound();
Assert.assertFalse(res2.headers().contains(HttpHeaderNames.SEC_WEBSOCKET_EXTENSIONS));
Assert.assertTrue(ch.pipeline().get(PerMessageDeflateDecoder.class) == null);
Assert.assertTrue(ch.pipeline().get(PerMessageDeflateEncoder.class) == null);
}
use of io.netty.handler.codec.http.HttpResponse in project netty by netty.
the class WebSocketServerCompressionHandlerTest method testClientNoContext.
@Test
public void testClientNoContext() {
EmbeddedChannel ch = new EmbeddedChannel(new WebSocketServerCompressionHandler());
HttpRequest req = newUpgradeRequest(PERMESSAGE_DEFLATE_EXTENSION + "; " + CLIENT_NO_CONTEXT);
ch.writeInbound(req);
HttpResponse res = newUpgradeResponse(null);
ch.writeOutbound(res);
HttpResponse res2 = ch.readOutbound();
List<WebSocketExtensionData> exts = WebSocketExtensionUtil.extractExtensions(res2.headers().get(HttpHeaderNames.SEC_WEBSOCKET_EXTENSIONS));
Assert.assertEquals(PERMESSAGE_DEFLATE_EXTENSION, exts.get(0).name());
Assert.assertTrue(exts.get(0).parameters().isEmpty());
Assert.assertTrue(ch.pipeline().get(PerMessageDeflateDecoder.class) != null);
Assert.assertTrue(ch.pipeline().get(PerMessageDeflateEncoder.class) != null);
}
use of io.netty.handler.codec.http.HttpResponse in project netty by netty.
the class RtspEncoderTest method testSend200OkResponseWithoutBody.
/**
* Test of a 200 OK response, without body.
*/
@Test
public void testSend200OkResponseWithoutBody() {
String expected = "RTSP/1.0 200 OK\r\n" + "server: Testserver\r\n" + "cseq: 1\r\n" + "session: 2547019973447939919\r\n" + "\r\n";
HttpResponse response = new DefaultHttpResponse(RtspVersions.RTSP_1_0, RtspResponseStatuses.OK);
response.headers().add(RtspHeaderNames.SERVER, "Testserver");
response.headers().add(RtspHeaderNames.CSEQ, "1");
response.headers().add(RtspHeaderNames.SESSION, "2547019973447939919");
EmbeddedChannel ch = new EmbeddedChannel(new RtspEncoder());
ch.writeOutbound(response);
ByteBuf buf = ch.readOutbound();
String actual = buf.toString(CharsetUtil.UTF_8);
buf.release();
assertEquals(expected, actual);
}
use of io.netty.handler.codec.http.HttpResponse in project netty by netty.
the class WebSocketServerExtensionHandlerTest method testCompatibleExtensionTogetherSuccess.
@Test
public void testCompatibleExtensionTogetherSuccess() {
// initialize
when(mainHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("main"))).thenReturn(mainExtensionMock);
when(mainHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("fallback"))).thenReturn(null);
when(fallbackHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("fallback"))).thenReturn(fallbackExtensionMock);
when(fallbackHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("main"))).thenReturn(null);
when(mainExtensionMock.rsv()).thenReturn(WebSocketExtension.RSV1);
when(mainExtensionMock.newReponseData()).thenReturn(new WebSocketExtensionData("main", Collections.<String, String>emptyMap()));
when(mainExtensionMock.newExtensionEncoder()).thenReturn(new DummyEncoder());
when(mainExtensionMock.newExtensionDecoder()).thenReturn(new DummyDecoder());
when(fallbackExtensionMock.rsv()).thenReturn(WebSocketExtension.RSV2);
when(fallbackExtensionMock.newReponseData()).thenReturn(new WebSocketExtensionData("fallback", Collections.<String, String>emptyMap()));
when(fallbackExtensionMock.newExtensionEncoder()).thenReturn(new Dummy2Encoder());
when(fallbackExtensionMock.newExtensionDecoder()).thenReturn(new Dummy2Decoder());
// execute
EmbeddedChannel ch = new EmbeddedChannel(new WebSocketServerExtensionHandler(mainHandshakerMock, fallbackHandshakerMock));
HttpRequest req = newUpgradeRequest("main, fallback");
ch.writeInbound(req);
HttpResponse res = newUpgradeResponse(null);
ch.writeOutbound(res);
HttpResponse res2 = ch.readOutbound();
List<WebSocketExtensionData> resExts = WebSocketExtensionUtil.extractExtensions(res2.headers().get(HttpHeaderNames.SEC_WEBSOCKET_EXTENSIONS));
// test
assertEquals(2, resExts.size());
assertEquals("main", resExts.get(0).name());
assertEquals("fallback", resExts.get(1).name());
assertNotNull(ch.pipeline().get(DummyDecoder.class));
assertNotNull(ch.pipeline().get(DummyEncoder.class));
assertNotNull(ch.pipeline().get(Dummy2Decoder.class));
assertNotNull(ch.pipeline().get(Dummy2Encoder.class));
verify(mainHandshakerMock).handshakeExtension(webSocketExtensionDataMatcher("main"));
verify(mainHandshakerMock).handshakeExtension(webSocketExtensionDataMatcher("fallback"));
verify(fallbackHandshakerMock).handshakeExtension(webSocketExtensionDataMatcher("fallback"));
verify(mainExtensionMock, times(2)).rsv();
verify(mainExtensionMock).newReponseData();
verify(mainExtensionMock).newExtensionEncoder();
verify(mainExtensionMock).newExtensionDecoder();
verify(fallbackExtensionMock, times(2)).rsv();
verify(fallbackExtensionMock).newReponseData();
verify(fallbackExtensionMock).newExtensionEncoder();
verify(fallbackExtensionMock).newExtensionDecoder();
}
Aggregations