示例#1
0
        public void CreateResponse()
        {
            // If: I create a response
            var message = Message.CreateResponse(CommonObjects.MessageId, CommonObjects.TestMessageContents.DefaultInstance);

            // Then: Message and JObject should have appropriate properties set
            var expectedResults = new MessagePropertyResults
            {
                MessageType   = MessageType.Response,
                IdSet         = true,
                MethodSetAs   = null,
                ContentsSetAs = "result",
                ErrorSet      = false
            };

            AssertPropertiesSet(expectedResults, message);
        }
示例#2
0
        public void CreateEvent()
        {
            // If: I create an event
            var message = Message.CreateEvent(CommonObjects.EventType, CommonObjects.TestMessageContents.DefaultInstance);

            // Then: Message and JObject should have appropriate properties set
            var expectedResults = new MessagePropertyResults
            {
                MessageType   = MessageType.Event,
                IdSet         = false,
                MethodSetAs   = CommonObjects.EventType.MethodName,
                ContentsSetAs = "params",
                ErrorSet      = false
            };

            AssertPropertiesSet(expectedResults, message);
        }
示例#3
0
        public void CreateRequest()
        {
            // If: I create a request
            var message = Message.CreateRequest(CommonObjects.RequestType, CommonObjects.MessageId, CommonObjects.TestMessageContents.DefaultInstance);

            // Then:
            // ... The message should have all the properties I defined
            // ... The JObject should have the same properties
            var expectedResults = new MessagePropertyResults
            {
                MessageType   = MessageType.Request,
                IdSet         = true,
                MethodSetAs   = CommonObjects.RequestType.MethodName,
                ContentsSetAs = "params",
                ErrorSet      = false
            };

            AssertPropertiesSet(expectedResults, message);
        }
示例#4
0
        private static void AssertPropertiesSet(MessagePropertyResults results, Message message)
        {
            Assert.NotNull(message);

            // Serialize the message and deserialize back into a JObject
            string messageJson = message.Serialize();

            Assert.NotNull(messageJson);
            JObject jObject = JObject.Parse(messageJson);

            // JSON RPC Version
            List <string> expectedProperties = new List <string> {
                "jsonrpc"
            };

            Assert.Equal("2.0", jObject["jsonrpc"]);

            // Message Type
            Assert.Equal(results.MessageType, message.MessageType);

            // ID
            if (results.IdSet)
            {
                Assert.Equal(CommonObjects.MessageId, message.Id);
                Assert.Equal(CommonObjects.MessageId, jObject["id"]);
                expectedProperties.Add("id");
            }
            else
            {
                Assert.Null(message.Id);
            }

            // Method
            if (results.MethodSetAs != null)
            {
                Assert.Equal(results.MethodSetAs, message.Method);
                Assert.Equal(results.MethodSetAs, jObject["method"]);
                expectedProperties.Add("method");
            }
            else
            {
                Assert.Null(message.Method);
            }

            // Contents
            if (results.ContentsSetAs != null)
            {
                Assert.Equal(CommonObjects.TestMessageContents.SerializedContents, message.Contents);
                Assert.Equal(CommonObjects.TestMessageContents.SerializedContents, jObject[results.ContentsSetAs]);
                expectedProperties.Add(results.ContentsSetAs);
            }

            // Error
            if (results.ErrorSet)
            {
                Assert.Equal(CommonObjects.TestErrorContents.SerializedContents, message.Contents);
                Assert.Equal(CommonObjects.TestErrorContents.SerializedContents, jObject["error"]);
                expectedProperties.Add("error");
            }

            // Look for any extra properties set in the JObject
            IEnumerable <string> setProperties = jObject.Properties().Select(p => p.Name);

            Assert.Empty(setProperties.Except(expectedProperties));
        }