public void InvokeActionMethodFilterAsynchronously_NormalExecutionNotCanceled()
        {
            // Arrange
            bool nextInChainWasCalled       = false;
            bool onActionExecutingWasCalled = false;
            bool onActionExecutedWasCalled  = false;

            ActionExecutingContext preContext   = GetActionExecutingContext();
            ActionFilterImpl       actionFilter = new ActionFilterImpl()
            {
                OnActionExecutingImpl = _ => { onActionExecutingWasCalled = true; },
                OnActionExecutedImpl  = _ => { onActionExecutedWasCalled = true; }
            };

            // Act
            Func <ActionExecutedContext> continuation = AsyncControllerActionInvoker.InvokeActionMethodFilterAsynchronously(actionFilter, preContext,
                                                                                                                            () => {
                nextInChainWasCalled = true;
                return(() => new ActionExecutedContext());
            });

            // Assert
            Assert.IsTrue(nextInChainWasCalled);
            Assert.IsTrue(onActionExecutingWasCalled, "OnActionExecuting() should've been called by the first invocation.");
            Assert.IsFalse(onActionExecutedWasCalled, "OnActionExecuted() shouldn't have been called by the first invocation.");

            continuation();
            Assert.IsTrue(onActionExecutedWasCalled, "OnActionExecuted() should've been called by the second invocation.");
        }
        public void InvokeActionMethodFilterAsynchronously_NextInChainThrowsOnActionExecutedException_Handled()
        {
            // Arrange
            ViewResult expectedResult = new ViewResult();

            bool nextInChainWasCalled      = false;
            bool onActionExecutedWasCalled = false;

            ActionExecutingContext preContext   = GetActionExecutingContext();
            ActionFilterImpl       actionFilter = new ActionFilterImpl()
            {
                OnActionExecutedImpl = filterContext => {
                    onActionExecutedWasCalled = true;
                    Assert.IsNotNull(filterContext.Exception);
                    filterContext.ExceptionHandled = true;
                    filterContext.Result           = expectedResult;
                }
            };

            // Act & assert pre-execution
            Func <ActionExecutedContext> continuation = AsyncControllerActionInvoker.InvokeActionMethodFilterAsynchronously(actionFilter, preContext,
                                                                                                                            () => () => {
                nextInChainWasCalled = true;
                throw new Exception("Some exception text.");
            });

            Assert.IsFalse(onActionExecutedWasCalled, "OnActionExecuted() shouldn't have been called yet.");

            // Act & assert post-execution
            ActionExecutedContext postContext = continuation();

            Assert.IsTrue(nextInChainWasCalled, "Next in chain should've been called.");
            Assert.IsTrue(onActionExecutedWasCalled, "OnActionExecuted() should've been called.");
            Assert.AreEqual(expectedResult, postContext.Result);
        }
        public void InvokeActionMethodFilterAsynchronously_NextInChainThrowsOnActionExecutingException_NotHandled()
        {
            // Arrange
            ViewResult expectedResult = new ViewResult();

            bool nextInChainWasCalled       = false;
            bool onActionExecutingWasCalled = false;
            bool onActionExecutedWasCalled  = false;

            ActionExecutingContext preContext   = GetActionExecutingContext();
            ActionFilterImpl       actionFilter = new ActionFilterImpl()
            {
                OnActionExecutingImpl = filterContext => { onActionExecutingWasCalled = true; },
                OnActionExecutedImpl  = filterContext => { onActionExecutedWasCalled = true; }
            };

            // Act & assert
            Assert.Throws <Exception>(
                delegate
            {
                AsyncControllerActionInvoker.InvokeActionMethodFilterAsynchronously(
                    actionFilter, preContext,
                    () =>
                {
                    nextInChainWasCalled = true;
                    throw new Exception("Some exception text.");
                });
            },
                @"Some exception text.");

            // Assert
            Assert.True(nextInChainWasCalled);
            Assert.True(onActionExecutingWasCalled);
            Assert.True(onActionExecutedWasCalled);
        }
        public void InvokeActionMethodFilterAsynchronously_NextInChainThrowsOnActionExecutedException_ThreadAbort()
        {
            // Arrange
            ViewResult expectedResult = new ViewResult();

            bool onActionExecutedWasCalled = false;

            ActionExecutingContext preContext   = GetActionExecutingContext();
            ActionFilterImpl       actionFilter = new ActionFilterImpl()
            {
                OnActionExecutedImpl = filterContext => {
                    onActionExecutedWasCalled = true;
                    Thread.ResetAbort();
                }
            };

            // Act & assert
            Func <ActionExecutedContext> continuation = AsyncControllerActionInvoker.InvokeActionMethodFilterAsynchronously(actionFilter, preContext,
                                                                                                                            () => () => {
                Thread.CurrentThread.Abort();
                return(null);
            });

            ExceptionHelper.ExpectException <ThreadAbortException>(
                delegate {
                continuation();
            });

            // Assert
            Assert.IsTrue(onActionExecutedWasCalled, "OnActionExecuted() should've been called.");
        }
        public void InvokeAction_AsyncControllerActionInvoker()
        {
            _controllerContext.Controller = new TestController();
            var actionInvoker = new AsyncControllerActionInvoker();

            IAsyncResult asyncResult = actionInvoker.BeginInvokeAction(_controllerContext, nameof(TestController.NormalAction), null, null);
            var          retVal      = actionInvoker.EndInvokeAction(asyncResult);
        }
        public void InvokeAction_ThrowsIfControllerContextIsNull()
        {
            // Arrange
            AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker();

            // Act & assert
            Assert.ThrowsArgumentNull(
                delegate { invoker.BeginInvokeAction(null, "someAction", null, null); }, "controllerContext");
        }
        public void InvokeAction_ThrowsIfActionNameIsNull()
        {
            // Arrange
            AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker();

            // Act & assert
            Assert.ThrowsArgumentNullOrEmpty(
                delegate { invoker.BeginInvokeAction(new ControllerContext(), null, null, null); }, "actionName");
        }
        public void InvokeAction_ActionThrowsException_ThreadAbort()
        {
            // Arrange
            ControllerContext controllerContext = GetControllerContext();
            AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker();

            // Act & assert
            Assert.Throws<ThreadAbortException>(
                delegate { invoker.BeginInvokeAction(controllerContext, "ActionCallsThreadAbort", null, null); });
        }
        public void InvokeAction_RequestValidationFails()
        {
            // Arrange
            ControllerContext            controllerContext = GetControllerContext(passesRequestValidation: false);
            AsyncControllerActionInvoker invoker           = new AsyncControllerActionInvoker();

            // Act & assert
            Assert.Throws <HttpRequestValidationException>(
                delegate { invoker.BeginInvokeAction(controllerContext, "NormalAction", null, null); });
        }
        public void InvokeAction_ActionThrowsException_ThreadAbort()
        {
            // Arrange
            ControllerContext            controllerContext = GetControllerContext();
            AsyncControllerActionInvoker invoker           = new AsyncControllerActionInvoker();

            // Act & assert
            Assert.Throws <ThreadAbortException>(
                delegate { invoker.BeginInvokeAction(controllerContext, "ActionCallsThreadAbort", null, null); });
        }
        public void InvokeAction_ThrowsIfActionNameIsEmpty()
        {
            // Arrange
            AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker();

            // Act & assert
            ExceptionHelper.ExpectArgumentExceptionNullOrEmpty(
                delegate {
                invoker.BeginInvokeAction(new ControllerContext(), "", null, null);
            }, "actionName");
        }
        public void InvokeAction_ActionThrowsException_NotHandled()
        {
            // Arrange
            ControllerContext            controllerContext = GetControllerContext();
            AsyncControllerActionInvoker invoker           = new AsyncControllerActionInvoker();

            // Act & assert
            Assert.Throws <Exception>(
                delegate { invoker.BeginInvokeAction(controllerContext, "ActionThrowsExceptionAndIsNotHandled", null, null); },
                @"Some exception text.");
        }
