public void TestPostProcessOnSession() { var sessionId = "my first session"; var h = JsonRpc.Handler.GetSessionHandler(sessionId); PostProcessHandlerLocal postHandler = new PostProcessHandlerLocal(false); h.SetPostProcessHandler(new PostProcessHandler(postHandler.PostProcess)); 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, postHandler.run); h.Destroy(); var result2 = JsonRpcProcessor.Process(sessionId, request); result2.Wait(); Assert.AreEqual(1, postHandler.run); Assert.AreEqual(JObject.Parse(expectedResultAfterDestroy), JObject.Parse(result2.Result)); }
public void TestPostProcessorThrowsExceptionChangesReturn() { try { PostProcessHandlerLocal handler = new PostProcessHandlerLocal(true); Config.SetPostProcessHandler(new PostProcessHandler(handler.PostProcess)); string request = @"{method:'TestPostProcessorThrowsException',params:{inputValue:'some string'},id:1}"; string expectedResult = "{\"jsonrpc\":\"2.0\",\"error\":{\"message\":\"Test error\",\"code\":-123,\"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.NotNull(handler.response, "response should not be null"); Assert.Null(handler.response.Result, "Result should be null"); Assert.NotNull(handler.response.Error, "Error should not be null"); Assert.AreEqual(-32603, handler.response.Error.code, "Error code mismatch"); Assert.Null(handler.context, "Context should be null"); } finally { Config.SetPostProcessHandler(null); } }
public void TestPostProcessorThrowsException() { try { PostProcessHandlerLocal handler = new PostProcessHandlerLocal(false); Config.SetPostProcessHandler(new PostProcessHandler(handler.PostProcess)); string request = @"{method:'TestPostProcessorThrowsException',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.NotNull(handler.response, "response should not be null"); Assert.Null(handler.response.Result, "Result should be null"); Assert.NotNull(handler.response.Error, "Error should not be null"); Assert.AreEqual(-32603, handler.response.Error.code, "Error code mismatch"); Assert.Null(handler.context, "Context should be null"); } finally { Config.SetPostProcessHandler(null); } }
public void TestPostProcessorSetsException() { try { PostProcessHandlerLocal handler = new PostProcessHandlerLocal(false); Config.SetPostProcessHandler(new PostProcessHandler(handler.PostProcess)); string request = @"{method:'TestPostProcessorSetsException',params:{inputValue:'some string'},id:1}"; string expectedResult = "{\"jsonrpc\":\"2.0\",\"error\":{\"code\":-27001,\"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); } }
public void TestPostProcessor() { try { PostProcessHandlerLocal handler = new PostProcessHandlerLocal(false); Config.SetPostProcessHandler(new PostProcessHandler(handler.PostProcess)); string request = @"{method:'TestPostProcessor',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, "Expect number of times run 1"); Assert.NotNull(handler.rpc, "RPC should not be null"); Assert.NotNull(handler.response, "response should not be null"); Assert.AreEqual("Success!", (string)handler.response.Result); Assert.Null(handler.context, "Context should be null"); } finally { Config.SetPostProcessHandler(null); } }