Search in sources :

Example 11 with HttpResponse

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();
}
Also used : HttpResponseEncoder(io.netty.handler.codec.http.HttpResponseEncoder) HttpObjectAggregator(io.netty.handler.codec.http.HttpObjectAggregator) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpRequestDecoder(io.netty.handler.codec.http.HttpRequestDecoder) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpResponse(io.netty.handler.codec.http.HttpResponse) HttpResponseDecoder(io.netty.handler.codec.http.HttpResponseDecoder) LastHttpContent(io.netty.handler.codec.http.LastHttpContent)

Example 12 with HttpResponse

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);
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpResponse(io.netty.handler.codec.http.HttpResponse) WebSocketServerExtensionHandler(io.netty.handler.codec.http.websocketx.extensions.WebSocketServerExtensionHandler) PerMessageDeflateServerExtensionHandshaker(io.netty.handler.codec.http.websocketx.extensions.compression.PerMessageDeflateServerExtensionHandshaker) Test(org.junit.Test)

Example 13 with HttpResponse

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);
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpResponse(io.netty.handler.codec.http.HttpResponse) WebSocketExtensionData(io.netty.handler.codec.http.websocketx.extensions.WebSocketExtensionData) Test(org.junit.Test)

Example 14 with HttpResponse

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);
}
Also used : DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) FullHttpResponse(io.netty.handler.codec.http.FullHttpResponse) DefaultHttpResponse(io.netty.handler.codec.http.DefaultHttpResponse) DefaultFullHttpResponse(io.netty.handler.codec.http.DefaultFullHttpResponse) HttpResponse(io.netty.handler.codec.http.HttpResponse) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 15 with HttpResponse

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();
}
Also used : HttpRequest(io.netty.handler.codec.http.HttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) HttpResponse(io.netty.handler.codec.http.HttpResponse) Test(org.junit.Test)

Aggregations

HttpResponse (io.netty.handler.codec.http.HttpResponse)118 DefaultFullHttpResponse (io.netty.handler.codec.http.DefaultFullHttpResponse)53 Test (org.junit.Test)50 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)30 HttpRequest (io.netty.handler.codec.http.HttpRequest)27 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)24 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)19 HttpContent (io.netty.handler.codec.http.HttpContent)18 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)18 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)14 ChannelFuture (io.netty.channel.ChannelFuture)12 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)12 ByteBuf (io.netty.buffer.ByteBuf)9 AsciiString (io.netty.util.AsciiString)8 HttpObject (io.netty.handler.codec.http.HttpObject)7 Map (java.util.Map)7 CoreMatchers.containsString (org.hamcrest.CoreMatchers.containsString)7 WebSocketExtensionData (io.netty.handler.codec.http.websocketx.extensions.WebSocketExtensionData)6 IOException (java.io.IOException)6 Settings (org.elasticsearch.common.settings.Settings)6