Search in sources :

Example 16 with HttpResponse

use of io.netty.handler.codec.http.HttpResponse in project netty by netty.

the class WebSocketServerExtensionHandlerTest method testMainSuccess.

@Test
public void testMainSuccess() {
    // 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.RSV1);
    // 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(1, resExts.size());
    assertEquals("main", resExts.get(0).name());
    assertTrue(resExts.get(0).parameters().isEmpty());
    assertNotNull(ch.pipeline().get(DummyDecoder.class));
    assertNotNull(ch.pipeline().get(DummyEncoder.class));
    verify(mainHandshakerMock, atLeastOnce()).handshakeExtension(webSocketExtensionDataMatcher("main"));
    verify(mainHandshakerMock, atLeastOnce()).handshakeExtension(webSocketExtensionDataMatcher("fallback"));
    verify(fallbackHandshakerMock, atLeastOnce()).handshakeExtension(webSocketExtensionDataMatcher("fallback"));
    verify(mainExtensionMock, atLeastOnce()).rsv();
    verify(mainExtensionMock).newReponseData();
    verify(mainExtensionMock).newExtensionEncoder();
    verify(mainExtensionMock).newExtensionDecoder();
    verify(fallbackExtensionMock, atLeastOnce()).rsv();
}
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)

Example 17 with HttpResponse

use of io.netty.handler.codec.http.HttpResponse in project netty by netty.

the class WebSocketClientExtensionHandlerTest method testAllSuccess.

@Test
public void testAllSuccess() {
    // initialize
    when(mainHandshakerMock.newRequestData()).thenReturn(new WebSocketExtensionData("main", Collections.<String, String>emptyMap()));
    when(mainHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("main"))).thenReturn(mainExtensionMock);
    when(mainHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("fallback"))).thenReturn(null);
    when(fallbackHandshakerMock.newRequestData()).thenReturn(new WebSocketExtensionData("fallback", Collections.<String, String>emptyMap()));
    when(fallbackHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("main"))).thenReturn(null);
    when(fallbackHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("fallback"))).thenReturn(fallbackExtensionMock);
    DummyEncoder mainEncoder = new DummyEncoder();
    DummyDecoder mainDecoder = new DummyDecoder();
    when(mainExtensionMock.rsv()).thenReturn(WebSocketExtension.RSV1);
    when(mainExtensionMock.newExtensionEncoder()).thenReturn(mainEncoder);
    when(mainExtensionMock.newExtensionDecoder()).thenReturn(mainDecoder);
    Dummy2Encoder fallbackEncoder = new Dummy2Encoder();
    Dummy2Decoder fallbackDecoder = new Dummy2Decoder();
    when(fallbackExtensionMock.rsv()).thenReturn(WebSocketExtension.RSV2);
    when(fallbackExtensionMock.newExtensionEncoder()).thenReturn(fallbackEncoder);
    when(fallbackExtensionMock.newExtensionDecoder()).thenReturn(fallbackDecoder);
    // execute
    EmbeddedChannel ch = new EmbeddedChannel(new WebSocketClientExtensionHandler(mainHandshakerMock, fallbackHandshakerMock));
    HttpRequest req = newUpgradeRequest(null);
    ch.writeOutbound(req);
    HttpRequest req2 = ch.readOutbound();
    List<WebSocketExtensionData> reqExts = WebSocketExtensionUtil.extractExtensions(req2.headers().get(HttpHeaderNames.SEC_WEBSOCKET_EXTENSIONS));
    HttpResponse res = newUpgradeResponse("main, fallback");
    ch.writeInbound(res);
    HttpResponse res2 = ch.readInbound();
    List<WebSocketExtensionData> resExts = WebSocketExtensionUtil.extractExtensions(res2.headers().get(HttpHeaderNames.SEC_WEBSOCKET_EXTENSIONS));
    // test
    assertEquals(2, reqExts.size());
    assertEquals("main", reqExts.get(0).name());
    assertEquals("fallback", reqExts.get(1).name());
    assertEquals(2, resExts.size());
    assertEquals("main", resExts.get(0).name());
    assertEquals("fallback", resExts.get(1).name());
    assertNotNull(ch.pipeline().context(mainEncoder));
    assertNotNull(ch.pipeline().context(mainDecoder));
    assertNotNull(ch.pipeline().context(fallbackEncoder));
    assertNotNull(ch.pipeline().context(fallbackDecoder));
    verify(mainHandshakerMock).newRequestData();
    verify(mainHandshakerMock).handshakeExtension(webSocketExtensionDataMatcher("main"));
    verify(mainHandshakerMock).handshakeExtension(webSocketExtensionDataMatcher("fallback"));
    verify(fallbackHandshakerMock).newRequestData();
    verify(fallbackHandshakerMock).handshakeExtension(webSocketExtensionDataMatcher("fallback"));
    verify(mainExtensionMock, atLeastOnce()).rsv();
    verify(mainExtensionMock).newExtensionEncoder();
    verify(mainExtensionMock).newExtensionDecoder();
    verify(fallbackExtensionMock, atLeastOnce()).rsv();
    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)

Example 18 with HttpResponse

use of io.netty.handler.codec.http.HttpResponse in project netty by netty.

the class WebSocketClientExtensionHandlerTest method testFallbackSuccess.

