Search in sources :

Example 26 with HttpRequest

use of io.netty.handler.codec.http.HttpRequest 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 27 with HttpRequest

use of io.netty.handler.codec.http.HttpRequest 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 28 with HttpRequest

use of io.netty.handler.codec.http.HttpRequest 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 29 with HttpRequest

use of io.netty.handler.codec.http.HttpRequest 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 30 with HttpRequest

use of io.netty.handler.codec.http.HttpRequest 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

HttpRequest (io.netty.handler.codec.http.HttpRequest)114 Test (org.junit.Test)38 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)29 HttpResponse (io.netty.handler.codec.http.HttpResponse)26 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)22 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)19 DefaultHttpRequest (io.netty.handler.codec.http.DefaultHttpRequest)17 ByteBuf (io.netty.buffer.ByteBuf)13 HttpContent (io.netty.handler.codec.http.HttpContent)13 HttpHeaders (io.netty.handler.codec.http.HttpHeaders)13 LastHttpContent (io.netty.handler.codec.http.LastHttpContent)12 URI (java.net.URI)12 Channel (io.netty.channel.Channel)10 ChannelFuture (io.netty.channel.ChannelFuture)9 NioSocketChannel (io.netty.channel.socket.nio.NioSocketChannel)9 FullHttpResponse (io.netty.handler.codec.http.FullHttpResponse)9 Map (java.util.Map)9 DefaultHttpResponse (io.netty.handler.codec.http.DefaultHttpResponse)7 Endpoint (com.nike.riposte.server.http.Endpoint)6 Bootstrap (io.netty.bootstrap.Bootstrap)6