コード例 #1
0
        public void Constructor()
        {
            var filterInstance = new Mock<IFilter>().Object;

            FilterInfo filter = new FilterInfo(filterInstance, FilterScope.Controller);

            Assert.Equal(FilterScope.Controller, filter.Scope);
            Assert.Same(filterInstance, filter.Instance);
        }
コード例 #2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TableFilterProvider"/> using the provided <see cref="QueryableAttribute"/>
        /// implementation for executing the query. 
        /// </summary>
        public TableFilterProvider(IActionFilter queryFilter)
        {
            if (queryFilter == null)
            {
                throw new ArgumentNullException("queryFilter");
            }

            this.queryFilter = new QueryableAttribute() { PageSize = TableUtils.PageSize };
            this.queryFilterProvider = new QueryFilterProvider(queryFilter);
            this.tableFilter = new FilterInfo(new TableQueryFilter(), FilterScope.Global);
        }
コード例 #3
0
        public static IEnumerable<FilterInfo> CreateFilterTracers(FilterInfo filter, ITraceWriter traceWriter)
        {
            IFilter filterInstance = filter.Instance;
            IEnumerable<IFilter> filterTracers = CreateFilterTracers(filterInstance, traceWriter);
            List<FilterInfo> filters = new List<FilterInfo>();
            foreach (IFilter filterTracer in filterTracers)
            {
                filters.Add(new FilterInfo(filterTracer, filter.Scope));
            }

            return filters;
        }
        public void GetFilterPipeline_Returns_Wrapped_Filters()
        {
            // Arrange
            Mock<IFilter> mockFilter = new Mock<IFilter>();
            FilterInfo filter = new FilterInfo(mockFilter.Object, FilterScope.Global);
            Collection<FilterInfo> filterCollection = new Collection<FilterInfo>(new FilterInfo[] { filter });
            Mock<HttpActionDescriptor> mockActionDescriptor = new Mock<HttpActionDescriptor>() { CallBase = true };
            mockActionDescriptor.Setup(a => a.ActionName).Returns("test");
            mockActionDescriptor.Setup(a => a.GetFilterPipeline()).Returns(filterCollection);
            HttpControllerContext controllerContext = ContextUtil.CreateControllerContext();
            controllerContext.ControllerDescriptor = new HttpControllerDescriptor(controllerContext.Configuration, "test", typeof(ApiController));
            HttpActionDescriptorTracer tracer = new HttpActionDescriptorTracer(controllerContext, mockActionDescriptor.Object, new TestTraceWriter());

            // Act
            Collection<FilterInfo> wrappedFilterCollection = tracer.GetFilterPipeline();

            // Assert
            Assert.IsType<FilterTracer>(wrappedFilterCollection[0].Instance);
        }
コード例 #5
0
        public override Collection<FilterInfo> GetFilterPipeline()
        {
            Collection<FilterInfo> originalFilters = _innerDescriptor.GetFilterPipeline();
            Collection<FilterInfo> newFilters = new Collection<FilterInfo>();

            // for any actions that support query composition, we need to replace it with our
            // query filter.
            foreach (FilterInfo filterInfo in originalFilters)
            {
                FilterInfo newInfo = filterInfo;
                QueryableAttribute queryableFilter = filterInfo.Instance as QueryableAttribute;
                if (queryableFilter != null)
                {
                    newInfo = new FilterInfo(new QueryFilterAttribute() { ResultLimit = queryableFilter.ResultLimit }, filterInfo.Scope);
                }
                newFilters.Add(newInfo);
            }

            return newFilters;
        }
コード例 #6
0
        public void ActionFilters_ReturnsControllerAndBelowActionFilters_WhenOverrideScopeIsController()
        {
            // Arrange
            IActionFilter expectedGlobalFilter = CreateDummyActionFilter();
            FilterInfo globalFilter = new FilterInfo(expectedGlobalFilter, FilterScope.Global);
            IActionFilter expectedControllerFilter = CreateDummyActionFilter();
            FilterInfo controllerFilter = new FilterInfo(expectedControllerFilter, FilterScope.Controller);
            IActionFilter expectedActionFilter = CreateDummyActionFilter();
            FilterInfo actionFilter = new FilterInfo(expectedActionFilter, FilterScope.Action);
            FilterInfo overrideFilter = new FilterInfo(CreateOverride(typeof(IActionFilter)), FilterScope.Controller);
            IEnumerable<FilterInfo> filters = new FilterInfo[] { globalFilter, controllerFilter, actionFilter, overrideFilter };
            FilterGrouping product = CreateProductUnderTest(filters);

            // Act
            IActionFilter[] actionFilters = product.ActionFilters;

            // Assert
            Assert.NotNull(actionFilters);
            Assert.Equal(2, actionFilters.Length);
            Assert.Same(expectedControllerFilter, actionFilters[0]);
            Assert.Same(expectedActionFilter, actionFilters[1]);
        }
