Example #1
0
        public void TestPreProcessOnSession()
        {
            var sessionId = "my session";
            var h = JsonRpc.Handler.GetSessionHandler(sessionId);
            PreProcessHandlerLocal preHandler = new PreProcessHandlerLocal();
            h.SetPreProcessHandler(new PreProcessHandler(preHandler.PreProcess));

            h.Register("workie", new Func<string, string>(x => "workie ... " + x));

            var metadata = new System.Collections.Generic.List<Tuple<string, Type>> {
                Tuple.Create ("sooper", typeof(string)),
                Tuple.Create ("returns", typeof(string))
            }.ToDictionary(x => x.Item1, x => x.Item2);
            h.MetaData.AddService("workie", metadata, new System.Collections.Generic.Dictionary<string, object>());

            string request = @"{method:'workie',params:{'sooper':'good'},id:1}";
            string expectedResult = "{\"jsonrpc\":\"2.0\",\"result\":\"workie ... good\",\"id\":1}";
            string expectedResultAfterDestroy = "{\"jsonrpc\":\"2.0\",\"error\":{\"message\":\"Method not found\",\"code\":-32601,\"data\":\"The method does not exist / is not available.\"},\"id\":1}";
            var result = JsonRpcProcessor.Process(sessionId, request);
            result.Wait();

            var actual1 = JObject.Parse(result.Result);
            var expected1 = JObject.Parse(expectedResult);
            Assert.AreEqual(expected1, actual1);
            Assert.AreEqual(1, preHandler.run);

            h.Destroy();

            var result2 = JsonRpcProcessor.Process(sessionId, request);
            result2.Wait();

            Assert.AreEqual(1, preHandler.run);
            Assert.AreEqual(JObject.Parse(expectedResultAfterDestroy), JObject.Parse(result2.Result));
        }
Example #2
0
 public void TestPreProcessorThrowsException()
 {
     try
     {
         PreProcessHandlerLocal handler = new PreProcessHandlerLocal();
         Config.SetPreProcessHandler(new PreProcessHandler(handler.PreProcess));
         string request = @"{method:'TestPreProcessorThrowsException',params:{inputValue:'some string'},id:1}";
         var result = JsonRpcProcessor.Process(request);
         result.Wait();
         StringAssert.Contains("-32603", result.Result);
         Assert.AreEqual(1, handler.run);
         Assert.NotNull(handler.rpc, "RPC should not be null");
         Assert.Null(handler.context, "Context should be null");
     }
     finally
     {
         Config.SetPreProcessHandler(null);
     }
 }
Example #3
0
 public void TestPreProcessorSetsException()
 {
     try
     {
         PreProcessHandlerLocal handler = new PreProcessHandlerLocal();
         Config.SetPreProcessHandler(new PreProcessHandler(handler.PreProcess));
         string request = @"{method:'TestPreProcessorSetsException',params:{inputValue:'some string'},id:1}";
         string expectedResult = "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-27000,\"message\":\"This exception was thrown using: JsonRpcContext.SetException()\",\"data\":null},\"id\":1}";
         var result = JsonRpcProcessor.Process(request);
         result.Wait();
         AssertJsonAreEqual(expectedResult, result.Result);
         Assert.AreEqual(1, handler.run);
         Assert.NotNull(handler.rpc, "RPC should not be null");
         Assert.Null(handler.context, "Context should be null");
     }
     finally
     {
         Config.SetPreProcessHandler(null);
     }
 }
Example #4
0
        public void TestPreProcessor()
        {
            try {
                PreProcessHandlerLocal handler = new PreProcessHandlerLocal();
                Config.SetPreProcessHandler(new PreProcessHandler(handler.PreProcess));
                string request = @"{method:'TestPreProcessor',params:{inputValue:'some string'},id:1}";
                string expectedResult = "{\"jsonrpc\":\"2.0\",\"result\":\"Success!\",\"id\":1}";
                var result = JsonRpcProcessor.Process(request);
                result.Wait();
                AssertJsonAreEqual(expectedResult, result.Result);
                Assert.AreEqual(1, handler.run);
                Assert.NotNull(handler.rpc, "RPC should not be null");
                Assert.Null(handler.context, "Context should be null");
            } finally {
                Config.SetPreProcessHandler(null);
            }

        }