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); }
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); }
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); }
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()); }
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); }
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); }
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); }
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); }
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); }
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()); }
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 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 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); }
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); }