@Test
public void testFallbackSuccess() {
    // initialize
    when(mainHandshakerMock.newRequestData()).thenReturn(new WebSocketExtensionData("main", Collections.<String, String>emptyMap()));
    when(mainHandshakerMock.handshakeExtension(any(WebSocketExtensionData.class))).thenReturn(null);
    when(fallbackHandshakerMock.newRequestData()).thenReturn(new WebSocketExtensionData("fallback", Collections.<String, String>emptyMap()));
    when(fallbackHandshakerMock.handshakeExtension(any(WebSocketExtensionData.class))).thenReturn(fallbackExtensionMock);
    when(fallbackExtensionMock.rsv()).thenReturn(WebSocketExtension.RSV1);
    when(fallbackExtensionMock.newExtensionEncoder()).thenReturn(new DummyEncoder());
    when(fallbackExtensionMock.newExtensionDecoder()).thenReturn(new DummyDecoder());
    // execute
    EmbeddedChannel ch = new EmbeddedChannel(new WebSocketClientExtensionHandler(mainHandshakerMock, fallbackHandshakerMock));
    HttpRequest req = newUpgradeRequest(null);
    ch.writeOutbound(req);
    HttpRequest req2 = ch.readOutbound();
    List<WebSocketExtensionData> reqExts = WebSocketExtensionUtil.extractExtensions(req2.headers().get(HttpHeaderNames.SEC_WEBSOCKET_EXTENSIONS));
    HttpResponse res = newUpgradeResponse("fallback");
    ch.writeInbound(res);
    HttpResponse res2 = ch.readInbound();
    List<WebSocketExtensionData> resExts = WebSocketExtensionUtil.extractExtensions(res2.headers().get(HttpHeaderNames.SEC_WEBSOCKET_EXTENSIONS));
    // test
    assertEquals(2, reqExts.size());
    assertEquals("main", reqExts.get(0).name());
    assertEquals("fallback", reqExts.get(1).name());
    assertEquals(1, resExts.size());
    assertEquals("fallback", resExts.get(0).name());
    assertTrue(resExts.get(0).parameters().isEmpty());
    assertNotNull(ch.pipeline().get(DummyDecoder.class));
    assertNotNull(ch.pipeline().get(DummyEncoder.class));
    verify(mainHandshakerMock).newRequestData();
    verify(mainHandshakerMock).handshakeExtension(any(WebSocketExtensionData.class));
    verify(fallbackHandshakerMock).newRequestData();
    verify(fallbackHandshakerMock).handshakeExtension(any(WebSocketExtensionData.class));
    verify(fallbackExtensionMock, atLeastOnce()).rsv();
    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)

Example 19 with HttpResponse

use of io.netty.handler.codec.http.HttpResponse in project netty by netty.

the class WebSocketServerExtensionHandlerTest method testNoneExtensionMatchingSuccess.

@Test
public void testNoneExtensionMatchingSuccess() {
    // initialize
    when(mainHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("unknown"))).thenReturn(null);
    when(mainHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("unknown2"))).thenReturn(null);
    when(fallbackHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("unknown"))).thenReturn(null);
    when(fallbackHandshakerMock.handshakeExtension(webSocketExtensionDataMatcher("unknown2"))).thenReturn(null);
    // execute
    EmbeddedChannel ch = new EmbeddedChannel(new WebSocketServerExtensionHandler(mainHandshakerMock, fallbackHandshakerMock));
    HttpRequest req = newUpgradeRequest("unknown, unknown2");
    ch.writeInbound(req);
    HttpResponse res = newUpgradeResponse(null);
    ch.writeOutbound(res);
    HttpResponse res2 = ch.readOutbound();
    // test
    assertFalse(res2.headers().contains(HttpHeaderNames.SEC_WEBSOCKET_EXTENSIONS));
    verify(mainHandshakerMock).handshakeExtension(webSocketExtensionDataMatcher("unknown"));
    verify(mainHandshakerMock).handshakeExtension(webSocketExtensionDataMatcher("unknown2"));
    verify(fallbackHandshakerMock).handshakeExtension(webSocketExtensionDataMatcher("unknown"));
    verify(fallbackHandshakerMock).handshakeExtension(webSocketExtensionDataMatcher("unknown2"));
}
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)

Example 20 with HttpResponse

use of io.netty.handler.codec.http.HttpResponse in project netty by netty.

the class WebSocketServerCompressionHandlerTest method testClientWindowSizeUnavailable.

@Test
public void testClientWindowSizeUnavailable() {
    EmbeddedChannel ch = new EmbeddedChannel(new WebSocketServerExtensionHandler(new PerMessageDeflateServerExtensionHandshaker(6, false, 10, false, false)));
    HttpRequest req = newUpgradeRequest(PERMESSAGE_DEFLATE_EXTENSION);
    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) WebSocketServerExtensionHandler(io.netty.handler.codec.http.websocketx.extensions.WebSocketServerExtensionHandler) PerMessageDeflateServerExtensionHandshaker(io.netty.handler.codec.http.websocketx.extensions.compression.PerMessageDeflateServerExtensionHandshaker) WebSocketExtensionData(io.netty.handler.codec.http.websocketx.extensions.WebSocketExtensionData) 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