public void ShouldCallOnMessageFromSplitFrame()
        {
            const string firstPart = "Blah blah";
            const string secondPart = "Do Data";
            const string expected = firstPart + secondPart;

            var firstFrame = new Hybi14DataFrame
                {
                    FrameType = FrameType.Text,
                    IsFinal = false,
                    IsMasked = true,
                    MaskKey = 342808,
                    Payload = Encoding.UTF8.GetBytes(firstPart)
                };

            var secondFrame = new Hybi14DataFrame
                {
                    FrameType = FrameType.Continuation,
                    IsFinal = true,
                    IsMasked = true,
                    MaskKey = 345808,
                    Payload = Encoding.UTF8.GetBytes(secondPart)
                };

            string result = null;
            _onMessage = s => result = s;
            _handler.Receive(firstFrame.ToBytes());
            _handler.Receive(secondFrame.ToBytes());

            Assert.AreEqual(expected, result);
        }
Beispiel #2
0
        public void ShouldCallOnMessageFromSplitFrame()
        {
            const string firstPart  = "Blah blah";
            const string secondPart = "Do Data";
            const string expected   = firstPart + secondPart;

            var firstFrame = new Hybi14DataFrame
            {
                FrameType = FrameType.Text,
                IsFinal   = false,
                IsMasked  = true,
                MaskKey   = 342808,
                Payload   = Encoding.UTF8.GetBytes(firstPart)
            };

            var secondFrame = new Hybi14DataFrame
            {
                FrameType = FrameType.Continuation,
                IsFinal   = true,
                IsMasked  = true,
                MaskKey   = 345808,
                Payload   = Encoding.UTF8.GetBytes(secondPart)
            };

            string result = null;

            _onMessage = s => result = s;
            _handler.Receive(firstFrame.ToBytes());
            _handler.Receive(secondFrame.ToBytes());

            Assert.AreEqual(expected, result);
        }
Beispiel #3
0
        public void ShouldThrowWhenRecievingUnexpectedContinuation()
        {
            var frame = new Hybi14DataFrame
            {
                FrameType = FrameType.Continuation,
                IsFinal   = true,
                IsMasked  = true,
                MaskKey   = 345808,
                Payload   = Encoding.UTF8.GetBytes("continue")
            };

            Assert.Catch <WebSocketException>(() => _handler.Receive(frame.ToBytes()));
        }
        public void ShouldTransformBytesInBothDirections()
        {
            const string original = "Whoa";
            const int    key      = 1343834;

            var bytes = Encoding.UTF8.GetBytes(original);

            var transformed = Hybi14DataFrame.TransformBytes(bytes, key);
            var result      = Hybi14DataFrame.TransformBytes(transformed, key);

            var decoded = Encoding.UTF8.GetString(result);

            Assert.AreEqual(original, decoded);
        }
        public void ShouldConvertToBytes()
        {
            var frame = new Hybi14DataFrame
            {
                IsFinal   = true,
                IsMasked  = false,
                FrameType = FrameType.Text,
                Payload   = Encoding.UTF8.GetBytes("Hello")
            };

            var expected = new byte[] { 129, 5, 72, 101, 108, 108, 111 };
            var actual   = frame.ToBytes();

            Assert.AreEqual(expected, actual);
        }
 public void ShouldConvertToBytes()
 {
     var frame = new Hybi14DataFrame
     {
         IsFinal = true,
         IsMasked = false,
         FrameType = FrameType.Text,
         Payload = Encoding.UTF8.GetBytes("Hello")
     };
     
     var expected = new byte[]{ 129, 5, 72, 101, 108, 108, 111 };
     var actual = frame.ToBytes();
     
     Assert.AreEqual(expected, actual);
 }
Beispiel #7
0
        public void ShouldThrowInvalidFrameOnInvalidUTF8()
        {
            var frame = new Hybi14DataFrame
            {
                FrameType = FrameType.Text,
                IsFinal   = true,
                IsMasked  = true,
                MaskKey   = 34398,
                Payload   = new byte[] { 0, 7, 3, 2, byte.MaxValue }
            };

            var ex = Assert.Throws <WebSocketException>(() => _handler.Receive(frame.ToBytes()));

            Assert.AreEqual(WebSocketStatusCodes.InvalidFramePayloadData, ex.StatusCode);
        }
