public void upgrade()
        {
            IHttpMessage actual = null;

            var decoder = new WebSocketDecoder();
            decoder.MessageReceived = o => actual = (IHttpMessage)o;
            sendWebSocketUpgradeRequest(decoder);

            actual.Headers["host"].Should().Be("localhost");
            actual.Headers["connection"].Should().Be("upgrade");
            actual.Headers["upgrade"].Should().Be("websocket");
        }
        public void text_message()
        {
            IWebSocketMessage actual = null;
            var buffer = new SocketBufferFake();
            buffer.Buffer = new byte[] { 129, 3, (byte)'a', (byte)'b', (byte)'c' };
            buffer.BytesTransferred = buffer.Buffer.Length;

            var decoder = new WebSocketDecoder();
            sendWebSocketUpgradeRequest(decoder);
            decoder.MessageReceived = o => actual = (IWebSocketMessage)o;
            decoder.ProcessReadBytes(buffer);

            actual.Opcode.Should().Be(WebSocketOpcode.Text);
            actual.Payload.Length.Should().Be(3);
            actual.Payload.ReadByte().Should().Be((int)'a');
            actual.Payload.ReadByte().Should().Be((int)'b');
            actual.Payload.ReadByte().Should().Be((int)'c');
        }
        public void binary_message()
        {
            IWebSocketMessage actual = null;
            var buffer = new SocketBufferFake();
            buffer.Buffer = new byte[] { 130, 3, 1, 2, 3 };
            buffer.BytesTransferred = buffer.Buffer.Length;

            var decoder = new WebSocketDecoder();
            sendWebSocketUpgradeRequest(decoder);
            decoder.MessageReceived = o => actual = (IWebSocketMessage)o;
            decoder.ProcessReadBytes(buffer);

            actual.Opcode.Should().Be(WebSocketOpcode.Binary);
            actual.Payload.Length.Should().Be(3);
            actual.Payload.ReadByte().Should().Be(1);
            actual.Payload.ReadByte().Should().Be(2);
            actual.Payload.ReadByte().Should().Be(3);
        }
        public void close_message()
        {
            IWebSocketMessage actual = null;
            var buffer = new SocketBufferFake();
            buffer.Buffer = new byte[] { 136, 0 };
            buffer.BytesTransferred = buffer.Buffer.Length;

            var decoder = new WebSocketDecoder();
            sendWebSocketUpgradeRequest(decoder);
            decoder.MessageReceived = o => actual = (IWebSocketMessage)o;
            decoder.ProcessReadBytes(buffer);

            actual.Opcode.Should().Be(WebSocketOpcode.Close);
            actual.Payload.Length.Should().Be(0);
        }
        public void big_message()
        {
            IWebSocketMessage actual = null;

            var buffer1 = new SocketBufferFake();
            buffer1.Buffer = (new byte[] { 2, 126, 255, 255 }).Concat(new byte[WebSocketFrame.FragmentLength]).ToArray(); // continuation frame
            buffer1.BytesTransferred = buffer1.Buffer.Length;

            var buffer2 = new SocketBufferFake();
            buffer2.Buffer = new byte[] { 128, 1, 1 }; // final frame
            buffer2.BytesTransferred = buffer2.Buffer.Length;

            var decoder = new WebSocketDecoder();
            sendWebSocketUpgradeRequest(decoder);
            decoder.MessageReceived = o => actual = (IWebSocketMessage)o;
            decoder.ProcessReadBytes(buffer1);
            decoder.ProcessReadBytes(buffer2);

            actual.Opcode.Should().Be(WebSocketOpcode.Binary);
            actual.Payload.Length.Should().Be(WebSocketFrame.FragmentLength + 1);
            actual.Payload.Position = WebSocketFrame.FragmentLength;
            actual.Payload.ReadByte().Should().Be(1);   
        }