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();
}
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();
}
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();
}
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"));
}
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);
}
Aggregations