public void GetActions_RouteOnController_CreatesOneActionInforPerRouteTemplateOnAction(Type controller)
        {
            // Arrange
            var conventions = new DefaultActionDiscoveryConventions();
            var typeInfo    = controller.GetTypeInfo();

            // Act
            var actionInfos = conventions.GetActions(typeInfo.GetMethod("Index"), typeInfo);

            // Assert
            Assert.Equal(2, actionInfos.Count());

            foreach (var action in actionInfos)
            {
                Assert.Equal("Index", action.ActionName);
                Assert.True(action.RequireActionNameMatch);

                var httpMethod = Assert.Single(action.HttpMethods);
                Assert.Equal("GET", httpMethod);

                Assert.NotNull(action.AttributeRoute);

                Assert.IsType <HttpGetAttribute>(Assert.Single(action.Attributes));
            }

            Assert.Single(actionInfos, ai => ai.AttributeRoute.Template.Equals("List"));
            Assert.Single(actionInfos, ai => ai.AttributeRoute.Template.Equals("All"));
        }
        public void GetActions_AttributeRouteOnAction()
        {
            // Arrange
            var conventions = new DefaultActionDiscoveryConventions();
            var typeInfo    = typeof(NoRouteAttributeOnControllerController).GetTypeInfo();
            var actionName  = nameof(NoRouteAttributeOnControllerController.Edit);

            // Act
            var actionInfos = conventions.GetActions(typeInfo.GetMethod(actionName), typeInfo);

            // Assert
            var action = Assert.Single(actionInfos);

            Assert.Equal("Edit", action.ActionName);
            Assert.True(action.RequireActionNameMatch);

            var httpMethod = Assert.Single(action.HttpMethods);

            Assert.Equal("POST", httpMethod);

            Assert.NotNull(action.AttributeRoute);
            Assert.Equal("Change", action.AttributeRoute.Template);

            Assert.IsType <HttpPostAttribute>(Assert.Single(action.Attributes));
        }
        public void GetActions_AttributeRouteOnAction_CreatesOneActionInforPerRouteTemplate()
        {
            // Arrange
            var conventions = new DefaultActionDiscoveryConventions();
            var typeInfo    = typeof(NoRouteAttributeOnControllerController).GetTypeInfo();
            var actionName  = nameof(NoRouteAttributeOnControllerController.Index);

            // Act
            var actionInfos = conventions.GetActions(typeInfo.GetMethod(actionName), typeInfo);

            // Assert
            Assert.Equal(2, actionInfos.Count());

            foreach (var action in actionInfos)
            {
                Assert.Equal("Index", action.ActionName);
                Assert.True(action.RequireActionNameMatch);

                Assert.NotNull(action.AttributeRoute);
            }

            var list       = Assert.Single(actionInfos, ai => ai.AttributeRoute.Template.Equals("List"));
            var listMethod = Assert.Single(list.HttpMethods);

            Assert.Equal("POST", listMethod);
            Assert.IsType <HttpPostAttribute>(Assert.Single(list.Attributes));

            var all       = Assert.Single(actionInfos, ai => ai.AttributeRoute.Template.Equals("All"));
            var allMethod = Assert.Single(all.HttpMethods);

            Assert.Equal("GET", allMethod);
            Assert.IsType <HttpGetAttribute>(Assert.Single(all.Attributes));
        }
        private async Task <ActionDescriptor> InvokeActionSelector(RouteContext context,
                                                                   DefaultActionDiscoveryConventions actionDiscoveryConventions)
        {
            var actionDescriptorProvider = GetActionDescriptorProvider(actionDiscoveryConventions);
            var descriptorProvider       =
                new NestedProviderManager <ActionDescriptorProviderContext>(new[] { actionDescriptorProvider });

            var serviceContainer = new ServiceContainer();

            serviceContainer.AddService(typeof(INestedProviderManager <ActionDescriptorProviderContext>),
                                        descriptorProvider);

            var actionCollectionDescriptorProvider = new DefaultActionDescriptorsCollectionProvider(serviceContainer);
            var decisionTreeProvider = new ActionSelectorDecisionTreeProvider(actionCollectionDescriptorProvider);

            var actionConstraintProvider = new NestedProviderManager <ActionConstraintProviderContext>(
                new INestedProvider <ActionConstraintProviderContext>[]
            {
                new DefaultActionConstraintProvider(serviceContainer),
            });

            var defaultActionSelector = new DefaultActionSelector(
                actionCollectionDescriptorProvider,
                decisionTreeProvider,
                actionConstraintProvider,
                NullLoggerFactory.Instance);

            return(await defaultActionSelector.SelectAsync(context));
        }
        public void GetActions_ConventionallyRoutedAction_WithMultipleOverlappingHttpConstraints()
        {
            // Arrange
            var conventions = new DefaultActionDiscoveryConventions();
            var typeInfo    = typeof(ConventionallyRoutedController).GetTypeInfo();
            var actionName  = nameof(ConventionallyRoutedController.List);

            // Act
            var actionInfos = conventions.GetActions(typeInfo.GetMethod(actionName), typeInfo);

            // Assert
            Assert.Equal(3, actionInfos.Count());
            Assert.Single(actionInfos, a => a.HttpMethods.Contains("GET"));
            Assert.Single(actionInfos, a => a.HttpMethods.Contains("POST"));
            Assert.Single(actionInfos, a => a.HttpMethods.Contains("PUT"));

            foreach (var action in actionInfos)
            {
                Assert.Equal("List", action.ActionName);
                Assert.True(action.RequireActionNameMatch);
                Assert.Null(action.AttributeRoute);

                Assert.Equal(3, action.Attributes.Length);
                Assert.Single(action.Attributes, a => a is HttpPutAttribute);
                Assert.Single(action.Attributes, a => a is HttpGetAttribute);
                Assert.Single(action.Attributes, a => a is AcceptVerbsAttribute);
            }
        }
