public void CustomHandshake()
        {
            var handshaker = new PerMessageDeflateServerExtensionHandshaker(6, true, 10, true, true);

            var parameters = new Dictionary <string, string>
            {
                { ClientMaxWindow, null },
                { ServerMaxWindow, "12" },
                { ClientNoContext, null },
                { ServerNoContext, null }
            };

            IWebSocketServerExtension extension = handshaker.HandshakeExtension(
                new WebSocketExtensionData(PerMessageDeflateExtension, parameters));

            Assert.NotNull(extension);
            Assert.Equal(WebSocketRsv.Rsv1, extension.Rsv);
            Assert.IsType <PerMessageDeflateDecoder>(extension.NewExtensionDecoder());
            Assert.IsType <PerMessageDeflateEncoder>(extension.NewExtensionEncoder());

            WebSocketExtensionData data = extension.NewReponseData();

            Assert.Equal(PerMessageDeflateExtension, data.Name);
            Assert.Contains(ClientMaxWindow, data.Parameters.Keys);
            Assert.Equal("10", data.Parameters[ClientMaxWindow]);
            Assert.Contains(ServerMaxWindow, data.Parameters.Keys);
            Assert.Equal("12", data.Parameters[ServerMaxWindow]);

            parameters = new Dictionary <string, string>
            {
                { ServerMaxWindow, "12" },
                { ServerNoContext, null }
            };
            extension = handshaker.HandshakeExtension(
                new WebSocketExtensionData(PerMessageDeflateExtension, parameters));

            Assert.NotNull(extension);
            Assert.Equal(WebSocketRsv.Rsv1, extension.Rsv);
            Assert.IsType <PerMessageDeflateDecoder>(extension.NewExtensionDecoder());
            Assert.IsType <PerMessageDeflateEncoder>(extension.NewExtensionEncoder());

            data = extension.NewReponseData();

            Assert.Equal(PerMessageDeflateExtension, data.Name);
            Assert.Equal(2, data.Parameters.Count);
            Assert.Contains(ServerMaxWindow, data.Parameters.Keys);
            Assert.Equal("12", data.Parameters[ServerMaxWindow]);
            Assert.Contains(ServerNoContext, data.Parameters.Keys);

            parameters = new Dictionary <string, string>();
            extension  = handshaker.HandshakeExtension(
                new WebSocketExtensionData(PerMessageDeflateExtension, parameters));
            Assert.NotNull(extension);

            data = extension.NewReponseData();
            Assert.Equal(PerMessageDeflateExtension, data.Name);
            Assert.Empty(data.Parameters);
        }
        public void NormalHandshake()
        {
            var handshaker = new PerMessageDeflateServerExtensionHandshaker();
            IWebSocketServerExtension extension = handshaker.HandshakeExtension(
                new WebSocketExtensionData(PerMessageDeflateExtension, new Dictionary <string, string>()));

            Assert.NotNull(extension);
            Assert.Equal(WebSocketRsv.Rsv1, extension.Rsv);
            Assert.IsType <PerMessageDeflateDecoder>(extension.NewExtensionDecoder());
            Assert.IsType <PerMessageDeflateEncoder>(extension.NewExtensionEncoder());

            WebSocketExtensionData data = extension.NewReponseData();

            Assert.Equal(PerMessageDeflateExtension, data.Name);
            Assert.Empty(data.Parameters);

            var parameters = new Dictionary <string, string>
            {
                { ClientMaxWindow, null },
                { ClientNoContext, null }
            };

            extension = handshaker.HandshakeExtension(
                new WebSocketExtensionData(PerMessageDeflateExtension, parameters));

            Assert.NotNull(extension);
            Assert.Equal(WebSocketRsv.Rsv1, extension.Rsv);
            Assert.IsType <PerMessageDeflateDecoder>(extension.NewExtensionDecoder());
            Assert.IsType <PerMessageDeflateEncoder>(extension.NewExtensionEncoder());

            data = extension.NewReponseData();

            Assert.Equal(PerMessageDeflateExtension, data.Name);
            Assert.Empty(data.Parameters);

            parameters = new Dictionary <string, string>
            {
                { ServerMaxWindow, "12" },
                { ServerNoContext, null }
            };

            extension = handshaker.HandshakeExtension(
                new WebSocketExtensionData(PerMessageDeflateExtension, parameters));
            Assert.Null(extension);
        }
        public void WebkitHandshake()
        {
            var handshaker = new DeflateFrameServerExtensionHandshaker();
            IWebSocketServerExtension extension = handshaker.HandshakeExtension(
                new WebSocketExtensionData(XWebkitDeflateFrameExtension, new Dictionary <string, string>()));

            Assert.NotNull(extension);
            Assert.Equal(WebSocketRsv.Rsv1, extension.Rsv);
            Assert.IsType <PerFrameDeflateDecoder>(extension.NewExtensionDecoder());
            Assert.IsType <PerFrameDeflateEncoder>(extension.NewExtensionEncoder());
        }
        private static void SwitchWebSocketExtensionHandler(Task promise, object state)
        {
            var wrapped         = (Tuple <IChannelHandlerContext, List <IWebSocketServerExtension> >)state;
            var ctx             = wrapped.Item1;
            var validExtensions = wrapped.Item2;
            var pipeline        = ctx.Pipeline;

            if (promise.IsSuccess())
            {
                for (int i = 0; i < validExtensions.Count; i++)
                {
                    IWebSocketServerExtension extension = validExtensions[i];
                    WebSocketExtensionDecoder decoder   = extension.NewExtensionDecoder();
                    WebSocketExtensionEncoder encoder   = extension.NewExtensionEncoder();
                    _ = pipeline
                        .AddAfter(ctx.Name, decoder.GetType().Name, decoder)
                        .AddAfter(ctx.Name, encoder.GetType().Name, encoder);
                }
            }
        }