예제 #1
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);
        }
예제 #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);
        }
예제 #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);
        }
예제 #6
0
 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);
 }
예제 #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);
        }
예제 #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);
        }
예제 #9
0
 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());
 }
예제 #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);
        }
예제 #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);
        }
예제 #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);
        }
예제 #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);
        }
예제 #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);
        }
예제 #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());
        }
예제 #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);
        }
예제 #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);
        }
예제 #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);
        }
예제 #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()));
        }
예제 #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);
        }
예제 #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);
        }
예제 #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);
        }