Esempio n. 6
0
        public void IsController_ClosedGenericClass()
        {
            // Arrange
            var conventions = new DefaultActionDiscoveryConventions();
            var typeInfo    = typeof(OpenGenericController <string>).GetTypeInfo();

            // Act
            var isController = conventions.IsController(typeInfo);

            // Assert
            Assert.False(isController);
        }
Esempio n. 7
0
        public void IsController_DerivedGenericClass()
        {
            // Arrange
            var conventions = new DefaultActionDiscoveryConventions();
            var typeInfo    = typeof(DerivedGenericController).GetTypeInfo();

            // Act
            var isController = conventions.IsController(typeInfo);

            // Assert
            Assert.True(isController);
        }
Esempio n. 8
0
        public void IsController_Poco_WithNamingConvention()
        {
            // Arrange
            var conventions = new DefaultActionDiscoveryConventions();
            var typeInfo    = typeof(PocoController).GetTypeInfo();

            // Act
            var isController = conventions.IsController(typeInfo);

            // Assert
            Assert.True(isController);
        }
Esempio n. 9
0
        public void IsController_NoControllerSuffix()
        {
            // Arrange
            var conventions = new DefaultActionDiscoveryConventions();
            var typeInfo    = typeof(NoSuffix).GetTypeInfo();

            // Act
            var isController = conventions.IsController(typeInfo);

            // Assert
            Assert.True(isController);
        }
        private ControllerActionDescriptorProvider GetActionDescriptorProvider(DefaultActionDiscoveryConventions actionDiscoveryConventions)
        {
            var assemblies       = new Assembly[] { typeof(DefaultActionDiscoveryConventionsActionSelectionTests).GetTypeInfo().Assembly, };
            var AssemblyProvider = new Mock <IAssemblyProvider>();

            AssemblyProvider.SetupGet(x => x.CandidateAssemblies).Returns(assemblies);
            return(new ControllerActionDescriptorProvider(
                       AssemblyProvider.Object,
                       actionDiscoveryConventions,
                       new TestGlobalFilterProvider(),
                       new MockMvcOptionsAccessor()));
        }