コード例 #7
0
        public void CreateFilterTracers_With_IAuthenticationFilter_Returns_Single_Wrapped_IAuthenticationFilter()
        {
            // Arrange
            IAuthenticationFilter expectedInner = new Mock<IAuthenticationFilter>().Object;
            FilterInfo inputFilterInfo = new FilterInfo(expectedInner, FilterScope.Action);
            ITraceWriter expectedTraceWriter = new TestTraceWriter();

            // Act
            IEnumerable<FilterInfo> filters = FilterTracer.CreateFilterTracers(inputFilterInfo, expectedTraceWriter);

            // Assert
            Assert.NotNull(filters);
            Assert.Equal(1, filters.Count());
            FilterInfo filterInfo = filters.Single();
            Assert.NotNull(filterInfo);
            IFilter untypedFilter = filterInfo.Instance;
            Assert.IsType<AuthenticationFilterTracer>(untypedFilter);
            AuthenticationFilterTracer tracer = (AuthenticationFilterTracer)untypedFilter;
            Assert.Same(expectedInner, tracer.InnerFilter);
            Assert.Same(expectedTraceWriter, tracer.TraceWriter);
        }
コード例 #8
0
        public void Compare(FilterInfo x, FilterInfo y, int expectedSign)
        {
            int result = FilterInfoComparer.Instance.Compare(x, y);

            Assert.Equal(expectedSign, Math.Sign(result));
        }
コード例 #9
0
        public void GetFilters_RemovesDuplicateUniqueFiltersKeepingMostSpecificScope()
        {
            // Arrange
            HttpActionDescriptor actionDescriptorMock = new Mock<HttpActionDescriptor>() { CallBase = true }.Object;
            actionDescriptorMock.Configuration = _configurationInstance;

            var multiActionFilter = new FilterInfo(new TestMultiFilter(), FilterScope.Action);
            var multiGlobalFilter = new FilterInfo(new TestMultiFilter(), FilterScope.Global);
            var uniqueControllerFilter = new FilterInfo(new TestUniqueFilter(), FilterScope.Controller);
            var uniqueActionFilter = new FilterInfo(new TestUniqueFilter(), FilterScope.Action);
            Mock<DefaultServices> servicesMock = BuildFilterProvidingServicesMock(
                _configurationInstance, actionDescriptorMock,
                multiActionFilter, multiGlobalFilter, uniqueControllerFilter, uniqueActionFilter);
            _configurationInstance.Services = servicesMock.Object;

            // Act
            var result = actionDescriptorMock.GetFilterPipeline().ToArray();

            // Assert
            Assert.Equal(new[] { multiGlobalFilter, multiActionFilter, uniqueActionFilter }, result);
        }
コード例 #10
0
        public void GetFilters_OrdersFilters()
        {
            // Arrange
            HttpActionDescriptor actionDescriptorMock = new Mock<HttpActionDescriptor>() { CallBase = true }.Object;
            actionDescriptorMock.Configuration = _configurationInstance;

            var globalFilter = new FilterInfo(new TestMultiFilter(), FilterScope.Global);
            var actionFilter = new FilterInfo(new TestMultiFilter(), FilterScope.Action);
            var controllerFilter = new FilterInfo(new TestMultiFilter(), FilterScope.Controller);
            Mock<DefaultServices> servicesMock = BuildFilterProvidingServicesMock(_configurationInstance, actionDescriptorMock, globalFilter, actionFilter, controllerFilter);
            _configurationInstance.Services = servicesMock.Object;

            // Act
            var result = actionDescriptorMock.GetFilterPipeline().ToArray();

            // Assert
            Assert.Equal(new[] { globalFilter, controllerFilter, actionFilter }, result);
        }
