public async Task Call_Async_Methods_With_Parameters() { var mock1 = Substitute.For <IMockClass>(); mock1.Method4Async(1, 2).Returns(Guid.NewGuid().ToString()); var mock2 = Substitute.For <IMockClass>(); mock2.Method4Async(3, 4).Returns(Guid.NewGuid().ToString()); var mock3 = Substitute.For <IMockClass>(); mock3.Method4Async(1, 2).Returns(Task <string> .Factory.StartNew(() => throw new Exception())); var mock4 = Substitute.For <IMockClass>(); mock4.Method4Async(3, 4).Returns(Task <string> .Factory.StartNew(() => throw new Exception())); var result1 = await AlwaysResult.For <IMockClass>(mock1).InvokeAsync(c => c.Method4Async(1, 2)); var result2 = await AlwaysResult.For <IMockClass>(mock2).InvokeAsync(c => c.Method4Async(3, 4)); var result3 = await AlwaysResult.For <IMockClass>(mock3).InvokeAsync(c => c.Method4Async(1, 2)); var result4 = await AlwaysResult.For <IMockClass>(mock4).InvokeAsync(c => c.Method4Async(3, 4)); Assert.Equal(result1, result3); Assert.Equal(result2, result4); }
/// <summary> /// 验证 /// </summary> /// <param name="context"></param> /// <param name="next"></param> /// <returns></returns> public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { Stopwatch sw = new Stopwatch(); sw.Start(); if (!GlobalContext.SystemConfig.Debug) { string token = context.HttpContext.Request.Headers[GlobalContext.SystemConfig.TokenName].ParseToString(); OperatorModel user = OperatorProvider.Provider.GetCurrent(); var description = (Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)context.ActionDescriptor; var methodanonymous = (AuthorizeAttribute)description.MethodInfo.GetCustomAttribute(typeof(AuthorizeAttribute)); if (user == null || methodanonymous == null) { AlwaysResult obj = new AlwaysResult(); obj.message = "抱歉,没有操作权限"; obj.state = ResultType.error.ToString(); context.Result = new JsonResult(obj); return; } _authorize = methodanonymous._authorize; if (!AuthorizeCheck(user.RoleId)) { AlwaysResult obj = new AlwaysResult(); obj.message = "抱歉,没有操作权限"; obj.state = ResultType.error.ToString(); context.Result = new JsonResult(obj); return; } } var resultContext = await next(); sw.Stop(); }
public void Call_Method_Without_Parameters_And_Get_Result() { var mock = Substitute.For <IMockClass>(); mock.Method1().Returns(Guid.NewGuid().ToString()); var result1 = AlwaysResult.For <IMockClass>(mock).Invoke(m => m.Method1()); mock.Method1().Returns(x => { throw new Exception(); }); var result2 = AlwaysResult.For <IMockClass>(mock).Invoke(m => m.Method1()); Assert.Equal(result1, result2); }
public async Task Call_Async_Methods_Without_Parameters() { var mock1 = Substitute.For <IMockClass>(); mock1.Method3Async().Returns(Guid.NewGuid().ToString()); var mock2 = Substitute.For <IMockClass>(); mock2.Method3Async().Returns(Task <string> .Factory.StartNew(() => throw new Exception())); var result1Task = AlwaysResult.For <IMockClass>(mock1).InvokeAsync(c => c.Method3Async()); var result2Task = AlwaysResult.For <IMockClass>(mock2).InvokeAsync(c => c.Method3Async()); Assert.Equal(await result1Task, await result2Task); }
public void Call_Method_With_ExceptionHandler() { var mock = Substitute.For <IMockClass>(); mock.Method2("test", 1).Returns("test"); AlwaysResult.For <IMockClass>(mock).Invoke(c => c.Method2("test", 1)); mock.Method2("test", 1).Returns(x => { throw new Exception(); }); AlwaysResult.For <IMockClass>(mock) .WithExceptionHandler((ex) => mock.Method1()) .Invoke(c => c.Method2("test", 1)); mock.Received().Method1(); }
public async Task Call_Async_Method_With_ExceptionHandler() { var mock = Substitute.For <IMockClass>(); var mock1 = Substitute.For <IMockClass>(); mock1.Method3Async().Returns("test"); var mock2 = Substitute.For <IMockClass>(); mock2.Method3Async().Returns(Task <string> .Factory.StartNew(() => throw new Exception())); await AlwaysResult.For <IMockClass>(mock1).InvokeAsync(c => c.Method3Async()); await AlwaysResult.For <IMockClass>(mock2) .WithExceptionHandler((ex) => mock.Method1()) .InvokeAsync(c => c.Method3Async()); mock.Received().Method1(); }
public void OnException(ExceptionContext context) { LogHelper.WriteWithTime(context); if (context.HttpContext.Request.IsAjaxRequest()) { AlwaysResult obj = new AlwaysResult(); obj.state = ResultType.error.ToString(); obj.message = context.Exception.GetOriginalException().Message; if (string.IsNullOrEmpty(obj.message)) { obj.message = "抱歉,系统错误,请联系管理员!"; } context.Result = new JsonResult(obj); context.ExceptionHandled = true; } else { //context.HttpContext.Response.WriteAsync("<script>top.location.href ='" + context.HttpContext.Request.PathBase + "/Home/Error?msg=500" + "';if(document.all) window.event.returnValue = false;</script>"); context.Result = new RedirectResult(context.HttpContext.Request.PathBase + "/Home/Error?msg=500"); context.ExceptionHandled = true; } }
public async Task <AlwaysResult> Start() { AlwaysResult obj = new AlwaysResult(); try { ServerStateEntity entity = new ServerStateEntity(); var computer = ComputerHelper.GetComputerInfo(); entity.F_ARM = computer.RAMRate; entity.F_CPU = computer.CPURate; entity.F_IIS = "0"; entity.F_WebSite = _hostingEnvironment.ContentRootPath; await _server.SubmitForm(entity); obj.state = ResultType.success.ToString(); obj.message = "服务器状态更新成功!"; } catch (Exception ex) { obj.state = ResultType.error.ToString(); obj.message = "服务器状态更新失败!" + ex.Message; } return(obj); }
public void Call_Method_With_Parameter_And_Get_Result() { var paramStr1 = "test1"; var paramStr2 = "test2"; var paramInt1 = 1; var paramInt2 = 2; var mock = Substitute.For <IMockClass>(); mock.Method2(paramStr1, paramInt1).Returns($"{Guid.NewGuid()}_{paramStr1}_{paramInt1}"); var result11 = AlwaysResult.For <IMockClass>(mock).Invoke(c => c.Method2(paramStr1, paramInt1)); mock.Method2(paramStr2, paramInt2).Returns($"{Guid.NewGuid()}_{paramStr2}_{paramInt2}"); var result12 = AlwaysResult.For <IMockClass>(mock).Invoke(c => c.Method2(paramStr2, paramInt2)); mock.Method2(paramStr1, paramInt1).Returns(x => { throw new Exception(); }); var result21 = AlwaysResult.For <IMockClass>(mock).Invoke(c => c.Method2(paramStr1, paramInt1)); mock.Method2(paramStr2, paramInt2).Returns(x => { throw new Exception(); }); var result22 = AlwaysResult.For <IMockClass>(mock).Invoke(c => c.Method2(paramStr2, paramInt2)); Assert.Equal(result11, result21); Assert.Equal(result12, result22); }
/// <summary> /// 验证 /// </summary> /// <param name="context"></param> /// <param name="next"></param> /// <returns></returns> public override async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next) { Stopwatch sw = new Stopwatch(); sw.Start(); if (!GlobalContext.SystemConfig.Debug) { string token = context.HttpContext.Request.Headers[GlobalContext.SystemConfig.TokenName].ParseToString(); OperatorModel user = OperatorProvider.Provider.GetCurrent(); var description = (Microsoft.AspNetCore.Mvc.Controllers.ControllerActionDescriptor)context.ActionDescriptor; //添加有允许匿名的Action,可以不用登录访问,如Login/Index //控制器整体忽略或者单独方法忽略 var anonymous = description.ControllerTypeInfo.GetCustomAttribute(typeof(AllowAnonymousAttribute)); var methodanonymous = description.MethodInfo.GetCustomAttribute(typeof(AllowAnonymousAttribute)); if (user != null) { //延长过期时间 int LoginExpire = GlobalContext.SystemConfig.LoginExpire; string cacheKeyOperator = "HaotianCloud_operator_";// +登录者token await CacheHelper.Expire(cacheKeyOperator + token, LoginExpire); await CacheHelper.Expire(cacheKeyOperator + "api_" + user.UserId, LoginExpire); // 根据传入的Token,添加token和客户参数 if (context.ActionArguments != null && context.ActionArguments.Count > 0) { PropertyInfo property = context.ActionArguments.FirstOrDefault().Value.GetType().GetProperty("Token"); if (property != null) { property.SetValue(context.ActionArguments.FirstOrDefault().Value, token, null); } switch (context.HttpContext.Request.Method.ToUpper()) { case "GET": break; case "POST": property = context.ActionArguments.FirstOrDefault().Value.GetType().GetProperty("CustomerId"); if (property != null) { property.SetValue(context.ActionArguments.FirstOrDefault().Value, user.UserId, null); } break; } } } else if (anonymous == null && methodanonymous == null) { AlwaysResult obj = new AlwaysResult(); obj.message = "抱歉,没有操作权限"; obj.state = ResultType.error.ToString(); context.Result = new JsonResult(obj); return; } } var resultContext = await next(); sw.Stop(); }