Esempio n. 11
0
        public void IsController_AbstractClass()
        {
            // Arrange
            var conventions = new DefaultActionDiscoveryConventions();
            var typeInfo    = typeof(AbstractController).GetTypeInfo();

            // Act
            var isController = conventions.IsController(typeInfo);

            // Assert
            Assert.False(isController);
        }
        private ReflectedActionDescriptorProvider GetActionDescriptorProvider(DefaultActionDiscoveryConventions actionDiscoveryConventions)
        {
            var assemblies = new Assembly[] { typeof(DefaultActionDiscoveryConventionsActionSelectionTests).GetTypeInfo().Assembly, };
            var controllerAssemblyProvider = new Mock <IControllerAssemblyProvider>();

            controllerAssemblyProvider.SetupGet(x => x.CandidateAssemblies).Returns(assemblies);
            return(new ReflectedActionDescriptorProvider(
                       controllerAssemblyProvider.Object,
                       actionDiscoveryConventions,
                       null,
                       new MockMvcOptionsAccessor(),
                       Mock.Of <IInlineConstraintResolver>()));
        }
Esempio n. 13
0
        public void IsValidActionMethod_WithInheritedMethods(string methodName, bool expected)
        {
            // Arrange
            var conventions = new DefaultActionDiscoveryConventions();
            var method      = typeof(DerivedController).GetMethod(methodName);

            Assert.NotNull(method);

            // Act
            var isValid = conventions.IsValidActionMethod(method);

            // Assert
            Assert.Equal(expected, isValid);
        }
Esempio n. 14
0
        public void IsValidActionMethod_OverridenMethodControllerClass()
        {
            // Arrange
            var conventions = new DefaultActionDiscoveryConventions();
            var method      = typeof(BaseController).GetMethod("Redirect");

            Assert.NotNull(method);

            // Act
            var isValid = conventions.IsValidActionMethod(method);

            // Assert
            Assert.False(isValid);
        }
Esempio n. 15
0
        public void IsValidActionMethod_OverridenNonActionMethod()
        {
            // Arrange
            var conventions = new DefaultActionDiscoveryConventions();
            var method      = typeof(DerivedController).GetMethod("OverridenNonActionMethod");

            Assert.NotNull(method);

            // Act
            var isValid = conventions.IsValidActionMethod(method);

            // Assert
            Assert.False(isValid);
        }
Esempio n. 16
0
        public void IsValidActionMethod_OperatorOverloadingMethod_FromOperatorOverloadingController()
        {
            // Arrange
            var conventions = new DefaultActionDiscoveryConventions();
            var method      = typeof(OperatorOverloadingController).GetMethod("op_Addition");

            Assert.NotNull(method);
            Assert.True(method.IsSpecialName);

            // Act
            var isValid = conventions.IsValidActionMethod(method);

            // Assert
            Assert.False(isValid);
        }
Esempio n. 17
0
        public void IsValidActionMethod_StaticMethods(string methodName)
        {
            // Arrange
            var conventions = new DefaultActionDiscoveryConventions();
            var method      = typeof(DerivedController).GetMethod(
                methodName,
                BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.Public);

            Assert.NotNull(method);

            // Act
            var isValid = conventions.IsValidActionMethod(method);

            // Assert
            Assert.False(isValid);
        }