Beispiel #8
0
        public void ShouldThrowOnCloseFrameTooLong()
        {
            var frame = new Hybi14DataFrame
            {
                FrameType = FrameType.Close,
                IsFinal   = true,
                IsMasked  = true,
                MaskKey   = 5232,
                Payload   = Encoding.UTF8.GetBytes(new String('x', 128))
            };

            var ex = Assert.Throws <WebSocketException>(() => _handler.Receive(frame.ToBytes()));

            Assert.AreEqual(WebSocketStatusCodes.ProtocolError, ex.StatusCode);
        }
 public void ShouldConvertPayloadsOver125BytesToBytes()
 {
     var frame = new Hybi14DataFrame
     {
         IsFinal = true,
         IsMasked = false,
         FrameType = FrameType.Text,
         Payload = Encoding.UTF8.GetBytes(new string('x', 140))
     };
     
     var expected = new List<byte>{ 129, 126, 0, 140};
     expected.AddRange(frame.Payload);
     
     var actual = frame.ToBytes();
     
     Assert.AreEqual(expected, actual.ToArray());
 }
Beispiel #10
0
        public void ShouldCallOnBinaryWhenBinaryFrame()
        {
            var expected = new byte[] {1, 2, byte.MaxValue, byte.MinValue};
            var frame = new Hybi14DataFrame
                {
                    FrameType = FrameType.Binary,
                    IsFinal = true,
                    IsMasked = true,
                    MaskKey = 234234,
                    Payload = expected
                };

            byte[] result = null;
            _onBinary = b => result = b;
            _handler.Receive(frame.ToBytes());

            Assert.AreEqual(expected, result);
        }
Beispiel #11
0
        public void ShouldCallOnBinaryWhenBinaryFrame()
        {
            var expected = new byte[] { 1, 2, byte.MaxValue, byte.MinValue };
            var frame    = new Hybi14DataFrame
            {
                FrameType = FrameType.Binary,
                IsFinal   = true,
                IsMasked  = true,
                MaskKey   = 234234,
                Payload   = expected
            };

            byte[] result = null;
            _onBinary = b => result = b;
            _handler.Receive(frame.ToBytes());

            Assert.AreEqual(expected, result);
        }
Beispiel #12
0
        public void ShouldCallOnMessageWhenRecievingTextFrameLargerThanUInt16()
        {
            var expected = new string('x', UInt16.MaxValue) + new string('x', UInt16.MaxValue);
            var frame    = new Hybi14DataFrame
            {
                FrameType = FrameType.Text,
                IsFinal   = true,
                IsMasked  = true,
                MaskKey   = 34298,
                Payload   = Encoding.UTF8.GetBytes(expected)
            };

            string result = null;

            _onMessage = s => result = s;
            _handler.Receive(frame.ToBytes());

            Assert.AreEqual(expected, result);
        }
Beispiel #13
0
        public void ShouldCloseOnCloseFromText()
        {
            var payload = 1000.ToBigEndianBytes < ushort > ().Concat(Encoding.UTF8.GetBytes("Reason")).ToArray();
            var frame   = new Hybi14DataFrame
            {
                FrameType = FrameType.Close,
                IsFinal   = true,
                IsMasked  = true,
                MaskKey   = 5232,
                Payload   = payload
            };

            var hit = false;

            _onClose = () => hit = true;
            _handler.Receive(frame.ToBytes());

            Assert.IsTrue(hit);
        }
Beispiel #14
0
        public void ShouldNotCallOnMessageOnNonFinalFrame()
        {
            const string expected = "Blah blah";
            var          frame    = new Hybi14DataFrame
            {
                FrameType = FrameType.Text,
                IsFinal   = false,
                IsMasked  = true,
                MaskKey   = 34298,
                Payload   = Encoding.UTF8.GetBytes(expected)
            };

            var hit = false;

            _onMessage = s => hit = true;
            _handler.Receive(frame.ToBytes());

            Assert.IsFalse(hit);
        }
Beispiel #15
0
        public void ShouldCallOnMessageWhenRecievingTextFrame()
        {
            const string expected = "This be a test";
            var          frame    = new Hybi14DataFrame
            {
                FrameType = FrameType.Text,
                IsFinal   = true,
                IsMasked  = true,
                MaskKey   = 34298,
                Payload   = Encoding.UTF8.GetBytes(expected)
            };

            string result = null;

            _onMessage = s => result = s;
            _handler.Receive(frame.ToBytes());

            Assert.AreEqual(expected, result);
        }
        public void ShouldConvertPayloadsOver125BytesToBytes()
        {
            var frame = new Hybi14DataFrame
            {
                IsFinal   = true,
                IsMasked  = false,
                FrameType = FrameType.Text,
                Payload   = Encoding.UTF8.GetBytes(new string('x', 140))
            };

            var expected = new List <byte> {
                129, 126, 0, 140
            };

            expected.AddRange(frame.Payload);

            var actual = frame.ToBytes();

            Assert.AreEqual(expected, actual.ToArray());
        }
