internal ControllerConfigurationSettings()
        {
            _mappings = new Dictionary<string, Type>(StringComparer.OrdinalIgnoreCase);
			_filterProviders = new FilterProviderCollection();

			InitDefaultFilterProviders();
        }
        public void GetFiltersSortsFiltersByOrderFirstThenScope()
        {
            // Arrange
            var context = new ControllerContext();
            var descriptor = new Mock<ActionDescriptor>().Object;
            var actionFilter = new Filter(new Object(), FilterScope.Action, null);
            var controllerFilter = new Filter(new Object(), FilterScope.Controller, null);
            var globalFilter = new Filter(new Object(), FilterScope.Global, null);
            var earlyActionFilter = new Filter(new Object(), FilterScope.Action, -100);
            var lateGlobalFilter = new Filter(new Object(), FilterScope.Global, 100);
            var provider = new Mock<IFilterProvider>(MockBehavior.Strict);
            var collection = new FilterProviderCollection(new[] { provider.Object });
            provider.Setup(p => p.GetFilters(context, descriptor))
                .Returns(new[] { actionFilter, controllerFilter, globalFilter, earlyActionFilter, lateGlobalFilter });

            // Act
            Filter[] result = collection.GetFilters(context, descriptor).ToArray();

            // Assert
            Assert.Equal(5, result.Length);
            Assert.Same(earlyActionFilter, result[0]);
            Assert.Same(globalFilter, result[1]);
            Assert.Same(controllerFilter, result[2]);
            Assert.Same(actionFilter, result[3]);
            Assert.Same(lateGlobalFilter, result[4]);
        }
