示例#1
0
            public void Returns_Correct_Result_For_Valid_Request()
            {
                // Given
                var handler = CreateRequestHandler(new JsonRpcServiceFake());
                var request = new JsonRpcRequest { Id = 1, MethodName = "test" };

                // When
                var result = (JsonRpcSuccessResponse) handler.Handle(request);

                // Then
                Assert.Equal(42, result.Result);
            }
示例#2
0
            public void Returns_Method_Not_Found_Error_For_Nonexistent_Method_Name()
            {
                // Given
                var request = new JsonRpcRequest { Id = 1, MethodName = "nonexistent" };
                var handler = CreateRequestHandler(new JsonRpcServiceFake());

                // When
                var response = (JsonRpcErrorResponse) handler.Handle(request);

                // Then
                Assert.Equal("Method not found", response.Error.Message);
            }
示例#3
0
            public void Returns_Invalid_Params_Error_For_String_As_Parameter()
            {
                // Given
                var request = new JsonRpcRequest { Id = 1, MethodName = "test", Parameters = "invalid" };
                var handler = CreateRequestHandler(new JsonRpcServiceFake());

                // When
                var response = (JsonRpcErrorResponse) handler.Handle(request);

                // Then
                Assert.Equal("Invalid params", response.Error.Message);
            }
示例#4
0
        public JsonRpcRequest Parse(string json)
        {
            var request = _serializer.DeserializeObject<IDictionary<string, object>>(json);

            // Validate protocol version
            if (!request.ContainsKey("jsonrpc")
                || request["jsonrpc"].GetType() != typeof (string)
                || request["jsonrpc"].ToString() != "2.0")
            {
                throw new InvalidRequestException("Invalid protocol version.");
            }

            // Validate method
            if (!request.ContainsKey("method")
                || request["method"].GetType() != typeof (string))
            {
                throw new InvalidRequestException("Invalid method name.");
            }

            // Validate id
            if (!(request.ContainsKey("id")
                  && request["id"] != null)
                || !(request["id"] is int
                     || request["id"] is long
                     || request["id"] is string))
            {
                throw new InvalidRequestException("Invalid id.");
            }

            var rpcRequest =  new JsonRpcRequest
            {
                Id = request["id"],
                MethodName = request["method"].ToString(),
                ProtocolVersion = request["jsonrpc"].ToString()
            };

            if (request.ContainsKey("params"))
            {
                rpcRequest.Parameters = request["params"];
            }

            return rpcRequest;
        }
示例#5
0
        public JsonRpcResponse Handle(JsonRpcRequest request)
        {
            try
            {
                // Get method
                var method = _methodCache.Get(request.MethodName);
                if (method == null)
                {
                    return new JsonRpcErrorResponse(request.Id, -32601, "Method not found");
                }

                // Find resolver
                var resolver = _parameterResolvers.FirstOrDefault(r => r.CanResolve(request.Parameters));
                if (resolver == null)
                {
                    return new JsonRpcErrorResponse(request.Id, -32602, "Invalid params");
                }

                // Resolve parameters
                var parameters = resolver.Resolve(request.Parameters, method.Parameters);

                // Execute method with resolved parameters
                var result = method.Execute(parameters);

                // Return response
                return new JsonRpcSuccessResponse(request.Id, result);
            }
            catch (Exception exception)
            {
                if (exception.InnerException is JsonRpcException)
                {
                    var inner = exception.InnerException as JsonRpcException;

                    var message = "Internal error";
                    if (!string.IsNullOrEmpty(inner.Message)) message = inner.Message;

                    return new JsonRpcErrorResponse(request.Id, inner.ErrorCode, message, inner.ToString());
                }

                return new JsonRpcErrorResponse(request.Id, -32603, "Internal error", exception.ToString());
            }
        }
示例#6
0
            public void Returns_Correct_Result_For_Positional_Array_And_Object_Parameters()
            {
                // Given
                var handler = CreateRequestHandler(new JsonRpcServiceFake());
                var request = new JsonRpcRequest
                {
                    Id = 1,
                    MethodName = "test.array+object",
                    Parameters = new object[] { new[] { 1, 1, 3 }, new TestDto() }
                };

                // When
                var result = (JsonRpcSuccessResponse)handler.Handle(request);

                // Then
                Assert.True((bool)result.Result);
            }
示例#7
0
            public void Returns_Correct_Error_Code_For_Method_Throwing_JsonRpcException()
            {
                // Given
                var request = new JsonRpcRequest
                {
                    Id = 1,
                    MethodName = "test.throw"
                };
                var handler = CreateRequestHandler(new JsonRpcServiceFake());

                // When
                var result = (JsonRpcErrorResponse)handler.Handle(request);

                // Then
                Assert.Equal(1000, result.Error.Code);
            }
