Example #1
0
        public void ReturnStringArray()
        {
            Response <object> reply = default;

            var handler    = new TestHandlerFake();
            var server     = new JsonRpcServer();
            var clientMock = new Mock <JsonRpcServer.ClientConnection>(1, "localhost", _fakePipe, _process, Encoding.UTF8);

            clientMock.Setup(x => x.WriteAsJson(It.IsAny <object>())).Callback <object>(o => reply = (Response <object>)o);

            var reqOring = new Request()
            {
                Id = 3, Method = "ReturnStringArray"
            };
            var json = Serializer.Serialize(reqOring);
            var req  = Serializer.Deserialize <Request>(json);

            server.Bind(handler);
            server.ExecuteHandler(clientMock.Object, req.Id, req.Method, req.Params);

            reply.Should().NotBeNull();
            reply.Id.Should().Be(3);
            reply.Error.Should().BeNull();
            reply.Result.Should().BeEquivalentTo(new string[] { "one", "two", "three" });
        }
Example #2
0
        public void CanInvokeDelegate_WithArgs_ReturnValue()
        {
            Response <object> reply = default;

            var fakePipe = new StreamDuplexPipe(PipeOptions.Default, new MemoryStream());
            var server   = new JsonRpcServer();

            var clientMock = new Mock <JsonRpcServer.ClientConnection>(1, "localhost", fakePipe, _process, Encoding.UTF8);

            clientMock.Setup(x => x.WriteAsJson(It.IsAny <object>())).Callback <object>(o => reply = (Response <object>)o);

            bool called = false;

            server.Bind("DelegateFunction", (Func <int, string, string>)((a, b) =>
            {
                called = b == "TestString";
                return(b);
            }));
            server.ExecuteHandler(clientMock.Object, 21, "DelegateFunction", new object[] { 176, "TestString" });

            called.Should().BeTrue();
            reply.Should().NotBeNull();
            reply.Error.Should().BeNull();
            reply.Id.Should().Be(21);
            reply.Result.Should().NotBeNull();
            reply.Result.Should().Be("TestString");
        }
Example #3
0
        public void Call_GivenDateTime()
        {
            Response reply = default;

            var handler    = new TestHandlerFake();
            var server     = new JsonRpcServer();
            var clientMock = new Mock <JsonRpcServer.ClientConnection>(1, "localhost", _fakePipe, _process, Encoding.UTF8);

            clientMock.Setup(x => x.WriteAsJson(It.IsAny <object>())).Callback <object>(o => reply = (Response)o);

            var now      = DateTime.Now;
            var reqOring = new Request()
            {
                Id = 81, Method = "DateTimeTest", Params = new object[] { now }
            };
            var json = Serializer.Serialize(reqOring);
            var req  = Serializer.Deserialize <Request>(json);

            server.Bind(handler);
            server.ExecuteHandler(clientMock.Object, req.Id, req.Method, req.Params);

            reply.Should().NotBeNull();
            reply.Id.Should().Be(81);
            reply.Error.Should().BeNull();
        }
Example #4
0
        public void NoReturnOnNotify()
        {
            string reply = null;

            var handler    = new TestHandlerFake();
            var server     = new JsonRpcServer();
            var clientMock = new Mock <JsonRpcServer.ClientConnection>(1, "localhost", _fakePipe, _process, Encoding.UTF8);

            clientMock.Setup(x => x.WriteString(It.IsAny <string>())).Callback <string>(s => reply = s);

            server.Bind(handler);
            server.ExecuteHandler(clientMock.Object, -1, "ReturnStringArray", null);

            reply.Should().BeNull();
        }
Example #5
0
        public void Call_WithDefaultArgsValues()
        {
            Response reply = default;

            var handler    = new TestHandlerFake();
            var server     = new JsonRpcServer();
            var clientMock = new Mock <JsonRpcServer.ClientConnection>(1, "localhost", _fakePipe, _process, Encoding.UTF8);

            clientMock.Setup(x => x.WriteAsJson(It.IsAny <object>())).Callback <object>(o => reply = (Response)o);

            server.Bind(handler);
            server.ExecuteHandler(clientMock.Object, 31, "DefaultArgs", new object[] { 123 });

            reply.Should().NotBeNull();
            reply.Id.Should().Be(31);
            reply.Error.Should().BeNull();
        }
Example #6
0
        public void Call_GivenTypesArgs()
        {
            Response <object> reply = default;

            var handler    = new TestHandlerFake();
            var server     = new JsonRpcServer();
            var clientMock = new Mock <JsonRpcServer.ClientConnection>(1, "localhost", _fakePipe, _process, Encoding.UTF8);

            clientMock.Setup(x => x.WriteAsJson(It.IsAny <object>())).Callback <object>(o => reply = (Response <object>)o);

            server.Bind(handler);
            server.ExecuteHandler(clientMock.Object, 1, "FirstTest", new object[] { 1, "string", false, null });

            reply.Should().NotBeNull();
            reply.Id.Should().Be(1);
            reply.Result.Should().Be(23);
        }
Example #7
0
        public void Call_StaticFunction()
        {
            Response reply = default;

            var fakePipe = new StreamDuplexPipe(PipeOptions.Default, new MemoryStream());
            var server   = new JsonRpcServer();

            var clientMock = new Mock <JsonRpcServer.ClientConnection>(1, "localhost", fakePipe, _process, Encoding.UTF8);

            clientMock.Setup(x => x.WriteAsJson(It.IsAny <object>())).Callback <object>(o => reply = (Response)o);

            server.Bind(typeof(StaticHandler));
            server.ExecuteHandler(clientMock.Object, 43, "Function1", null);

            reply.Should().NotBeNull();
            reply.Error.Should().BeNull();
        }
Example #8
0
        public void CanInvokeDelegate()
        {
            Response reply = default;

            var fakePipe = new StreamDuplexPipe(PipeOptions.Default, new MemoryStream());
            var server   = new JsonRpcServer();

            var clientMock = new Mock <JsonRpcServer.ClientConnection>(1, "localhost", fakePipe, _process, Encoding.UTF8);

            clientMock.Setup(x => x.WriteAsJson(It.IsAny <object>())).Callback <object>(o => reply = (Response)o);

            bool called = false;

            server.Bind("DelegateFunction", (Action)(() => { called = true; }));
            server.ExecuteHandler(clientMock.Object, 43, "DelegateFunction", null);

            called.Should().BeTrue();
            reply.Should().NotBeNull();
            reply.Error.Should().BeNull();
            reply.Id.Should().Be(43);
        }
Example #9
0
        public void Call_GivenIntStringBoolNull()
        {
            Response <object> reply = default;

            var handler    = new TestHandlerFake();
            var server     = new JsonRpcServer();
            var clientMock = new Mock <JsonRpcServer.ClientConnection>(1, "localhost", _fakePipe, _process, Encoding.UTF8);

            clientMock.Setup(x => x.WriteAsJson(It.IsAny <object>())).Callback <object>(o => reply = (Response <object>)o);

            var reqOring = new Request()
            {
                Id = 1, Method = "FirstTest", Params = new object[] { 1, "string", false, null }
            };
            var json = Serializer.Serialize(reqOring);
            var req  = Serializer.Deserialize <Request>(json);

            server.Bind(handler);
            server.ExecuteHandler(clientMock.Object, req.Id, req.Method, req.Params);

            reply.Should().NotBeNull();
            reply.Id.Should().Be(1);
            reply.Result.Should().Be(23);
        }