Exemple #3
0
        public static void RegisterProvider(FilterProviderCollection providerCollection)
        {
            var filterProvider = FluentMvcConfiguration
                .ConfigureFilterProvider(x =>
                {
                    x.WithResultFactory<ActionResultFactory>()
                        .WithResultFactory<JsonResultFactory>()
                        .WithResultFactory<ViewResultFactory>(isDefault: true);

                    x.WithResultFactory<ErrorThrowingResultFactory>(
                        Apply.For<HomeController>(hc => hc.ErrorResultFactory()));

                    x.WithFilter<HandleErrorAttribute>();
                    x.WithFilter<AuthorizeAttribute>(
                        Except
                            .For<AccountController>(ac => ac.LogOn())
                            .AndFor<AccountController>(
                                ac => ac.LogOn(null, null, false, null))
                            .AndFor<HomeController>());

                    x.WithFilter<ErrorThrowingFilter>(
                        Apply.When<ExpectsHtml>().For<HomeController>(hc => hc.About()));
                });

            providerCollection.Insert(0, filterProvider);
        }
 static FilterProviders()
 {
     Providers = new FilterProviderCollection();
     Providers.Add(GlobalFilters.Filters);
     Providers.Add(new FilterAttributeFilterProvider());
     Providers.Add(new ControllerInstanceFilterProvider());
 }
        /// <summary>
        /// Replace the default filter attribute filter provider with an instance
        /// of this type.
        /// </summary>
        /// <param name="filterProviders">The global filter provider collection.</param>
        public static void Install(FilterProviderCollection filterProviders)
        {
            if (filterProviders == null) throw new ArgumentNullException("filterProviders");

            var existing = filterProviders.OfType<FilterAttributeFilterProvider>().SingleOrDefault();
            if (existing != null)
                filterProviders.Remove(existing);
            filterProviders.Add(new ImportCapableFilterAttributeFilterProvider());
        }
        public void GetFiltersUsesRegisteredProviders() {
            // Arrange
            var context = new ControllerContext();
            var descriptor = new Mock<ActionDescriptor>().Object;
            var filter = new Filter(new Object(), FilterScope.Action, null);
            var provider = new Mock<IFilterProvider>(MockBehavior.Strict);
            var collection = new FilterProviderCollection(new[] { provider.Object });
            provider.Setup(p => p.GetFilters(context, descriptor)).Returns(new[] { filter });

            // Act
            IEnumerable<Filter> result = collection.GetFilters(context, descriptor);

            // Assert
            Assert.AreSame(filter, result.Single());
        }
        public void GuardClauses() {
            // Arrange
            var context = new ControllerContext();
            var descriptor = new Mock<ActionDescriptor>().Object;
            var collection = new FilterProviderCollection();

            // Act & Assert
            ExceptionHelper.ExpectArgumentNullException(
                () => collection.GetFilters(null, descriptor),
                "controllerContext"
            );
            ExceptionHelper.ExpectArgumentNullException(
                () => collection.GetFilters(context, null),
                "actionDescriptor"
            );
        }
        public void GetFiltersDelegatesToResolver()
        {
            // Arrange
            var context = new ControllerContext();
            var descriptor = new Mock<ActionDescriptor>().Object;
            var filter = new Filter(new Object(), FilterScope.Action, null);
            var provider = new Mock<IFilterProvider>(MockBehavior.Strict);
            var resolver = new Resolver<IEnumerable<IFilterProvider>> { Current = new[] { provider.Object } };
            var collection = new FilterProviderCollection(resolver);

            provider.Setup(p => p.GetFilters(context, descriptor)).Returns(new[] { filter });

            // Act
            IEnumerable<Filter> result = collection.GetFilters(context, descriptor);

            // Assert
            Assert.Same(filter, result.Single());
        }
        private static void TestCacheReset(Action<FilterProviderCollection> mutatingAction)
        {
            // Arrange
            var providers = new List<IFilterProvider>() 
            {
                new Mock<IFilterProvider>(MockBehavior.Strict).Object, 
                new Mock<IFilterProvider>(MockBehavior.Strict).Object
            };
            var collection = new FilterProviderCollection(providers);

            // Act
            mutatingAction(collection);

            IFilterProvider[] combined = collection.CombinedItems;

            // Assert
            Assert.Equal(providers, combined);
        }
        public void FilterProviderCollectionCombinedItemsCaches()
        {
            // Arrange
            var providers = new IFilterProvider[] 
            {
                new Mock<IFilterProvider>(MockBehavior.Strict).Object, 
                new Mock<IFilterProvider>(MockBehavior.Strict).Object
            };
            var collection = new FilterProviderCollection(providers);

            // Act
            IFilterProvider[] combined1 = collection.CombinedItems;
            IFilterProvider[] combined2 = collection.CombinedItems;

            // Assert
            Assert.Equal(providers, combined1);
            Assert.Same(combined1, combined2);
        }
        public void GetFiltersIncludesLastFilterOnlyWithAttributeUsageAllowMultipleFalse()      // DDB #222988
        // Arrange
        {
            var context          = new ControllerContext();
            var descriptor       = new Mock <ActionDescriptor>().Object;
            var globalFilter     = new Filter(new AllowMultipleFalseAttribute(), FilterScope.Global, null);
            var controllerFilter = new Filter(new AllowMultipleFalseAttribute(), FilterScope.Controller, null);
            var actionFilter     = new Filter(new AllowMultipleFalseAttribute(), FilterScope.Action, null);
            var provider         = new Mock <IFilterProvider>(MockBehavior.Strict);
            var collection       = new FilterProviderCollection(new[] { provider.Object });

            provider.Setup(p => p.GetFilters(context, descriptor))
            .Returns(new[] { controllerFilter, actionFilter, globalFilter });

            // Act
            IEnumerable <Filter> result = collection.GetFilters(context, descriptor);

            // Assert
            Assert.AreSame(actionFilter, result.Single());
        }
        public void GetFiltersDelegatesToResolver()
        {
            // Arrange
            var context    = new ControllerContext();
            var descriptor = new Mock <ActionDescriptor>().Object;
            var filter     = new Filter(new Object(), FilterScope.Action, null);
            var provider   = new Mock <IFilterProvider>(MockBehavior.Strict);
            var resolver   = new Resolver <IEnumerable <IFilterProvider> > {
                Current = new[] { provider.Object }
            };
            var collection = new FilterProviderCollection(resolver);

            provider.Setup(p => p.GetFilters(context, descriptor)).Returns(new[] { filter });

            // Act
            IEnumerable <Filter> result = collection.GetFilters(context, descriptor);

            // Assert
            Assert.Same(filter, result.Single());
        }
        public static IWindsorContainer InstallMvcFilterProvider(
            [NotNull] this FilterProviderCollection filterProviders, [NotNull] IWindsorContainer container,
            [CanBeNull] TypePropertyDescriptorCache propertyDescriptorCache)
        {
            if (filterProviders == null)
            {
                throw new ArgumentNullException(nameof(filterProviders));
            }
            if (container == null)
            {
                throw new ArgumentNullException(nameof(container));
            }
            var attributeFilterProviders = filterProviders.OfType <FilterAttributeFilterProvider>().ToArray();

            foreach (var attributeFilterProvider in attributeFilterProviders)
            {
                filterProviders.Remove(attributeFilterProvider);
            }
            filterProviders.Add(new WindsorFilterAttributeProvider(container, propertyDescriptorCache));
            return(container);
        }
        public void FilterProviderCollectionCombinedItemsDelegatesToResolver()
        {
            // Arrange
            var firstProvider = new Mock<IFilterProvider>();
            var secondProvider = new Mock<IFilterProvider>();
            var thirdProvider = new Mock<IFilterProvider>();
            var dependencyProviders = new IFilterProvider[] { firstProvider.Object, secondProvider.Object };
            var collectionProviders = new IFilterProvider[] { thirdProvider.Object };
            var expectedProviders = new IFilterProvider[] { firstProvider.Object, secondProvider.Object, thirdProvider.Object };

            Mock<IDependencyResolver> resolver = new Mock<IDependencyResolver>();
            resolver.Setup(r => r.GetServices(typeof(IFilterProvider))).Returns(dependencyProviders);

            var providers = new FilterProviderCollection(collectionProviders, resolver.Object);

            // Act
            IFilterProvider[] combined = providers.CombinedItems;

            // Assert
            Assert.Equal(expectedProviders, combined);
        }