Beispiel #17
0
        public void ShouldCallOnMessageWhenRecievingTextFrameLargerThanUInt16()
        {
            var expected = new string('x', UInt16.MaxValue) + new string('x', UInt16.MaxValue);
            var frame = new Hybi14DataFrame
                {
                    FrameType = FrameType.Text,
                    IsFinal = true,
                    IsMasked = true,
                    MaskKey = 34298,
                    Payload = Encoding.UTF8.GetBytes(expected)
                };

            string result = null;
            _onMessage = s => result = s;
            _handler.Receive(frame.ToBytes());

            Assert.AreEqual(expected, result);
        }
Beispiel #18
0
        public void ShouldThrowOnCloseFrameTooLong()
        {
            var frame = new Hybi14DataFrame
                {
                    FrameType = FrameType.Close,
                    IsFinal = true,
                    IsMasked = true,
                    MaskKey = 5232,
                    Payload = Encoding.UTF8.GetBytes(new String('x',128))
                };

            var ex = Assert.Throws<WebSocketException>(() => _handler.Receive(frame.ToBytes()));
            Assert.AreEqual(WebSocketStatusCodes.ProtocolError, ex.StatusCode);
        }
Beispiel #19
0
        public void ShouldNotCallOnMessageOnNonFinalFrame()
        {
            const string expected = "Blah blah";
            var frame = new Hybi14DataFrame
                {
                    FrameType = FrameType.Text,
                    IsFinal = false,
                    IsMasked = true,
                    MaskKey = 34298,
                    Payload = Encoding.UTF8.GetBytes(expected)
                };

            var hit = false;
            _onMessage = s => hit = true;
            _handler.Receive(frame.ToBytes());

            Assert.IsFalse(hit);
        }
Beispiel #20
0
        public void ShouldThrowWhenRecievingUnexpectedContinuation()
        {
            var frame = new Hybi14DataFrame
                {
                    FrameType = FrameType.Continuation,
                    IsFinal = true,
                    IsMasked = true,
                    MaskKey = 345808,
                    Payload = Encoding.UTF8.GetBytes("continue")
                };

            Assert.Catch<WebSocketException>(() => _handler.Receive(frame.ToBytes()));
        }
Beispiel #21
0
        public void ShouldCloseOnCloseFromText()
        {
            var payload = Fleck2Extensions.ToArray(1000.ToBigEndianBytes<ushort>().Concat(Encoding.UTF8.GetBytes("Reason")));
            var frame = new Hybi14DataFrame
                {
                    FrameType = FrameType.Close,
                    IsFinal = true,
                    IsMasked = true,
                    MaskKey = 5232,
                    Payload = payload
                };

            var hit = false;
            _onClose = () => hit = true;
            _handler.Receive(frame.ToBytes());

            Assert.IsTrue(hit);
        }
Beispiel #22
0
        public void ShouldCallOnMessageWhenRecievingTextFrame()
        {
            const string expected = "This be a test";
            var frame = new Hybi14DataFrame
                {
                    FrameType = FrameType.Text,
                    IsFinal = true,
                    IsMasked = true,
                    MaskKey = 34298,
                    Payload = Encoding.UTF8.GetBytes(expected)
                };

            string result = null;
            _onMessage = s => result = s;
            _handler.Receive(frame.ToBytes());

            Assert.AreEqual(expected, result);
        }
Beispiel #23
0
        public void ShouldThrowInvalidFrameOnInvalidUtf8()
        {
            var frame = new Hybi14DataFrame
                {
                    FrameType = FrameType.Text,
                    IsFinal = true,
                    IsMasked = true,
                    MaskKey = 34398,
                    Payload = new byte[] { 0, 7, 3, 2, byte.MaxValue}
                };

            var ex = Assert.Throws<WebSocketException>(() => _handler.Receive(frame.ToBytes()));
            Assert.AreEqual(WebSocketStatusCodes.InvalidFramePayloadData, ex.StatusCode);
        }