Exemplo n.º 13
0
        private ActionResult BeingInvokeActionMethodWithFiltersTester(Func <IAsyncResult> beginFunction, Func <ActionResult> endFunction, bool checkBegin, bool checkEnd, IActionFilter[] filters)
        {
            AsyncControllerActionInvoker invoker              = new AsyncControllerActionInvoker();
            ControllerContext            controllerContext    = new ControllerContext();
            Dictionary <string, object>  parameters           = new Dictionary <string, object>();
            Mock <AsyncActionDescriptor> mockActionDescriptor = new Mock <AsyncActionDescriptor>();
            bool endExecuteCalled          = false;
            bool beginExecuteCalled        = false;
            Func <ActionResult> endExecute = () =>
            {
                endExecuteCalled = true;
                return(endFunction());
            };
            Func <IAsyncResult> beingExecute = () =>
            {
                beginExecuteCalled = true;
                return(beginFunction());
            };

            mockActionDescriptor.Setup(d => d.BeginExecute(controllerContext, parameters, It.IsAny <AsyncCallback>(), It.IsAny <object>())).Returns(beingExecute);
            mockActionDescriptor.Setup(d => d.EndExecute(It.IsAny <IAsyncResult>())).Returns(endExecute);

            IAsyncResult outerAsyncResult = null;

            try
            {
                outerAsyncResult = invoker.BeginInvokeActionMethodWithFilters(controllerContext, filters, mockActionDescriptor.Object, parameters, null, null);
            }
            catch (Exception ex)
            {
                if (checkEnd)
                {
                    // Testing end, so not expecting exception thrown from begin
                    Assert.NotNull(ex);
                }
                else
                {
                    throw ex;
                }
            }

            Assert.NotNull(outerAsyncResult);
            Assert.Equal(checkBegin, beginExecuteCalled);
            Assert.False(endExecuteCalled);

            ActionExecutedContext postContext = invoker.EndInvokeActionMethodWithFilters(outerAsyncResult);

            Assert.NotNull(postContext);
            if (checkEnd)
            {
                Assert.True(endExecuteCalled);
            }
            return(postContext.Result);
        }
        public void InvokeAction_ActionThrowsException_NotHandled()
        {
            // Arrange
            ControllerContext controllerContext = GetControllerContext();
            AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker();

            // Act & assert
            Assert.Throws<Exception>(
                delegate { invoker.BeginInvokeAction(controllerContext, "ActionThrowsExceptionAndIsNotHandled", null, null); },
                @"Some exception text.");
        }
