public async Task InvokeRequest_ComplexParam_TwoRequests_NotCached() { string methodName = nameof(TestRouteClass.ComplexParam); DefaultRpcInvoker invoker = this.GetInvoker(methodName); async Task Test(TestComplexParam param) { var rpcParameter = JsonBytesRpcParameter.FromRaw(param); RpcRequest stringRequest = new RpcRequest("1", methodName, parameters: new RpcParameters(rpcParameter)); RpcResponse?response = await invoker.InvokeRequestAsync(stringRequest); RpcResponse resultResponse = Assert.IsType <RpcResponse>(response); Assert.False(resultResponse.HasError); Assert.Equal(param, resultResponse.Result); } TestComplexParam param1 = new TestComplexParam { A = "Test", B = 5 }; await Test(param1); TestComplexParam param2 = new TestComplexParam { A = "Test2", B = 6 }; await Test(param2); }
public void TryGetValue_Number_NumberParsed() { JsonBytesRpcParameter param = this.BuildParam("1"); bool parsed = param.TryGetValue(out int?actual); Assert.True(parsed); Assert.Equal(1, actual); parsed = param.TryGetValue(out long actual2); Assert.True(parsed); Assert.Equal(1L, actual2); parsed = param.TryGetValue(out short actual3); Assert.True(parsed); Assert.Equal(1, actual3); parsed = param.TryGetValue(out float actual4); Assert.True(parsed); Assert.Equal(1f, actual4); parsed = param.TryGetValue(out double actual5); Assert.True(parsed); Assert.Equal(1d, actual5); parsed = param.TryGetValue(out decimal actual6); Assert.True(parsed); Assert.Equal(1m, actual6); }
public async Task InvokeRequest_MultipleDictionaryValues_Valid() { string methodName = nameof(TestRouteClass.AllTypes); DefaultRpcInvoker invoker = this.GetInvoker(methodName); bool a = true; string bb = "Test"; object?ccc = null; var dddd = new TestComplexParam(); int eeeee = 1; var paramDict = new Dictionary <string, IRpcParameter> { ["a"] = JsonBytesRpcParameter.FromRaw(a), ["bb"] = JsonBytesRpcParameter.FromRaw(bb), ["ccc"] = JsonBytesRpcParameter.FromRaw(ccc), ["dddd"] = JsonBytesRpcParameter.FromRaw(dddd), ["eeeee"] = JsonBytesRpcParameter.FromRaw(eeeee) }; RpcRequest stringRequest = new RpcRequest("1", methodName, parameters: new RpcParameters(paramDict)); RpcResponse?response = await invoker.InvokeRequestAsync(stringRequest); RpcResponse resultResponse = Assert.IsType <RpcResponse>(response); Assert.False(resultResponse.HasError); var value = Assert.IsType <ValueTuple <bool, string, object, TestComplexParam, int> >(resultResponse.Result); Assert.Equal((a, bb, ccc, dddd, eeeee), value); }
public async Task InvokeRequest_WithAudit() { Guid randomGuid = Guid.NewGuid(); var parameters = new RpcParameters(JsonBytesRpcParameter.FromRaw(randomGuid.ToString())); string methodName = nameof(TestRouteClass.GuidTypeMethod); var stringRequest = new RpcRequest("1", methodName, parameters); int startCalledCount = 0; int endCalledCount = 0; object data = new object(); DefaultRpcInvoker invoker = this.GetInvoker(methodName, configure: (config) => { config.OnInvokeStart = (context) => { startCalledCount++; context.CustomContextData = data; }; config.OnInvokeEnd = (context, response) => { endCalledCount++; Assert.Same(data, context.CustomContextData); }; }); RpcResponse?stringResponse = await invoker.InvokeRequestAsync(stringRequest); Assert.Equal(1, startCalledCount); Assert.Equal(1, endCalledCount); }
public void TryGetValue_String_StringParsed() { const string expected = "Test"; JsonBytesRpcParameter param = this.BuildParam($"\"{expected}\""); bool parsed = param.TryGetValue(out string?actual); Assert.True(parsed); Assert.Equal(expected, actual); }
public async Task InvokeRequest_MethodNotFound_ErrorResponse() { var parameters = new RpcParameters(JsonBytesRpcParameter.FromRaw(1)); var stringRequest = new RpcRequest("1", "MethodNotFound", parameters); DefaultRpcInvoker invoker = this.GetInvoker(methodInfo: null); RpcResponse? response = await invoker.InvokeRequestAsync(stringRequest); Assert.NotNull(response); Assert.NotNull(response !.Error); Assert.Equal((int)RpcErrorCode.MethodNotFound, response.Error !.Code); }
public void TryGetValue_Array_ArrayParsed() { string json = $"[0,1,2,3,4]"; JsonBytesRpcParameter param = this.BuildParam(json); bool parsed = param.TryGetValue(out List <int> actual); Assert.True(parsed); for (int i = 0; i < actual.Count; i++) { Assert.Equal(i, actual[i]); } }
public async Task InvokeRequest_StringParam_ParseAsGuidType() { Guid randomGuid = Guid.NewGuid(); var parameters = new RpcParameters(JsonBytesRpcParameter.FromRaw(randomGuid.ToString())); string methodName = nameof(TestRouteClass.GuidTypeMethod); var stringRequest = new RpcRequest("1", methodName, parameters); DefaultRpcInvoker invoker = this.GetInvoker(methodName); RpcResponse? stringResponse = await invoker.InvokeRequestAsync(stringRequest); Assert.NotNull(stringResponse); Assert.Equal(randomGuid, stringResponse !.Result); }
public async Task InvokeRequest_BoolParam_Valid() { string methodName = nameof(TestRouteClass.BoolParameter); DefaultRpcInvoker invoker = this.GetInvoker(methodName); bool expectedValue = true; var param = JsonBytesRpcParameter.FromRaw(expectedValue); RpcRequest stringRequest = new RpcRequest("1", methodName, parameters: new RpcParameters(param)); RpcResponse?response = await invoker.InvokeRequestAsync(stringRequest); RpcResponse resultResponse = Assert.IsType <RpcResponse>(response); Assert.False(resultResponse.HasError); Assert.Equal(expectedValue, resultResponse.Result); }
public async Task InvokeRequest_Int64RequestParam_ConvertToInt32Param() { var parameters = new RpcParameters(JsonBytesRpcParameter.FromRaw(1L)); string methodName = nameof(TestRouteClass.IntParameter); var stringRequest = new RpcRequest("1", methodName, parameters); DefaultRpcInvoker invoker = this.GetInvoker(methodName); RpcResponse?response = await invoker.InvokeRequestAsync(stringRequest); RpcResponse resultResponse = Assert.IsType <RpcResponse>(response); Assert.NotNull(resultResponse.Result); Assert.Equal(1, resultResponse.Result); }
public async Task InvokeRequest_AsyncMethod_Valid() { var parameters = new RpcParameters(JsonBytesRpcParameter.FromRaw(1), JsonBytesRpcParameter.FromRaw(1)); string methodName = nameof(TestRouteClass.AddAsync); var stringRequest = new RpcRequest("1", methodName, parameters); DefaultRpcInvoker invoker = this.GetInvoker(methodName); RpcResponse?response = await invoker.InvokeRequestAsync(stringRequest); RpcResponse resultResponse = Assert.IsType <RpcResponse>(response); Assert.NotNull(resultResponse.Result); Assert.Equal(2, resultResponse.Result); }
public void TryGetValue_Object_ObjectParsed() { const string expectedDateTime = "2019-01-01T00:00:00.000"; const decimal expectedDecimal = 1.1m; const int expectedInteger = 1; const string expectedString = "Test"; string json = $"{{\"DateTime\": \"{expectedDateTime}\", \"String\": \"{expectedString}\", \"Integer\": {expectedInteger}, \"Decimal\": {expectedDecimal}}}"; JsonBytesRpcParameter param = this.BuildParam(json); bool parsed = param.TryGetValue(out TestObject actual); Assert.True(parsed); Assert.Equal(DateTime.Parse(expectedDateTime), actual.DateTime); Assert.Equal(expectedDecimal, actual.Decimal); Assert.Equal(expectedInteger, actual.Integer); Assert.Equal(expectedString, actual.String); }
public void TryGetValue_DateTime_DateTimeParsed() { const string expected = "2019-01-01T00:00:00.000"; JsonBytesRpcParameter param = this.BuildParam($"\"{expected}\""); bool parsed = param.TryGetValue(out DateTime actual); Assert.True(parsed); Assert.Equal(DateTime.Parse(expected), actual); parsed = param.TryGetValue(out DateTimeOffset actual2); Assert.True(parsed); Assert.Equal(DateTimeOffset.Parse(expected), actual2); parsed = param.TryGetValue(out string actual3); Assert.True(parsed); Assert.Equal(expected, actual3); }
public async Task InvokeRequest_ListParam_Valid() { string methodName = nameof(TestRouteClass.ListParam); DefaultRpcInvoker invoker = this.GetInvoker(methodName); var param = new TestComplexParam[] { new TestComplexParam { A = "Test", B = 5 } }; var rpcParameter = JsonBytesRpcParameter.FromRaw(param); RpcRequest stringRequest = new RpcRequest("1", methodName, parameters: new RpcParameters(rpcParameter)); RpcResponse?response = await invoker.InvokeRequestAsync(stringRequest); RpcResponse resultResponse = Assert.IsType <RpcResponse>(response); Assert.False(resultResponse.HasError); Assert.Equal(param, resultResponse.Result); }
public async Task InvokeRequest_OptionalParameter_Valid() { string methodName = nameof(TestRouteClass.Optional); DefaultRpcInvoker invoker = this.GetInvoker(methodName); //No params specified RpcRequest stringRequest = new RpcRequest("1", methodName, parameters: null); RpcResponse?response = await invoker.InvokeRequestAsync(stringRequest); RpcResponse resultResponse = Assert.IsType <RpcResponse>(response); Assert.Null(resultResponse.Result); Assert.False(resultResponse.HasError, resultResponse.Error?.Message); //Param is empty var parameters = new RpcParameters(new IRpcParameter[0]); stringRequest = new RpcRequest("1", methodName, parameters: parameters); response = await invoker.InvokeRequestAsync(stringRequest); resultResponse = Assert.IsType <RpcResponse>(response); Assert.Null(resultResponse.Result); Assert.False(resultResponse.HasError); //Param is a string const string value = "Test"; parameters = new RpcParameters(new IRpcParameter[] { JsonBytesRpcParameter.FromRaw(value) }); stringRequest = new RpcRequest("1", methodName, parameters: parameters); response = await invoker.InvokeRequestAsync(stringRequest); resultResponse = Assert.IsType <RpcResponse>(response); Assert.NotNull(resultResponse.Result); Assert.IsType <string>(resultResponse.Result); Assert.Equal(value, (string)resultResponse.Result !); }