コード例 #11
0
        public void FilterImplementingMultipleType_WhereOneTypeIsOverridden_AppearsOnlyInTheOtherList()
        {
            // Arrange
            IFilter expectedInstance = new ActionAndExceptionFilter();
            FilterInfo actionAndExceptionFilter = new FilterInfo(expectedInstance, FilterScope.Global);
            FilterInfo overrideExceptionFilter = new FilterInfo(CreateOverride(typeof(IExceptionFilter)),
                FilterScope.Action);
            IEnumerable<FilterInfo> filters = new FilterInfo[] { actionAndExceptionFilter, overrideExceptionFilter };
            FilterGrouping product = CreateProductUnderTest(filters);

            // Act
            IActionFilter[] actionFilters = product.ActionFilters;
            IExceptionFilter[] exceptionFilters = product.ExceptionFilters;

            // Assert
            Assert.NotNull(actionFilters);
            Assert.Equal(1, actionFilters.Length);
            Assert.Same(expectedInstance, actionFilters[0]);
            Assert.NotNull(exceptionFilters);
            Assert.Equal(0, exceptionFilters.Length);
        }
コード例 #12
0
 /// <summary>
 /// Composes the filters.
 /// </summary>
 /// <param name="filters">The filters.</param>
 private void ComposeFilters(FilterInfo[] filters)
 {
     HeliarCompositionProvider.Current.ComposeParts(filters);
 }
 void ComposeFilters(FilterInfo[] filters)
 {
     CompositionProvider.Current.ComposeParts(filters);
 }
コード例 #14
0
 public CustomFilterInfo(IFilter instance, FilterScope scope)
 {
     Instance = instance;
     Scope = scope;
     FilterInfo = new FilterInfo(Instance, Scope);
 }
コード例 #15
0
 public CustomFilterInfo(FilterInfo filterInfo)
 {
     Instance = filterInfo.Instance;
     Scope = filterInfo.Scope;
     FilterInfo = filterInfo;
 }
コード例 #16
0
        public void CreateFilterTracers_With_AuthorizationFilterAttribute_Returns_Single_Wrapped_Filter()
        {
            // Arrange
            Mock<AuthorizationFilterAttribute> mockFilter = new Mock<AuthorizationFilterAttribute>();
            FilterInfo filter = new FilterInfo(mockFilter.Object, FilterScope.Action);

            // Act
            FilterInfo[] wrappedFilters = FilterTracer.CreateFilterTracers(filter, new TestTraceWriter()).ToArray();

            // Assert
            Assert.Equal(1, wrappedFilters.Length);
            Assert.IsType<AuthorizationFilterAttributeTracer>(wrappedFilters[0].Instance);
        }
コード例 #17
0
        public void CreateFilterTracers_With_All_Filter_Interfaces_Returns_4_Wrapped_Filters()
        {
            // Arrange
            FilterInfo filter = new FilterInfo(new TestFilterAllBehaviors(), FilterScope.Action);

            // Act
            FilterInfo[] wrappedFilters = FilterTracer.CreateFilterTracers(filter, new TestTraceWriter()).ToArray();

            // Assert
            Assert.Equal(4, wrappedFilters.Length);
            Assert.Equal(1, wrappedFilters.Where(f => f.Instance.GetType() == typeof(ActionFilterTracer)).Count());
            Assert.Equal(1, wrappedFilters.Where(f => f.Instance.GetType() == typeof(AuthorizationFilterTracer)).Count());
            Assert.Equal(1, wrappedFilters.Where(f => f.Instance.GetType() == typeof(AuthenticationFilterTracer)).Count());
            Assert.Equal(1, wrappedFilters.Where(f => f.Instance.GetType() == typeof(ExceptionFilterTracer)).Count());
        }
コード例 #18
0
        public void OrderedFilterInfo_ThrowsException_GivenIncorrectType()
        {
            OrderedFilterInfo filter1 = new OrderedFilterInfo(new OrderedFilter1(), FilterScope.Action);
            FilterInfo filter2 = new FilterInfo(new UnorderedFilter(), FilterScope.Action);

            Assert.Throws<ArgumentException>(() =>
            {
                filter1.CompareTo(filter2);
            });
        }