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 ); }
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); }
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."); }
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."); }
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); }
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"); }
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)); }
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); }
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."); }
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); }
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); }
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)); }
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); }