/// <summary>
 /// Asserts that a response message from the server queued and that it is of the supplied type.
 /// </summary>
 /// <param name="connection">The connection.</param>
 /// <param name="type">The type of message to check for.</param>
 /// <param name="dequeue">if true, the message is removed from the queue.</param>
 internal static void AssertApolloResponse(
     this MockClientConnection connection,
     ApolloMessageType type,
     bool dequeue = true)
 {
     AssertApolloResponse(connection, type, null, false, null, false, dequeue);
 }
 /// <summary>
 /// Asserts that the next message on the received queue is a DATA message and that the payload
 /// matches the provided json string.
 /// </summary>
 /// <param name="connection">The connection.</param>
 /// <param name="type">The expected type of the message.</param>
 /// <param name="id">The expected identifier of the subscription that rendered the data.</param>
 /// <param name="expectedPayloadJson">The expected payload of the message, converted to a json string.</param>
 /// <param name="dequeue">if set to <c>true</c> if the message should be removed from the queue.</param>
 internal static void AssertApolloResponse(
     this MockClientConnection connection,
     ApolloMessageType type,
     string id,
     string expectedPayloadJson,
     bool dequeue = true)
 {
     AssertApolloResponse(connection, type, id, true, expectedPayloadJson, true, dequeue);
 }
示例#3
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);
            }
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="ApolloMessage{TPayload}"/> class.
 /// </summary>
 /// <param name="messageType">Type of the message.</param>
 protected ApolloMessage(ApolloMessageType messageType)
     : base(messageType)
 {
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="ApolloMessage" /> class.
 /// </summary>
 /// <param name="messageType">Type of the message.</param>
 protected ApolloMessage(ApolloMessageType messageType)
 {
     this.Type = messageType;
     this.Id   = null;
 }