示例#8
0
            public void Returns_Correct_Enum_For_Dto_With_Enum_Property()
            {
                // Given
                var request = new JsonRpcRequest
                {
                    Id = 1,
                    MethodName = "test.objectEnum",
                    Parameters = new[] { new TestDto { Enum = TestEnum.Two } }
                };
                var handler = CreateRequestHandler(new JsonRpcServiceFake());

                // When
                var result = (JsonRpcSuccessResponse)handler.Handle(request);

                // Then
                Assert.Equal(TestEnum.Two, (TestEnum)result.Result);
            }
示例#9
0
            public void Returns_Correct_Result_For_Method_Returning_Void_Given_Positional_Parameters()
            {
                // Given
                var request = new JsonRpcRequest
                {
                    Id = 1,
                    MethodName = "test.void",
                    Parameters = new[] { 1, 2 }
                };
                var handler = CreateRequestHandler(new JsonRpcServiceFake());

                // When
                var result = (JsonRpcSuccessResponse)handler.Handle(request);

                // Then
                Assert.Null(result.Result);
            }
示例#10
0
            public void Returns_Correct_Result_For_Method_Returning_Void_Given_Named_Parameters()
            {
                // Given
                var request = new JsonRpcRequest
                {
                    Id = 1,
                    MethodName = "test.void",
                    Parameters = new Dictionary<string, object>
                    {
                        {"i", 1},
                        {"j", 2}
                    }
                };
                var handler = CreateRequestHandler(new JsonRpcServiceFake());

                // When
                var result = (JsonRpcSuccessResponse)handler.Handle(request);

                // Then
                Assert.Null(result.Result);
            }
示例#11
0
            public void Returns_Correct_Result_For_Method_Accepting_No_Arguments()
            {
                // Given
                var request = new JsonRpcRequest
                {
                    Id = 1,
                    MethodName = "test.noargs"
                };
                var handler = CreateRequestHandler(new JsonRpcServiceFake());

                // When
                var result = (JsonRpcSuccessResponse)handler.Handle(request);

                // Then
                Assert.True((bool)result.Result);
            }
示例#12
0
            public void Returns_Correct_Result_For_Named_Array_Parameter()
            {
                // Given
                var request = new JsonRpcRequest
                {
                    Id = 1,
                    MethodName = "test.array",
                    Parameters = new Dictionary<string, object>
                    {
                        {"arr", new[] {"1", "2", "3", "4"}}
                    }
                };
                var handler = CreateRequestHandler(new JsonRpcServiceFake());

                // When
                var result = (JsonRpcSuccessResponse)handler.Handle(request);

                // Then
                Assert.Equal(4, ((string[])result.Result).Length);
            }
示例#13
0
            public void Returns_Correct_Result_For_Named_String_And_Boolean_Parameters()
            {
                // Given
                var request = new JsonRpcRequest
                {
                    Id = 1,
                    MethodName = "test.string+bool",
                    Parameters = new Dictionary<string, object>
                    {
                        {"str", "test"},
                        {"b", true}
                    }
                };
                var handler = CreateRequestHandler(new JsonRpcServiceFake());

                // When
                var result = (JsonRpcSuccessResponse)handler.Handle(request);

                // Then
                Assert.True((bool)result.Result);
            }
示例#14
0
            public void Returns_Correct_Result_For_Positional_String_And_Boolean_Parameters()
            {
                // Given
                var request = new JsonRpcRequest
                {
                    Id = 1,
                    MethodName = "test.string+bool",
                    Parameters = new object[] { "test", false }
                };
                var handler = CreateRequestHandler(new JsonRpcServiceFake());

                // When
                var result = (JsonRpcSuccessResponse)handler.Handle(request);

                // Then
                Assert.True((bool)result.Result);
            }
示例#15
0
            public void Returns_Correct_Result_For_Named_Array_And_Object_Parameters()
            {
                // Given
                var handler = CreateRequestHandler(new JsonRpcServiceFake());
                var request = new JsonRpcRequest
                {
                    Id = 1,
                    MethodName = "test.array+object",
                    Parameters = new Dictionary<string, object>
                    {
                        {"array", new[] {1, 2, 3}},
                        {"obj", new TestDto()}
                    }
                };

                // When
                var result = (JsonRpcSuccessResponse)handler.Handle(request);

                // Then
                Assert.True((bool)result.Result);
            }