コード例 #1
0
        public void SerializesMessageCorrectly_MsgSerial(long msgSerial)
        {
            // Arrange
            MsgPackMessageSerializer serializer = new MsgPackMessageSerializer();
            ProtocolMessage          message    = new ProtocolMessage()
            {
                MsgSerial = msgSerial
            };
            List <byte> expectedMessage = new List <byte>();

            expectedMessage.Add(0x82);
            expectedMessage.AddRange(SerializeString("action"));
            expectedMessage.Add(0);
            expectedMessage.AddRange(SerializeString("msgSerial"));
            if (Math.Abs(msgSerial) < 255)
            {
                expectedMessage.Add(BitConverter.GetBytes(msgSerial).First());
            }
            else
            {
                expectedMessage.Add(0xd1);
                expectedMessage.AddRange(BitConverter.GetBytes(msgSerial).TakeWhile(c => c > 0).Reverse());
            }

            // Act
            object result = serializer.SerializeProtocolMessage(message);

            // Assert
            Assert.IsType <byte[]>(result);
            Assert.Equal <byte[]>(expectedMessage.ToArray(), result as byte[]);
        }
コード例 #2
0
        public void SerializesMessageCorrectly_Channel(string channel)
        {
            // Arrange
            MsgPackMessageSerializer serializer = new MsgPackMessageSerializer();
            ProtocolMessage          message    = new ProtocolMessage()
            {
                Channel = channel
            };
            List <byte> expectedMessage = new List <byte>();

            expectedMessage.Add(0x82);
            expectedMessage.AddRange(SerializeString("action"));
            expectedMessage.Add(0);
            if (!string.IsNullOrEmpty(channel))
            {
                expectedMessage[0]++;
                expectedMessage.AddRange(SerializeString("channel"));
                expectedMessage.AddRange(SerializeString(channel));
            }
            expectedMessage.AddRange(SerializeString("msgSerial"));
            expectedMessage.Add(0);

            // Act
            object result = serializer.SerializeProtocolMessage(message);

            // Assert
            Assert.IsType <byte[]>(result);
            Assert.Equal <byte[]>(expectedMessage.ToArray(), result as byte[]);
        }
コード例 #3
0
        public void SerializesMessageCorrectly_Action(ProtocolMessage.MessageAction messageAction)
        {
            // Arrange
            MsgPackMessageSerializer serializer = new MsgPackMessageSerializer();
            ProtocolMessage          message    = new ProtocolMessage(messageAction);
            List <byte> expectedMessage         = new List <byte>();

            expectedMessage.Add(0x82);
            expectedMessage.AddRange(SerializeString("action"));
            expectedMessage.Add((byte)messageAction);
            expectedMessage.AddRange(SerializeString("msgSerial"));
            expectedMessage.Add(0);

            // Act
            object result = serializer.SerializeProtocolMessage(message);

            // Assert
            Assert.IsType <byte[]>(result);
            Assert.Equal <byte[]>(expectedMessage.ToArray(), result as byte[]);
        }
コード例 #4
0
        public void SerializesMessageCorrectly_Presence(params PresenceMessage[] messages)
        {
            // Arrange
            MsgPackMessageSerializer serializer = new MsgPackMessageSerializer();
            ProtocolMessage          message    = new ProtocolMessage()
            {
                Presence = messages
            };
            List <byte> expectedMessage = new List <byte>();

            expectedMessage.Add(0x82);
            expectedMessage.AddRange(SerializeString("action"));
            expectedMessage.Add(0);
            expectedMessage.AddRange(SerializeString("msgSerial"));
            expectedMessage.Add(0);
            if (messages.Length > 0)
            {
                expectedMessage[0]++;
                expectedMessage.AddRange(SerializeString("presence"));
                expectedMessage.Add((byte)((0x09 << 4) + messages.Length));
                foreach (PresenceMessage msg in messages)
                {
                    expectedMessage.Add((0x08 << 4) + 1);
                    expectedMessage[expectedMessage.Count - 1] += (byte)(string.IsNullOrEmpty(msg.ClientId) ? 0 : 1);
                    expectedMessage.AddRange(SerializeString("action"));
                    expectedMessage.Add((byte)msg.Action);
                    if (!string.IsNullOrEmpty(msg.ClientId))
                    {
                        expectedMessage.AddRange(SerializeString("clientId"));
                        expectedMessage.AddRange(SerializeString(msg.ClientId));
                    }
                }
            }

            // Act
            object result = serializer.SerializeProtocolMessage(message);

            // Assert
            Assert.IsType <byte[]>(result);
            Assert.Equal <byte[]>(expectedMessage.ToArray(), result as byte[]);
        }
コード例 #5
0
        public void SerializesMessageCorrectly_Messages(params Message[] messages)
        {
            // Arrange
            MsgPackMessageSerializer serializer = new MsgPackMessageSerializer();
            ProtocolMessage          message    = new ProtocolMessage()
            {
                Messages = messages
            };
            List <byte> expectedMessage = new List <byte>();

            expectedMessage.Add(0x82);
            expectedMessage.AddRange(SerializeString("action"));
            expectedMessage.Add(0);
            expectedMessage.AddRange(SerializeString("msgSerial"));
            expectedMessage.Add(0);
            var validMessages = messages.Where(c => !string.IsNullOrEmpty(c.Name));

            if (validMessages.Any())
            {
                expectedMessage[0]++;
                expectedMessage.AddRange(SerializeString("messages"));
                expectedMessage.Add((byte)((0x09 << 4) + validMessages.Count()));
                foreach (Message msg in validMessages)
                {
                    expectedMessage.Add((0x08 << 4) + 1);
                    expectedMessage.AddRange(SerializeString("name"));
                    expectedMessage.AddRange(SerializeString(msg.Name));
                }
            }

            // Act
            object result = serializer.SerializeProtocolMessage(message);

            // Assert
            Assert.IsType <byte[]>(result);
            Assert.Equal <byte[]>(expectedMessage.ToArray(), result as byte[]);
        }