public void FindAction_ReturnsMatchingMethodIfOneMethodMatches()
        {
            // Arrange
            Type controllerType = typeof(SelectionAttributeController);
            AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);

            // Act
            ActionDescriptorCreator creator = selector.FindAction(
                new ControllerContext(),
                "OneMatch"
                );
            ActionDescriptor actionDescriptor = creator(
                "someName",
                new Mock <ControllerDescriptor>().Object
                );

            // Assert
            var castActionDescriptor = Assert.IsType <ReflectedActionDescriptor>(actionDescriptor);

            Assert.Equal("OneMatch", castActionDescriptor.MethodInfo.Name);
            Assert.Equal(
                typeof(string),
                castActionDescriptor.MethodInfo.GetParameters()[0].ParameterType
                );
        }
예제 #2
0
        private static ActionDescriptor CreateActionDescriptor(ControllerDescriptor controller,
            AsyncActionMethodSelector actionSelector, MethodInfo method)
        {
            string actionName = actionSelector.GetActionName(method);
            ActionDescriptorCreator creator = actionSelector.GetActionDescriptorDelegate(method);
            Debug.Assert(creator != null);

            return creator(actionName, controller);
        }
예제 #3
0
        public void FindActionMethod_Asynchronous_ThrowsIfCompletionMethodNotFound()
        {
            // Arrange
            Type controllerType = typeof(MethodLocatorController);
            AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);

            // Act & assert
            Assert.Throws <InvalidOperationException>(
                delegate { ActionDescriptorCreator creator = selector.FindAction(new ControllerContext(), "EventPatternWithoutCompletionMethod"); },
                @"Could not locate a method named 'EventPatternWithoutCompletionMethodCompleted' on controller type System.Web.Mvc.Async.Test.AsyncActionMethodSelectorTest+MethodLocatorController.");
        }
예제 #4
0
        public void FindAction_ReturnsNullIfNoMethodMatches()
        {
            // Arrange
            Type controllerType = typeof(SelectionAttributeController);
            AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);

            // Act
            ActionDescriptorCreator creator = selector.FindAction(new ControllerContext(), "ZeroMatch");

            // Assert
            Assert.Null(creator);
        }
        public void FindAction_DoesNotMatchCompletedMethod()
        {
            // Arrange
            Type controllerType = typeof(MethodLocatorController);
            AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);

            // Act
            ActionDescriptorCreator creator = selector.FindAction(null, "EventPatternCompleted");

            // Assert
            Assert.IsNull(creator, "No method should have matched.");
        }
        public void FindAction_ReturnsNullIfNoMethodMatches()
        {
            // Arrange
            Type controllerType = typeof(SelectionAttributeController);
            AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);

            // Act
            ActionDescriptorCreator creator = selector.FindAction(null, "ZeroMatch");

            // Assert
            Assert.IsNull(creator, "No method should have matched.");
        }
        public void FindActionMethodDoesNotMatchBeginMethod()
        {
            // Arrange
            Type controllerType = typeof(MethodLocatorController);
            AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);

            // Act
            ActionDescriptorCreator creator = selector.FindActionMethod(null, "BeginAsyncResultPattern");

            // Assert
            Assert.IsNull(creator, "No method should have matched.");
        }
예제 #8
0
        public void FindAction_DoesNotMatchCompletedMethod()
        {
            // Arrange
            Type controllerType = typeof(MethodLocatorController);
            AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);

            // Act
            ActionDescriptorCreator creator = selector.FindAction(new ControllerContext(), "EventPatternCompleted");

            // Assert
            Assert.Null(creator);
        }
예제 #9
0
        public void FindActionMethod_Asynchronous_ThrowsIfMultipleCompletedMethodsMatched()
        {
            // Arrange
            Type controllerType = typeof(MethodLocatorController);
            AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);

            // Act & assert
            Assert.Throws <AmbiguousMatchException>(
                delegate { ActionDescriptorCreator creator = selector.FindAction(new ControllerContext(), "EventPatternAmbiguous"); },
                "Lookup for method 'EventPatternAmbiguousCompleted' on controller type 'MethodLocatorController' failed because of an ambiguity between the following methods:" + Environment.NewLine
                + "Void EventPatternAmbiguousCompleted(Int32) on type System.Web.Mvc.Async.Test.AsyncActionMethodSelectorTest+MethodLocatorController" + Environment.NewLine
                + "Void EventPatternAmbiguousCompleted(System.String) on type System.Web.Mvc.Async.Test.AsyncActionMethodSelectorTest+MethodLocatorController");
        }
예제 #10
0
        public override ActionDescriptor FindAction(ControllerContext context, string actionName)
        {
            Precondition.Require(context, () => Error.ArgumentNull("context"));
            Precondition.Defined(actionName, () => Error.ArgumentNull("actionName"));

            ActionDescriptorCreator creator = _selector.GetAsyncDescriptor(context, actionName);

            if (creator == null)
            {
                return(null);
            }

            return(creator(actionName, this));
        }