Exemple #15
0
        public void FilterProviderCollectionCombinedItemsDelegatesToResolver()
        {
            // Arrange
            var firstProvider       = new Mock <IFilterProvider>();
            var secondProvider      = new Mock <IFilterProvider>();
            var thirdProvider       = new Mock <IFilterProvider>();
            var dependencyProviders = new IFilterProvider[] { firstProvider.Object, secondProvider.Object };
            var collectionProviders = new IFilterProvider[] { thirdProvider.Object };
            var expectedProviders   = new IFilterProvider[] { firstProvider.Object, secondProvider.Object, thirdProvider.Object };

            Mock <IDependencyResolver> resolver = new Mock <IDependencyResolver>();

            resolver.Setup(r => r.GetServices(typeof(IFilterProvider))).Returns(dependencyProviders);

            var providers = new FilterProviderCollection(collectionProviders, resolver.Object);

            // Act
            IFilterProvider[] combined = providers.CombinedItems;

            // Assert
            Assert.Equal(expectedProviders, combined);
        }
Exemple #16
0
        public void GetFiltersIncludesAllFiltersWithCustomFilterAllowMultipleTrue()
        { // DDB #222988
            // Arrange
            var context          = new ControllerContext();
            var descriptor       = new Mock <ActionDescriptor>().Object;
            var globalFilter     = new Filter(new AllowMultipleCustomFilter(true), FilterScope.Global, null);
            var controllerFilter = new Filter(new AllowMultipleCustomFilter(true), FilterScope.Controller, null);
            var actionFilter     = new Filter(new AllowMultipleCustomFilter(true), FilterScope.Action, null);
            var provider         = new Mock <IFilterProvider>(MockBehavior.Strict);
            var collection       = new FilterProviderCollection(new[] { provider.Object });

            provider.Setup(p => p.GetFilters(context, descriptor))
            .Returns(new[] { controllerFilter, actionFilter, globalFilter });

            // Act
            List <Filter> result = collection.GetFilters(context, descriptor).ToList();

            // Assert
            Assert.Same(globalFilter, result[0]);
            Assert.Same(controllerFilter, result[1]);
            Assert.Same(actionFilter, result[2]);
        }
Exemple #17
0
        public void GetFiltersSortsFiltersByOrderFirstThenScope()
        {
            // Arrange
            var context           = new ControllerContext();
            var descriptor        = new Mock <ActionDescriptor>().Object;
            var actionFilter      = new Filter(new Object(), FilterScope.Action, null);
            var controllerFilter  = new Filter(new Object(), FilterScope.Controller, null);
            var globalFilter      = new Filter(new Object(), FilterScope.Global, null);
            var earlyActionFilter = new Filter(new Object(), FilterScope.Action, -100);
            var lateGlobalFilter  = new Filter(new Object(), FilterScope.Global, 100);
            var provider          = new Mock <IFilterProvider>(MockBehavior.Strict);
            var collection        = new FilterProviderCollection(new[] { provider.Object });

            provider
            .Setup(p => p.GetFilters(context, descriptor))
            .Returns(
                new[]
            {
                actionFilter,
                controllerFilter,
                globalFilter,
                earlyActionFilter,
                lateGlobalFilter
            }
                );

            // Act
            Filter[] result = collection.GetFilters(context, descriptor).ToArray();

            // Assert
            Assert.Equal(5, result.Length);
            Assert.Same(earlyActionFilter, result[0]);
            Assert.Same(globalFilter, result[1]);
            Assert.Same(controllerFilter, result[2]);
            Assert.Same(actionFilter, result[3]);
            Assert.Same(lateGlobalFilter, result[4]);
        }
        public void GetFiltersIncludesLastFilterOnlyWithCustomFilterAllowMultipleFalse()
        { // DDB #222988
            // Arrange
            var context = new ControllerContext();
            var descriptor = new Mock<ActionDescriptor>().Object;
            var globalFilter = new Filter(new AllowMultipleCustomFilter(false), FilterScope.Global, null);
            var controllerFilter = new Filter(new AllowMultipleCustomFilter(false), FilterScope.Controller, null);
            var actionFilter = new Filter(new AllowMultipleCustomFilter(false), FilterScope.Action, null);
            var provider = new Mock<IFilterProvider>(MockBehavior.Strict);
            var collection = new FilterProviderCollection(new[] { provider.Object });
            provider.Setup(p => p.GetFilters(context, descriptor))
                .Returns(new[] { controllerFilter, actionFilter, globalFilter });

            // Act
            IEnumerable<Filter> result = collection.GetFilters(context, descriptor);

            // Assert
            Assert.Same(actionFilter, result.Single());
        }
 public ExternalReviewFilterProvider(IList <IFilterProvider> filters)
 {
     _filterProviders = new FilterProviderCollection(filters);
 }
 public CustomFilterProvider(IList <IFilterProvider> filters)
 {
     _filterProviders = new FilterProviderCollection(filters);
 }