Esempio n. 18
0
        public void IsValidActionMethod_PrivateMethod_FromUserDefinedController()
        {
            // Arrange
            var conventions = new DefaultActionDiscoveryConventions();
            var method      = typeof(DerivedController).GetMethod(
                "PrivateMethod",
                BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance);

            Assert.NotNull(method);

            // Act
            var isValid = conventions.IsValidActionMethod(method);

            // Assert
            Assert.False(isValid);
        }
        public void GetActions_ConventionallyRoutedAction_WithMultipleOverlappingHttpConstraints()
        {
            // Arrange
            var conventions = new DefaultActionDiscoveryConventions();
            var typeInfo    = typeof(ConventionallyRoutedController).GetTypeInfo();
            var actionName  = nameof(ConventionallyRoutedController.List);

            // Act
            var actionInfos = conventions.GetActions(typeInfo.GetMethod(actionName), typeInfo);

            // Assert
            var action = Assert.Single(actionInfos);

            Assert.Equal("List", action.ActionName);
            Assert.True(action.RequireActionNameMatch);
            Assert.Equal(new[] { "GET", "PUT", "POST" }, action.HttpMethods);
            Assert.Null(action.AttributeRoute);
        }
        public void GetActions_RouteAttributeOnController_CreatesAttributeRoute_ForNonAttributedActions(Type controller)
        {
            // Arrange
            var conventions = new DefaultActionDiscoveryConventions();
            var typeInfo    = controller.GetTypeInfo();

            // Act
            var actionInfos = conventions.GetActions(typeInfo.GetMethod("Delete"), typeInfo);

            // Assert
            var action = Assert.Single(actionInfos);

            Assert.Equal("Delete", action.ActionName);
            Assert.True(action.RequireActionNameMatch);

            Assert.Null(action.HttpMethods);

            Assert.Null(action.AttributeRoute);
        }
        public void GetActions_ConventionallyRoutedAction_WithoutHttpConstraints()
        {
            // Arrange
            var conventions = new DefaultActionDiscoveryConventions();
            var typeInfo    = typeof(ConventionallyRoutedController).GetTypeInfo();
            var actionName  = nameof(ConventionallyRoutedController.Edit);

            // Act
            var actionInfos = conventions.GetActions(typeInfo.GetMethod(actionName), typeInfo);

            // Assert
            var action = Assert.Single(actionInfos);

            Assert.Equal("Edit", action.ActionName);
            Assert.True(action.RequireActionNameMatch);
            Assert.Null(action.HttpMethods);
            Assert.Null(action.AttributeRoute);
            Assert.Empty(action.Attributes);
        }
        public void GetActions_NoRouteOnController_AllowsConventionallyRoutedActions_OnTheSameController()
        {
            // Arrange
            var conventions = new DefaultActionDiscoveryConventions();
            var typeInfo    = typeof(NoRouteAttributeOnControllerController).GetTypeInfo();
            var actionName  = nameof(NoRouteAttributeOnControllerController.Remove);

            // Act
            var actionInfos = conventions.GetActions(typeInfo.GetMethod(actionName), typeInfo);

            // Assert
            var action = Assert.Single(actionInfos);

            Assert.Equal("Remove", action.ActionName);
            Assert.True(action.RequireActionNameMatch);

            Assert.Null(action.HttpMethods);

            Assert.Null(action.AttributeRoute);
        }
        public void GetActions_ConventionallyRoutedActionWithHttpConstraints_AndInvalidRouteTemplateProvider()
        {
            // Arrange
            var conventions = new DefaultActionDiscoveryConventions();
            var typeInfo    = typeof(ConventionallyRoutedController).GetTypeInfo();
            var actionName  = nameof(ConventionallyRoutedController.Delete);

            // Act
            var actionInfos = conventions.GetActions(typeInfo.GetMethod(actionName), typeInfo);

            // Assert
            var action = Assert.Single(actionInfos);

            Assert.Equal("Delete", action.ActionName);
            Assert.True(action.RequireActionNameMatch);

            var httpMethod = Assert.Single(action.HttpMethods);

            Assert.Equal("DELETE", httpMethod);
            Assert.Null(action.AttributeRoute);
        }
        public void GetActions_AttributeRouteOnAction_RouteAttribute()
        {
            // Arrange
            var conventions = new DefaultActionDiscoveryConventions();
            var typeInfo    = typeof(NoRouteAttributeOnControllerController).GetTypeInfo();
            var actionName  = nameof(NoRouteAttributeOnControllerController.Update);

            // Act
            var actionInfos = conventions.GetActions(typeInfo.GetMethod(actionName), typeInfo);

            // Assert
            var action = Assert.Single(actionInfos);

            Assert.Equal("Update", action.ActionName);
            Assert.True(action.RequireActionNameMatch);

            Assert.Null(action.HttpMethods);

            Assert.NotNull(action.AttributeRoute);
            Assert.Equal("Update", action.AttributeRoute.Template);
        }
        public void GetActions_AttributeRouteOnAction_AcceptVerbsAttributeWithTemplate()
        {
            // Arrange
            var conventions = new DefaultActionDiscoveryConventions();
            var typeInfo    = typeof(NoRouteAttributeOnControllerController).GetTypeInfo();
            var actionName  = nameof(NoRouteAttributeOnControllerController.List);

            // Act
            var actionInfos = conventions.GetActions(typeInfo.GetMethod(actionName), typeInfo);

            // Assert
            var action = Assert.Single(actionInfos);

            Assert.Equal("List", action.ActionName);
            Assert.True(action.RequireActionNameMatch);

            Assert.Equal(new[] { "GET", "HEAD" }, action.HttpMethods);

            Assert.NotNull(action.AttributeRoute);
            Assert.Equal("ListAll", action.AttributeRoute.Template);
        }
        private async Task <ActionDescriptor> InvokeActionSelector(RouteContext context,
                                                                   DefaultActionDiscoveryConventions actionDiscoveryConventions)
        {
            var actionDescriptorProvider = GetActionDescriptorProvider(actionDiscoveryConventions);
            var descriptorProvider       =
                new NestedProviderManager <ActionDescriptorProviderContext>(new[] { actionDescriptorProvider });

            var serviceContainer = new ServiceContainer();

            serviceContainer.AddService(typeof(INestedProviderManager <ActionDescriptorProviderContext>),
                                        descriptorProvider);

            var actionCollectionDescriptorProvider = new DefaultActionDescriptorsCollectionProvider(serviceContainer);

            var bindingProvider = new Mock <IActionBindingContextProvider>();

            var defaultActionSelector = new DefaultActionSelector(actionCollectionDescriptorProvider,
                                                                  bindingProvider.Object,
                                                                  NullLoggerFactory.Instance);

            return(await defaultActionSelector.SelectAsync(context));
        }
        public void GetActions_ConventionallyRoutedAction_WithHttpConstraints()
        {
            // Arrange
            var conventions = new DefaultActionDiscoveryConventions();
            var typeInfo    = typeof(ConventionallyRoutedController).GetTypeInfo();
            var actionName  = nameof(ConventionallyRoutedController.Update);

            // Act
            var actionInfos = conventions.GetActions(typeInfo.GetMethod(actionName), typeInfo);

            // Assert
            Assert.Equal(2, actionInfos.Count());
            Assert.Single(actionInfos, a => a.HttpMethods.Contains("PUT"));
            Assert.Single(actionInfos, a => a.HttpMethods.Contains("PATCH"));

            foreach (var action in actionInfos)
            {
                Assert.Equal("Update", action.ActionName);
                Assert.True(action.RequireActionNameMatch);
                Assert.Null(action.AttributeRoute);
                Assert.IsType <CustomHttpMethodsAttribute>(Assert.Single(action.Attributes));
            }
        }
 private ReflectedActionDescriptorProvider GetActionDescriptorProvider(DefaultActionDiscoveryConventions actionDiscoveryConventions)
 {
     var assemblies = new Assembly[] { typeof(DefaultActionDiscoveryConventionsActionSelectionTests).GetTypeInfo().Assembly, };
     var controllerAssemblyProvider = new Mock<IControllerAssemblyProvider>();
     controllerAssemblyProvider.SetupGet(x => x.CandidateAssemblies).Returns(assemblies);
     return new ReflectedActionDescriptorProvider(
                                 controllerAssemblyProvider.Object,
                                 actionDiscoveryConventions,
                                 null,
                                 new MockMvcOptionsAccessor(),
                                 Mock.Of<IInlineConstraintResolver>());
 }
        private async Task<ActionDescriptor> InvokeActionSelector(RouteContext context,
                                                                  DefaultActionDiscoveryConventions actionDiscoveryConventions)
        {
            var actionDescriptorProvider = GetActionDescriptorProvider(actionDiscoveryConventions);
            var descriptorProvider =
                new NestedProviderManager<ActionDescriptorProviderContext>(new[] { actionDescriptorProvider });

            var serviceContainer = new ServiceContainer();
            serviceContainer.AddService(typeof(INestedProviderManager<ActionDescriptorProviderContext>),
                                        descriptorProvider);

            var actionCollectionDescriptorProvider = new DefaultActionDescriptorsCollectionProvider(serviceContainer);

            var bindingProvider = new Mock<IActionBindingContextProvider>();

            var defaultActionSelector = new DefaultActionSelector(actionCollectionDescriptorProvider,
                                                                  bindingProvider.Object,
                                                                  NullLoggerFactory.Instance);

            return await defaultActionSelector.SelectAsync(context);
        }