예제 #11
0
        public void FindActionMethod_Asynchronous()
        {
            // Arrange
            Type controllerType = typeof(MethodLocatorController);
            AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);

            // Act
            ActionDescriptorCreator creator          = selector.FindAction(new ControllerContext(), "EventPattern");
            ActionDescriptor        actionDescriptor = creator("someName", new Mock <ControllerDescriptor>().Object);

            // Assert
            var castActionDescriptor = Assert.IsType <ReflectedAsyncActionDescriptor>(actionDescriptor);

            Assert.Equal("EventPatternAsync", castActionDescriptor.AsyncMethodInfo.Name);
            Assert.Equal("EventPatternCompleted", castActionDescriptor.CompletedMethodInfo.Name);
        }
        public void FindActionMethodWithDelegatePatternType()
        {
            // Arrange
            Type controllerType = typeof(MethodLocatorController);
            AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);

            // Act
            ActionDescriptorCreator creator          = selector.FindActionMethod(null, "DelegatePattern");
            ActionDescriptor        actionDescriptor = creator("someName", new Mock <ControllerDescriptor>().Object);

            // Assert
            Assert.IsInstanceOfType(actionDescriptor, typeof(ReflectedDelegatePatternActionDescriptor));
            ReflectedDelegatePatternActionDescriptor castActionDescriptor = (ReflectedDelegatePatternActionDescriptor)actionDescriptor;

            Assert.AreEqual("DelegatePattern", castActionDescriptor.ActionMethod.Name);
        }
예제 #13
0
        public void FindActionMethod_GenericTask()
        {
            // Arrange
            Type controllerType = typeof(MethodLocatorController);
            AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);

            // Act
            ActionDescriptorCreator creator          = selector.FindAction(new ControllerContext(), "GenericTaskPattern");
            ActionDescriptor        actionDescriptor = creator("someName", new Mock <ControllerDescriptor>().Object);

            // Assert
            var castActionDescriptor = Assert.IsType <TaskAsyncActionDescriptor>(actionDescriptor);

            Assert.Equal("GenericTaskPattern", castActionDescriptor.TaskMethodInfo.Name);
            Assert.Equal(typeof(Task <string>), castActionDescriptor.TaskMethodInfo.ReturnType);
        }
        public void FindActionMethod_Asynchronous()
        {
            // Arrange
            Type controllerType = typeof(MethodLocatorController);
            AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);

            // Act
            ActionDescriptorCreator creator          = selector.FindAction(null, "EventPattern");
            ActionDescriptor        actionDescriptor = creator("someName", new Mock <ControllerDescriptor>().Object);

            // Assert
            Assert.IsInstanceOfType(actionDescriptor, typeof(ReflectedAsyncActionDescriptor));
            ReflectedAsyncActionDescriptor castActionDescriptor = (ReflectedAsyncActionDescriptor)actionDescriptor;

            Assert.AreEqual("EventPatternAsync", castActionDescriptor.AsyncMethodInfo.Name);
            Assert.AreEqual("EventPatternCompleted", castActionDescriptor.CompletedMethodInfo.Name);
        }
        public void FindAction_ReturnsMatchingMethodIfOneMethodMatches()
        {
            // Arrange
            Type controllerType = typeof(SelectionAttributeController);
            AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);

            // Act
            ActionDescriptorCreator creator          = selector.FindAction(null, "OneMatch");
            ActionDescriptor        actionDescriptor = creator("someName", new Mock <ControllerDescriptor>().Object);

            // Assert
            Assert.IsInstanceOfType(actionDescriptor, typeof(ReflectedActionDescriptor));
            ReflectedActionDescriptor castActionDescriptor = (ReflectedActionDescriptor)actionDescriptor;

            Assert.AreEqual("OneMatch", castActionDescriptor.MethodInfo.Name, "Method named OneMatch() should have matched.");
            Assert.AreEqual(typeof(string), castActionDescriptor.MethodInfo.GetParameters()[0].ParameterType, "Method overload OneMatch(string) should have matched.");
        }
예제 #16
0
        public void FindAction_ReturnsMethodWithActionSelectionAttributeIfMultipleMethodsMatchRequest()
        {
            // DevDiv Bugs 212062: If multiple action methods match a request, we should match only the methods with an
            // [ActionMethod] attribute since we assume those methods are more specific.

            // Arrange
            Type controllerType = typeof(SelectionAttributeController);
            AsyncActionMethodSelector selector = new AsyncActionMethodSelector(controllerType);

            // Act
            ActionDescriptorCreator creator          = selector.FindAction(new ControllerContext(), "ShouldMatchMethodWithSelectionAttribute");
            ActionDescriptor        actionDescriptor = creator("someName", new Mock <ControllerDescriptor>().Object);

            // Assert
            var castActionDescriptor = Assert.IsType <ReflectedActionDescriptor>(actionDescriptor);

            Assert.Equal("MethodHasSelectionAttribute1", castActionDescriptor.MethodInfo.Name);
        }