Exemple #21
0
        //private IFilterProvider[] filters;

        public ExcludeFilterProvider(IFilterProvider[] filters)
        {
            this.filterProviders = new FilterProviderCollection(filters);
        }
 public static void RegisterGlobalProviders(FilterProviderCollection filterProviderCollection)
 {
     //filterProviderCollection.Add(new CrossSiteRequestForgeryFilter());
 }
 public void Init(IList <IFilterProvider> filters)
 {
     _filterProviders = new FilterProviderCollection(filters);
 }
        public void GetFiltersIncludesAllFiltersWithCustomFilterAllowMultipleTrue()
        { // DDB #222988
            // Arrange
            var context = new ControllerContext();
            var descriptor = new Mock<ActionDescriptor>().Object;
            var globalFilter = new Filter(new AllowMultipleCustomFilter(true), FilterScope.Global, null);
            var controllerFilter = new Filter(new AllowMultipleCustomFilter(true), FilterScope.Controller, null);
            var actionFilter = new Filter(new AllowMultipleCustomFilter(true), FilterScope.Action, null);
            var provider = new Mock<IFilterProvider>(MockBehavior.Strict);
            var collection = new FilterProviderCollection(new[] { provider.Object });
            provider.Setup(p => p.GetFilters(context, descriptor))
                .Returns(new[] { controllerFilter, actionFilter, globalFilter });

            // Act
            List<Filter> result = collection.GetFilters(context, descriptor).ToList();

            // Assert
            Assert.Same(globalFilter, result[0]);
            Assert.Same(controllerFilter, result[1]);
            Assert.Same(actionFilter, result[2]);
        }
 /// <summary>
 /// Sets up the filter providers; default implementation uses <see cref="AdjustableFilterProvider"/>
 /// to build up each filter instance using the <see cref="Container"/>
 /// </summary>
 protected virtual void RegisterFilterProviders(FilterProviderCollection providers)
 {
     FilterProviders.Providers.Add(new AdjustableFilterProvider(new InjectionFilterAdjuster(Container)));
 }
 public void RegisterFilterProviders(FilterProviderCollection providers)
 {
     providers.Remove<FilterAttributeFilterProvider>();
     providers.Add(new WindsorFilterProvider(Container));
 }
Exemple #27
0
        public static void Remove <TProvider>(this FilterProviderCollection providers) where TProvider : IFilterProvider
        {
            var provider = FilterProviders.Providers.Single(f => f is TProvider);

            providers.Remove(provider);
        }
 /// <summary>
 /// Replace the default filter attribute filter provider with an instance
 /// of this type.
 /// </summary>
 /// <param name="filterProviders">The global filter provider collection.</param>
 public static void Install(FilterProviderCollection filterProviders)
 {
     if (filterProviders == null) throw new ArgumentNullException("filterProviders");
     filterProviders.Add(new CompositionFilterProvider());
 }
 public ExcludeFilterProvider(IFilterProvider[] filters)
 {
     this.filterProviders = new FilterProviderCollection(filters);
 }
 public ExcludeFilterProvider(IList <IFilterProvider> filters)
 {
     _filterProviders = new FilterProviderCollection(filters);
 }
Exemple #31
0
        public static void RegisterGlobalFilters(FilterProviderCollection filterProviders, GlobalFilterCollection filters, IContainer container)
        {
            filterProviders.Add(new AntiForgeryTokenFilterProvider());

            filters.Add(new ApplicationInsightsHandleErrorAttribute());
        }