Exemplo n.º 15
0
        private static IEnumerable <ReflectedAsyncControllerDescriptor> GetControllerDescriptors(IEnumerable <Type> controllerTypes)
        {
            Contract.Assert(controllerTypes != null);

            Func <Type, ControllerDescriptor> descriptorFactory = ReflectedAsyncControllerDescriptor.DefaultDescriptorFactory;
            ControllerDescriptorCache         descriptorsCache  = new AsyncControllerActionInvoker().DescriptorCache;

            return
                (controllerTypes
                 .Select(type => descriptorsCache.GetDescriptor(type, descriptorFactory, type))
                 .Cast <ReflectedAsyncControllerDescriptor>());
        }
        public void InvokeAction_ActionNotFound() {
            // Arrange
            ControllerContext controllerContext = GetControllerContext();
            AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker();

            // Act
            IAsyncResult asyncResult = invoker.BeginInvokeAction(controllerContext, "ActionNotFound", null, null);
            bool retVal = invoker.EndInvokeAction(asyncResult);

            // Assert
            Assert.IsFalse(retVal);
        }
        public void InvokeAction_RequestValidationFails()
        {
            // Arrange
            ControllerContext            controllerContext = GetControllerContext(false /* passesRequestValidation */);
            AsyncControllerActionInvoker invoker           = new AsyncControllerActionInvoker();

            // Act & assert
            ExceptionHelper.ExpectException <HttpRequestValidationException>(
                delegate {
                invoker.BeginInvokeAction(controllerContext, "NormalAction", null, null);
            });
        }
        public void InvokeAction_ActionNotFound()
        {
            // Arrange
            ControllerContext            controllerContext = GetControllerContext();
            AsyncControllerActionInvoker invoker           = new AsyncControllerActionInvoker();

            // Act
            IAsyncResult asyncResult = invoker.BeginInvokeAction(controllerContext, "ActionNotFound", null, null);
            bool         retVal      = invoker.EndInvokeAction(asyncResult);

            // Assert
            Assert.IsFalse(retVal);
        }
        public void InvokeAction_ResultThrowsException_Handled()
        {
            // Arrange
            ControllerContext            controllerContext = GetControllerContext();
            AsyncControllerActionInvoker invoker           = new AsyncControllerActionInvoker();

            // Act & assert
            IAsyncResult asyncResult = invoker.BeginInvokeAction(controllerContext, "ResultThrowsExceptionAndIsHandled", null, null);
            bool         retVal      = invoker.EndInvokeAction(asyncResult);

            Assert.IsTrue(retVal);
            Assert.AreEqual("From exception filter", ((TestController)controllerContext.Controller).Log);
        }
        public void InvokeAction_ActionThrowsException_Handled() {
            // Arrange
            ControllerContext controllerContext = GetControllerContext();
            AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker();

            // Act & assert
            IAsyncResult asyncResult = invoker.BeginInvokeAction(controllerContext, "ActionThrowsExceptionAndIsHandled", null, null);
            Assert.IsNull(((TestController)controllerContext.Controller).Log, "Result filter shouldn't have executed yet.");

            bool retVal = invoker.EndInvokeAction(asyncResult);
            Assert.IsTrue(retVal);
            Assert.AreEqual("From exception filter", ((TestController)controllerContext.Controller).Log);
        }
        public void InvokeAction_AuthorizationFilterShortCircuits() {
            // Arrange
            ControllerContext controllerContext = GetControllerContext();
            AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker();

            // Act
            IAsyncResult asyncResult = invoker.BeginInvokeAction(controllerContext, "AuthorizationFilterShortCircuits", null, null);
            bool retVal = invoker.EndInvokeAction(asyncResult);

            // Assert
            Assert.IsTrue(retVal);
            Assert.AreEqual("From authorization filter", ((TestController)controllerContext.Controller).Log);
        }
        public void InvokeAction_ResultThrowsException_ThreadAbort()
        {
            // Arrange
            ControllerContext            controllerContext = GetControllerContext();
            AsyncControllerActionInvoker invoker           = new AsyncControllerActionInvoker();

            // Act & assert
            IAsyncResult asyncResult = invoker.BeginInvokeAction(controllerContext, "ResultCallsThreadAbort", null, null);

            ExceptionHelper.ExpectException <ThreadAbortException>(
                delegate {
                invoker.EndInvokeAction(asyncResult);
            });
        }
        public void InvokeAction_NormalAction()
        {
            // Arrange
            ControllerContext            controllerContext = GetControllerContext();
            AsyncControllerActionInvoker invoker           = new AsyncControllerActionInvoker();

            // Act
            IAsyncResult asyncResult = invoker.BeginInvokeAction(controllerContext, "NormalAction", null, null);
            bool         retVal      = invoker.EndInvokeAction(asyncResult);

            // Assert
            Assert.IsTrue(retVal);
            Assert.AreEqual("From action", ((TestController)controllerContext.Controller).Log);
        }
        public void InvokeAction_AuthorizationFilterShortCircuits()
        {
            // Arrange
            ControllerContext            controllerContext = GetControllerContext();
            AsyncControllerActionInvoker invoker           = new AsyncControllerActionInvoker();

            // Act
            IAsyncResult asyncResult = invoker.BeginInvokeAction(controllerContext, "AuthorizationFilterShortCircuits", null, null);
            bool         retVal      = invoker.EndInvokeAction(asyncResult);

            // Assert
            Assert.IsTrue(retVal);
            Assert.AreEqual("From authorization filter", ((TestController)controllerContext.Controller).Log);
        }
        internal void AddRouteEntries(SubRouteCollection collector, IEnumerable <Type> controllerTypes)
        {
            ControllerDescriptorCache descriptorsCache = new AsyncControllerActionInvoker().DescriptorCache;
            IEnumerable <ReflectedAsyncControllerDescriptor> descriptors = controllerTypes
                                                                           .Select(
                type =>
                descriptorsCache.GetDescriptor(type, innerType => new ReflectedAsyncControllerDescriptor(innerType), type))
                                                                           .Cast <ReflectedAsyncControllerDescriptor>();

            foreach (ReflectedAsyncControllerDescriptor controllerDescriptor in descriptors)
            {
                AddRouteEntries(collector, controllerDescriptor);
            }
        }
        public void InvokeAction_ResultThrowsException_NotHandled()
        {
            // Arrange
            ControllerContext            controllerContext = GetControllerContext();
            AsyncControllerActionInvoker invoker           = new AsyncControllerActionInvoker();

            // Act & assert
            IAsyncResult asyncResult = invoker.BeginInvokeAction(controllerContext, "ResultThrowsExceptionAndIsNotHandled", null, null);

            ExceptionHelper.ExpectException <Exception>(
                delegate {
                invoker.EndInvokeAction(asyncResult);
            },
                @"Some exception text.");
        }
        public void InvokeAction_ActionThrowsException_Handled()
        {
            // Arrange
            ControllerContext            controllerContext = GetControllerContext();
            AsyncControllerActionInvoker invoker           = new AsyncControllerActionInvoker();

            // Act & assert
            IAsyncResult asyncResult = invoker.BeginInvokeAction(controllerContext, "ActionThrowsExceptionAndIsHandled", null, null);

            Assert.Null(((TestController)controllerContext.Controller).Log); // Result filter shouldn't have executed yet

            bool retVal = invoker.EndInvokeAction(asyncResult);

            Assert.True(retVal);
            Assert.Equal("From exception filter", ((TestController)controllerContext.Controller).Log);
        }
        public void InvokeActionMethodWithFilters_ShortCircuited()
        {
            // Arrange
            List <string>               actionLog         = new List <string>();
            ControllerContext           controllerContext = new ControllerContext();
            Dictionary <string, object> parameters        = new Dictionary <string, object>();
            ActionResult actionResult = new ViewResult();

            ActionFilterImpl filter1 = new ActionFilterImpl()
            {
                OnActionExecutingImpl = delegate(ActionExecutingContext filterContext) {
                    actionLog.Add("OnActionExecuting1");
                },
                OnActionExecutedImpl = delegate(ActionExecutedContext filterContext) {
                    actionLog.Add("OnActionExecuted1");
                }
            };
            ActionFilterImpl filter2 = new ActionFilterImpl()
            {
                OnActionExecutingImpl = delegate(ActionExecutingContext filterContext) {
                    actionLog.Add("OnActionExecuting2");
                    filterContext.Result = actionResult;
                },
                OnActionExecutedImpl = delegate(ActionExecutedContext filterContext) {
                    actionLog.Add("OnActionExecuted2");
                }
            };

            Mock <AsyncActionDescriptor> mockActionDescriptor = new Mock <AsyncActionDescriptor>();

            mockActionDescriptor.Expect(d => d.BeginExecute(controllerContext, parameters, It.IsAny <AsyncCallback>(), It.IsAny <object>())).Throws(new Exception("I shouldn't have been called."));
            mockActionDescriptor.Expect(d => d.EndExecute(It.IsAny <IAsyncResult>())).Throws(new Exception("I shouldn't have been called."));

            AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker();

            IActionFilter[] filters = new IActionFilter[] { filter1, filter2 };

            // Act
            IAsyncResult          outerAsyncResult = invoker.BeginInvokeActionMethodWithFilters(controllerContext, filters, mockActionDescriptor.Object, parameters, null, null);
            ActionExecutedContext postContext      = invoker.EndInvokeActionMethodWithFilters(outerAsyncResult);

            // Assert
            CollectionAssert.AreEqual(
                new string[] { "OnActionExecuting1", "OnActionExecuting2", "OnActionExecuted1" },
                actionLog);
            Assert.AreEqual(actionResult, postContext.Result);
        }
        internal List <RouteEntry> MapMvcAttributeRoutes(IEnumerable <Type> controllerTypes)
        {
            ControllerDescriptorCache descriptorsCache = new AsyncControllerActionInvoker().DescriptorCache;
            IEnumerable <ReflectedAsyncControllerDescriptor> descriptors = controllerTypes
                                                                           .Select(
                type =>
                descriptorsCache.GetDescriptor(type, innerType => new ReflectedAsyncControllerDescriptor(innerType), type))
                                                                           .Cast <ReflectedAsyncControllerDescriptor>();

            List <RouteEntry> routeEntries = new List <RouteEntry>();

            foreach (ReflectedAsyncControllerDescriptor controllerDescriptor in descriptors)
            {
                routeEntries.AddRange(MapMvcAttributeRoutes(controllerDescriptor));
            }

            return(routeEntries);
        }
        public void InvokeActionMethod_SynchronousDescriptor()
        {
            // Arrange
            ControllerContext           controllerContext = new ControllerContext();
            Dictionary <string, object> parameters        = new Dictionary <string, object>();
            ActionResult expectedResult = new ViewResult();

            Mock <ActionDescriptor> mockActionDescriptor = new Mock <ActionDescriptor>();

            mockActionDescriptor.Expect(d => d.Execute(controllerContext, parameters)).Returns(expectedResult);

            AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker();

            // Act
            IAsyncResult asyncResult    = invoker.BeginInvokeActionMethod(controllerContext, mockActionDescriptor.Object, parameters, null, null);
            ActionResult returnedResult = invoker.EndInvokeActionMethod(asyncResult);

            // Assert
            Assert.AreEqual(expectedResult, returnedResult);
        }
        public void InvokeActionMethodFilterAsynchronously_NextInChainThrowsOnActionExecutingException_Handled()
        {
            // Arrange
            ViewResult expectedResult = new ViewResult();

            bool nextInChainWasCalled       = false;
            bool onActionExecutingWasCalled = false;
            bool onActionExecutedWasCalled  = false;

            ActionExecutingContext preContext   = GetActionExecutingContext();
            ActionFilterImpl       actionFilter = new ActionFilterImpl()
            {
                OnActionExecutingImpl = filterContext => { onActionExecutingWasCalled = true; },
                OnActionExecutedImpl  = filterContext =>
                {
                    onActionExecutedWasCalled = true;
                    Assert.NotNull(filterContext.Exception);
                    filterContext.ExceptionHandled = true;
                    filterContext.Result           = expectedResult;
                }
            };

            // Act
            Func <ActionExecutedContext> continuation = AsyncControllerActionInvoker.InvokeActionMethodFilterAsynchronously(
                actionFilter, preContext,
                () =>
            {
                nextInChainWasCalled = true;
                throw new Exception("Some exception text.");
            });

            // Assert
            Assert.True(nextInChainWasCalled);
            Assert.True(onActionExecutingWasCalled);
            Assert.True(onActionExecutedWasCalled);

            ActionExecutedContext postContext = continuation();

            Assert.Equal(expectedResult, postContext.Result);
        }
        public void InvokeActionMethodFilterAsynchronously_NextInChainThrowsOnActionExecutingException_ThreadAbort()
        {
            // Arrange
            ViewResult expectedResult = new ViewResult();

            bool nextInChainWasCalled       = false;
            bool onActionExecutingWasCalled = false;
            bool onActionExecutedWasCalled  = false;

            ActionExecutingContext preContext   = GetActionExecutingContext();
            ActionFilterImpl       actionFilter = new ActionFilterImpl()
            {
                OnActionExecutingImpl = filterContext => { onActionExecutingWasCalled = true; },
                OnActionExecutedImpl  = filterContext =>
                {
                    onActionExecutedWasCalled = true;
                    Thread.ResetAbort();
                }
            };

            // Act & assert
            Assert.Throws <ThreadAbortException>(
                delegate
            {
                AsyncControllerActionInvoker.InvokeActionMethodFilterAsynchronously(
                    actionFilter, preContext,
                    () =>
                {
                    nextInChainWasCalled = true;
                    Thread.CurrentThread.Abort();
                    return(null);
                });
            });

            // Assert
            Assert.True(nextInChainWasCalled);
            Assert.True(onActionExecutingWasCalled);
            Assert.True(onActionExecutedWasCalled);
        }
        public void InvokeActionMethod_AsynchronousDescriptor()
        {
            // Arrange
            ControllerContext           controllerContext = new ControllerContext();
            Dictionary <string, object> parameters        = new Dictionary <string, object>();
            IAsyncResult innerAsyncResult = new MockAsyncResult();
            ActionResult expectedResult   = new ViewResult();

            Mock <AsyncActionDescriptor> mockActionDescriptor = new Mock <AsyncActionDescriptor>();

            mockActionDescriptor.Setup(d => d.BeginExecute(controllerContext, parameters, It.IsAny <AsyncCallback>(), It.IsAny <object>())).Returns(innerAsyncResult);
            mockActionDescriptor.Setup(d => d.EndExecute(innerAsyncResult)).Returns(expectedResult);

            AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker();

            // Act
            IAsyncResult asyncResult    = invoker.BeginInvokeActionMethod(controllerContext, mockActionDescriptor.Object, parameters, null, null);
            ActionResult returnedResult = invoker.EndInvokeActionMethod(asyncResult);

            // Assert
            Assert.Equal(expectedResult, returnedResult);
        }
        public void InvokeActionMethodFilterAsynchronously_OnActionExecutingSetsResult()
        {
            // Arrange
            ViewResult expectedResult = new ViewResult();

            bool nextInChainWasCalled       = false;
            bool onActionExecutingWasCalled = false;
            bool onActionExecutedWasCalled  = false;

            ActionExecutingContext preContext   = GetActionExecutingContext();
            ActionFilterImpl       actionFilter = new ActionFilterImpl()
            {
                OnActionExecutingImpl = filterContext =>
                {
                    onActionExecutingWasCalled = true;
                    filterContext.Result       = expectedResult;
                },
                OnActionExecutedImpl = _ => { onActionExecutedWasCalled = true; }
            };

            // Act
            Func <ActionExecutedContext> continuation = AsyncControllerActionInvoker.InvokeActionMethodFilterAsynchronously(
                actionFilter, preContext,
                () =>
            {
                nextInChainWasCalled = true;
                return(() => new ActionExecutedContext());
            });

            // Assert
            Assert.False(nextInChainWasCalled);
            Assert.True(onActionExecutingWasCalled);
            Assert.False(onActionExecutedWasCalled);

            ActionExecutedContext postContext = continuation();

            Assert.False(onActionExecutedWasCalled);
            Assert.Equal(expectedResult, postContext.Result);
        }
        public void InvokeActionMethodFilterAsynchronously_NextInChainThrowsOnActionExecutingException_ThreadAbort()
        {
            // Arrange
            ViewResult expectedResult = new ViewResult();

            bool nextInChainWasCalled       = false;
            bool onActionExecutingWasCalled = false;
            bool onActionExecutedWasCalled  = false;

            ActionExecutingContext preContext   = GetActionExecutingContext();
            ActionFilterImpl       actionFilter = new ActionFilterImpl()
            {
                OnActionExecutingImpl = filterContext => {
                    onActionExecutingWasCalled = true;
                },
                OnActionExecutedImpl = filterContext => {
                    onActionExecutedWasCalled = true;
                    Thread.ResetAbort();
                }
            };

            // Act & assert
            ExceptionHelper.ExpectException <ThreadAbortException>(
                delegate {
                AsyncControllerActionInvoker.InvokeActionMethodFilterAsynchronously(actionFilter, preContext,
                                                                                    () => {
                    nextInChainWasCalled = true;
                    Thread.CurrentThread.Abort();
                    return(null);
                });
            });

            // Assert
            Assert.IsTrue(nextInChainWasCalled, "Next in chain should've been called.");
            Assert.IsTrue(onActionExecutingWasCalled, "OnActionExecuting() should've been called by the first invocation.");
            Assert.IsTrue(onActionExecutedWasCalled, "OnActionExecuted() should've been called due to exception handling.");
        }
        public void InvokeActionMethodWithFilters()
        {
            // Arrange
            List<string> actionLog = new List<string>();
            ControllerContext controllerContext = new ControllerContext();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            MockAsyncResult innerAsyncResult = new MockAsyncResult();
            ActionResult actionResult = new ViewResult();

            ActionFilterImpl filter1 = new ActionFilterImpl()
            {
                OnActionExecutingImpl = delegate(ActionExecutingContext filterContext) { actionLog.Add("OnActionExecuting1"); },
                OnActionExecutedImpl = delegate(ActionExecutedContext filterContext) { actionLog.Add("OnActionExecuted1"); }
            };
            ActionFilterImpl filter2 = new ActionFilterImpl()
            {
                OnActionExecutingImpl = delegate(ActionExecutingContext filterContext) { actionLog.Add("OnActionExecuting2"); },
                OnActionExecutedImpl = delegate(ActionExecutedContext filterContext) { actionLog.Add("OnActionExecuted2"); }
            };

            Mock<AsyncActionDescriptor> mockActionDescriptor = new Mock<AsyncActionDescriptor>();
            mockActionDescriptor.Setup(d => d.BeginExecute(controllerContext, parameters, It.IsAny<AsyncCallback>(), It.IsAny<object>())).Returns(innerAsyncResult);
            mockActionDescriptor.Setup(d => d.EndExecute(innerAsyncResult)).Returns(actionResult);

            AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker();
            IActionFilter[] filters = new IActionFilter[] { filter1, filter2 };

            // Act
            IAsyncResult outerAsyncResult = invoker.BeginInvokeActionMethodWithFilters(controllerContext, filters, mockActionDescriptor.Object, parameters, null, null);
            ActionExecutedContext postContext = invoker.EndInvokeActionMethodWithFilters(outerAsyncResult);

            // Assert
            Assert.Equal(new[] { "OnActionExecuting1", "OnActionExecuting2", "OnActionExecuted2", "OnActionExecuted1" }, actionLog.ToArray());
            Assert.Equal(actionResult, postContext.Result);
        }
        public void InvokeActionMethod_SynchronousDescriptor()
        {
            // Arrange
            ControllerContext controllerContext = new ControllerContext();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            ActionResult expectedResult = new ViewResult();

            Mock<ActionDescriptor> mockActionDescriptor = new Mock<ActionDescriptor>();
            mockActionDescriptor.Setup(d => d.Execute(controllerContext, parameters)).Returns(expectedResult);

            AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker();

            // Act
            IAsyncResult asyncResult = invoker.BeginInvokeActionMethod(controllerContext, mockActionDescriptor.Object, parameters, null, null);
            ActionResult returnedResult = invoker.EndInvokeActionMethod(asyncResult);

            // Assert
            Assert.Equal(expectedResult, returnedResult);
        }
        public void InvokeAction_ThrowsIfControllerContextIsNull()
        {
            // Arrange
            AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker();

            // Act & assert
            Assert.ThrowsArgumentNull(
                delegate { invoker.BeginInvokeAction(null, "someAction", null, null); }, "controllerContext");
        }
        public void InvokeAction_ThrowsIfActionNameIsNull()
        {
            // Arrange
            AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker();

            // Act & assert
            Assert.ThrowsArgumentNullOrEmpty(
                delegate { invoker.BeginInvokeAction(new ControllerContext(), null, null, null); }, "actionName");
        }
        public void InvokeAction_ResultThrowsException_Handled()
        {
            // Arrange
            ControllerContext controllerContext = GetControllerContext();
            AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker();

            // Act & assert
            IAsyncResult asyncResult = invoker.BeginInvokeAction(controllerContext, "ResultThrowsExceptionAndIsHandled", null, null);
            bool retVal = invoker.EndInvokeAction(asyncResult);

            Assert.True(retVal);
            Assert.Equal("From exception filter", ((TestController)controllerContext.Controller).Log);
        }
        public void InvokeAction_RequestValidationFails() {
            // Arrange
            ControllerContext controllerContext = GetControllerContext(false /* passesRequestValidation */);
            AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker();

            // Act & assert
            ExceptionHelper.ExpectException<HttpRequestValidationException>(
                delegate {
                    invoker.BeginInvokeAction(controllerContext, "NormalAction", null, null);
                });
        }
        public void InvokeAction_AuthenticationFilterChallenges()
        {
            // Arrange
            ControllerContext controllerContext = GetControllerContext();
            AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker();

            // Act
            IAsyncResult asyncResult = invoker.BeginInvokeAction(controllerContext, "AuthenticationFilterChallenges", null, null);
            bool retVal = invoker.EndInvokeAction(asyncResult);

            // Assert
            Assert.True(retVal);
            Assert.Equal("From authentication filter challenge", ((TestController)controllerContext.Controller).Log);
        }
        public void InvokeActionMethodWithFilters_ShortCircuited() {
            // Arrange
            List<string> actionLog = new List<string>();
            ControllerContext controllerContext = new ControllerContext();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            ActionResult actionResult = new ViewResult();

            ActionFilterImpl filter1 = new ActionFilterImpl() {
                OnActionExecutingImpl = delegate(ActionExecutingContext filterContext) {
                    actionLog.Add("OnActionExecuting1");
                },
                OnActionExecutedImpl = delegate(ActionExecutedContext filterContext) {
                    actionLog.Add("OnActionExecuted1");
                }
            };
            ActionFilterImpl filter2 = new ActionFilterImpl() {
                OnActionExecutingImpl = delegate(ActionExecutingContext filterContext) {
                    actionLog.Add("OnActionExecuting2");
                    filterContext.Result = actionResult;
                },
                OnActionExecutedImpl = delegate(ActionExecutedContext filterContext) {
                    actionLog.Add("OnActionExecuted2");
                }
            };

            Mock<AsyncActionDescriptor> mockActionDescriptor = new Mock<AsyncActionDescriptor>();
            mockActionDescriptor.Expect(d => d.BeginExecute(controllerContext, parameters, It.IsAny<AsyncCallback>(), It.IsAny<object>())).Throws(new Exception("I shouldn't have been called."));
            mockActionDescriptor.Expect(d => d.EndExecute(It.IsAny<IAsyncResult>())).Throws(new Exception("I shouldn't have been called."));

            AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker();
            IActionFilter[] filters = new IActionFilter[] { filter1, filter2 };

            // Act
            IAsyncResult outerAsyncResult = invoker.BeginInvokeActionMethodWithFilters(controllerContext, filters, mockActionDescriptor.Object, parameters, null, null);
            ActionExecutedContext postContext = invoker.EndInvokeActionMethodWithFilters(outerAsyncResult);

            // Assert
            CollectionAssert.AreEqual(
                new string[] { "OnActionExecuting1", "OnActionExecuting2", "OnActionExecuted1" },
                actionLog);
            Assert.AreEqual(actionResult, postContext.Result);
        }
        public void InvokeActionMethod_AsynchronousDescriptor() {
            // Arrange
            ControllerContext controllerContext = new ControllerContext();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            IAsyncResult innerAsyncResult = new MockAsyncResult();
            ActionResult expectedResult = new ViewResult();

            Mock<AsyncActionDescriptor> mockActionDescriptor = new Mock<AsyncActionDescriptor>();
            mockActionDescriptor.Expect(d => d.BeginExecute(controllerContext, parameters, It.IsAny<AsyncCallback>(), It.IsAny<object>())).Returns(innerAsyncResult);
            mockActionDescriptor.Expect(d => d.EndExecute(innerAsyncResult)).Returns(expectedResult);

            AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker();

            // Act
            IAsyncResult asyncResult = invoker.BeginInvokeActionMethod(controllerContext, mockActionDescriptor.Object, parameters, null, null);
            ActionResult returnedResult = invoker.EndInvokeActionMethod(asyncResult);

            // Assert
            Assert.AreEqual(expectedResult, returnedResult);
        }
        public void InvokeAction_ThrowsIfActionNameIsEmpty() {
            // Arrange
            AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker();

            // Act & assert
            ExceptionHelper.ExpectArgumentExceptionNullOrEmpty(
                delegate {
                    invoker.BeginInvokeAction(new ControllerContext(), "", null, null);
                }, "actionName");
        }
        public void InvokeAction_ResultThrowsException_ThreadAbort() {
            // Arrange
            ControllerContext controllerContext = GetControllerContext();
            AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker();

            // Act & assert
            IAsyncResult asyncResult = invoker.BeginInvokeAction(controllerContext, "ResultCallsThreadAbort", null, null);
            ExceptionHelper.ExpectException<ThreadAbortException>(
                delegate {
                    invoker.EndInvokeAction(asyncResult);
                });
        }
        private ActionResult BeingInvokeActionMethodWithFiltersTester(Func<IAsyncResult> beginFunction, Func<ActionResult> endFunction, bool checkBegin, bool checkEnd, IActionFilter[] filters)
        {
            AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker();
            ControllerContext controllerContext = new ControllerContext();
            Dictionary<string, object> parameters = new Dictionary<string, object>();
            Mock<AsyncActionDescriptor> mockActionDescriptor = new Mock<AsyncActionDescriptor>();
            bool endExecuteCalled = false;
            bool beginExecuteCalled = false;
            Func<ActionResult> endExecute = () =>
            {
                endExecuteCalled = true;
                return endFunction();
            };
            Func<IAsyncResult> beingExecute = () =>
            {
                beginExecuteCalled = true;
                return beginFunction();
            };

            mockActionDescriptor.Setup(d => d.BeginExecute(controllerContext, parameters, It.IsAny<AsyncCallback>(), It.IsAny<object>())).Returns(beingExecute);
            mockActionDescriptor.Setup(d => d.EndExecute(It.IsAny<IAsyncResult>())).Returns(endExecute);

            IAsyncResult outerAsyncResult = null;
            try
            {
                outerAsyncResult = invoker.BeginInvokeActionMethodWithFilters(controllerContext, filters, mockActionDescriptor.Object, parameters, null, null);
            }
            catch (Exception ex)
            {
                if (checkEnd)
                {
                    // Testing end, so not expecting exception thrown from begin
                    Assert.NotNull(ex);
                }
                else
                {
                    throw ex;
                }
            }

            Assert.NotNull(outerAsyncResult);
            Assert.Equal(checkBegin, beginExecuteCalled);
            Assert.False(endExecuteCalled);

            ActionExecutedContext postContext = invoker.EndInvokeActionMethodWithFilters(outerAsyncResult);

            Assert.NotNull(postContext);
            if (checkEnd)
            {
                Assert.True(endExecuteCalled);
            }
            return postContext.Result;
        }
        public void InvokeAction_RequestValidationFails()
        {
            // Arrange
            ControllerContext controllerContext = GetControllerContext(passesRequestValidation: false);
            AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker();

            // Act & assert
            Assert.Throws<HttpRequestValidationException>(
                delegate { invoker.BeginInvokeAction(controllerContext, "NormalAction", null, null); });
        }
        public void InvokeAction_NormalAction()
        {
            // Arrange
            ControllerContext controllerContext = GetControllerContext();
            AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker();

            // Act
            IAsyncResult asyncResult = invoker.BeginInvokeAction(controllerContext, "NormalAction", null, null);
            bool retVal = invoker.EndInvokeAction(asyncResult);

            // Assert
            Assert.True(retVal);
            Assert.Equal("From action", ((TestController)controllerContext.Controller).Log);
        }
        public void InvokeAction_ResultThrowsException_NotHandled() {
            // Arrange
            ControllerContext controllerContext = GetControllerContext();
            AsyncControllerActionInvoker invoker = new AsyncControllerActionInvoker();

            // Act & assert
            IAsyncResult asyncResult = invoker.BeginInvokeAction(controllerContext, "ResultThrowsExceptionAndIsNotHandled", null, null);
            ExceptionHelper.ExpectException<Exception>(
                delegate {
                    invoker.EndInvokeAction(asyncResult);
                },
                @"Some exception text.");
        }