예제 #17
0
        private static List <ActionDescriptor> GetActionDescriptors(ReflectedAsyncControllerDescriptor controller)
        {
            Contract.Assert(controller != null);

            AsyncActionMethodSelector actionSelector = controller.Selector;

            var actions = new List <ActionDescriptor>();

            foreach (MethodInfo method in actionSelector.ActionMethods)
            {
                string actionName = actionSelector.GetActionName(method);
                ActionDescriptorCreator creator = actionSelector.GetActionDescriptorDelegate(method);
                Debug.Assert(creator != null);

                ActionDescriptor action = creator(actionName, controller);
                actions.Add(action);
            }

            return(actions);
        }
예제 #18
0
        public override ActionDescriptor FindAction(ControllerContext controllerContext, string actionName)
        {
            if (controllerContext == null)
            {
                throw new ArgumentNullException("controllerContext");
            }
            if (String.IsNullOrEmpty(actionName))
            {
                throw new ArgumentException(MvcResources.Common_NullOrEmpty, "actionName");
            }

            ActionDescriptorCreator creator = _selector.FindActionMethod(controllerContext, actionName);

            if (creator == null)
            {
                return(null);
            }

            return(creator(actionName, this));
        }
예제 #19
0
        public override ActionDescriptor FindAction(ControllerContext controllerContext, string actionName)
        {
            if (controllerContext == null)
            {
                throw new ArgumentNullException("controllerContext");
            }
            if (String.IsNullOrEmpty(actionName))
            {
                throw Error.ParameterCannotBeNullOrEmpty("actionName");
            }

            ActionDescriptorCreator creator = _selector.FindAction(controllerContext, actionName);

            if (creator == null)
            {
                return(null);
            }

            return(creator(actionName, this));
        }
        internal List <RouteEntry> MapMvcAttributeRoutes(ReflectedAsyncControllerDescriptor controllerDescriptor)
        {
            string prefix = controllerDescriptor.GetPrefixFrom();

            ValidatePrefixTemplate(prefix, controllerDescriptor);

            RouteAreaAttribute area       = controllerDescriptor.GetAreaFrom();
            string             areaName   = controllerDescriptor.GetAreaName(area);
            string             areaPrefix = area != null ? area.AreaPrefix ?? area.AreaName : null;

            ValidateAreaPrefixTemplate(areaPrefix, areaName, controllerDescriptor);

            AsyncActionMethodSelector actionSelector    = controllerDescriptor.Selector;
            IEnumerable <MethodInfo>  actionMethodsInfo = actionSelector.DirectRouteMethods;

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

            foreach (var method in actionMethodsInfo)
            {
                string actionName = actionSelector.GetActionName(method);
                ActionDescriptorCreator creator = actionSelector.GetActionDescriptorDelegate(method);
                Debug.Assert(creator != null);

                ActionDescriptor actionDescriptor = creator(actionName, controllerDescriptor);

                IEnumerable <IRouteInfoProvider> routeAttributes = GetRouteAttributes(method, controllerDescriptor.ControllerType);

                foreach (var routeAttribute in routeAttributes)
                {
                    ValidateTemplate(routeAttribute.Template, actionName, controllerDescriptor);

                    string template = CombinePrefixAndAreaWithTemplate(areaPrefix, prefix, routeAttribute.Template);
                    Route  route    = _routeBuilder.BuildDirectRoute(template, routeAttribute, controllerDescriptor, actionDescriptor);

                    RouteEntry entry = new RouteEntry
                    {
                        Name     = routeAttribute.Name,
                        Route    = route,
                        Template = template,
                    };

                    routeEntries.Add(entry);
                }
            }

            // Check for controller-level routes.
            IEnumerable <IRouteInfoProvider> controllerRouteAttributes = controllerDescriptor.GetDirectRoutes();

            List <ActionDescriptor> actions = new List <ActionDescriptor>();

            foreach (var actionMethod in actionSelector.StandardRouteMethods)
            {
                string actionName = actionSelector.GetActionName(actionMethod);
                ActionDescriptorCreator creator = actionSelector.GetActionDescriptorDelegate(actionMethod);
                Debug.Assert(creator != null);

                ActionDescriptor actionDescriptor = creator(actionName, controllerDescriptor);
                actions.Add(actionDescriptor);
            }

            // Don't create a route for the controller-level attributes if no actions could possibly match
            if (actions.Any())
            {
                foreach (var routeAttribute in controllerRouteAttributes)
                {
                    string template = CombinePrefixAndAreaWithTemplate(areaPrefix, prefix, routeAttribute.Template);

                    Route      route = _routeBuilder.BuildDirectRoute(template, routeAttribute, controllerDescriptor, actions);
                    RouteEntry entry = new RouteEntry
                    {
                        Name     = routeAttribute.Name,
                        Route    = route,
                        Template = template,
                    };

                    routeEntries.Add(entry);
                }
            }

            return(routeEntries);
        }