Exemplo n.º 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);
        }
Exemplo n.º 2
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);
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
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);
        }
Exemplo n.º 5
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);
        }
Exemplo n.º 6
0
        public void ShouldThrowWhenRecievingUnexpectedContinuation()
        {
            var frame = new Hybi14DataFrame {
                FrameType = FrameType.Continuation,
                IsFinal   = true,
                IsMasked  = true,
                MaskKey   = 345808,
                Payload   = Encoding.UTF8.GetBytes("continue")
            };

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

            Assert.AreEqual(WebSocketStatusCodes.ProtocolError, ex.StatusCode);
        }
Exemplo n.º 7
0
        public void ShouldCallOnPongWhenPongFrame()
        {
            var expected = new byte[] { 1, 2, byte.MaxValue, byte.MinValue };
            var frame    = new Hybi14DataFrame {
                FrameType = FrameType.Pong,
                IsFinal   = true,
                IsMasked  = true,
                MaskKey   = 234234,
                Payload   = expected
            };

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

            Assert.AreEqual(expected, result);
        }
Exemplo n.º 8
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);
        }
Exemplo n.º 9
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);
        }
Exemplo n.º 10
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);
        }
Exemplo n.º 11
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);
        }
Exemplo n.º 12
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());
        }