예제 #1
0
 /// <summary>
 /// Serializes the specified message type to a string that is expected
 /// to be handled by downstream clients.
 /// </summary>
 /// <param name="messageType">Type of the message.</param>
 /// <returns>System.String.</returns>
 public static string Serialize(ApolloMessageType messageType)
 {
     if (messageType == ApolloMessageType.CONNECTION_KEEP_ALIVE)
     {
         return("ka");
     }
     else
     {
         return(messageType.ToString().ToLowerInvariant());
     }
 }
        private static void AssertApolloResponse(
            this MockClientConnection connection,
            ApolloMessageType type,
            string id,
            bool compareId,
            string expectedPayloadJson,
            bool compareJson,
            bool dequeue = true)
        {
            if (connection.ResponseMessageCount == 0)
            {
                Assert.Fail("No messages queued.");
            }

            var message = dequeue ? connection.DequeueNextReceivedMessage() : connection.PeekNextReceivedMessage();
            var str     = Encoding.UTF8.GetString(message.Data);

            var options = new JsonSerializerOptions();

            options.PropertyNameCaseInsensitive = true;
            options.AllowTrailingCommas         = true;
            options.Converters.Add(new ApolloResponseMessageConverter());

            var convertedMessage = System.Text.Json.JsonSerializer.Deserialize <ApolloResponseMessage>(str, options);

            Assert.IsNotNull(convertedMessage, "Could not deserialized response message");
            Assert.AreEqual(type, convertedMessage.Type, $"Expected message type of {type.ToString()} but got {convertedMessage.Type.ToString()}");

            if (compareJson)
            {
                if (expectedPayloadJson == null)
                {
                    Assert.IsNull(convertedMessage.Payload);
                }
                else
                {
                    CommonAssertions.AreEqualJsonStrings(expectedPayloadJson, convertedMessage.Payload);
                }
            }

            if (compareId)
            {
                Assert.AreEqual(id, convertedMessage.Id);
            }
        }