private ControllerActionDescriptorProvider CreateProvider()
        {
            var assemblyProvider = new FixedSetAssemblyProvider();

            assemblyProvider.CandidateAssemblies.Add(GetType().GetTypeInfo().Assembly);
            var controllerTypeProvider = new NamespaceFilteredControllerTypeProvider(assemblyProvider);
            var modelBuilder           = new DefaultControllerModelBuilder(new DefaultActionModelBuilder(null), null);

            var filterProvider = new Mock <IGlobalFilterProvider>();

            filterProvider
            .SetupGet(fp => fp.Filters)
            .Returns(new List <IFilter>());

            var options = new MvcOptions();

            var setup = new WebApiCompatShimOptionsSetup();

            setup.Configure(options);

            var optionsAccessor = new Mock <IOptions <MvcOptions> >();

            optionsAccessor
            .SetupGet(o => o.Options)
            .Returns(options);

            var provider = new ControllerActionDescriptorProvider(
                controllerTypeProvider,
                modelBuilder,
                filterProvider.Object,
                optionsAccessor.Object);

            return(provider);
        }
        public void BuildControllerModel_AuthorizeAttributeAddsAuthorizeFilter()
        {
            // Arrange
            var builder = new DefaultControllerModelBuilder(new DefaultActionModelBuilder(null), null);
            var typeInfo = typeof(AccountController).GetTypeInfo();

            // Act
            var model = builder.BuildControllerModel(typeInfo);

            // Assert
            Assert.True(model.Filters.Any(f => f is AuthorizeFilter));
        }
        public void BuildControllerModel_DerivedFromControllerClass_HasFilter()
        {
            // Arrange
            var builder = new DefaultControllerModelBuilder(new DefaultActionModelBuilder(null), null);
            var typeInfo = typeof(StoreController).GetTypeInfo();

            // Act
            var model = builder.BuildControllerModel(typeInfo);

            // Assert
            var filter = Assert.Single(model.Filters);
            Assert.IsType<ControllerActionFilter>(filter);
        }
Exemplo n.º 4
0
        private ControllerActionDescriptorProvider GetActionDescriptorProvider()
        {
            var controllerTypes = typeof(DefaultActionSelectorTests)
                                  .GetNestedTypes(BindingFlags.NonPublic)
                                  .Select(t => t.GetTypeInfo())
                                  .ToList();

            var controllerTypeProvider = new FixedSetControllerTypeProvider(controllerTypes);
            var modelBuilder           = new DefaultControllerModelBuilder(new DefaultActionModelBuilder(null), null);

            return(new ControllerActionDescriptorProvider(
                       controllerTypeProvider,
                       modelBuilder,
                       new TestGlobalFilterProvider(),
                       new MockMvcOptionsAccessor()));
        }
Exemplo n.º 5
0
        private ControllerActionDescriptorProvider GetProvider(
            ILoggerFactory loggerFactory, params TypeInfo[] controllerTypeInfo)
        {
            var controllerTypeProvider = new FixedSetControllerTypeProvider(controllerTypeInfo);
            var modelBuilder           = new DefaultControllerModelBuilder(new DefaultActionModelBuilder(null),
                                                                           loggerFactory,
                                                                           null);

            var provider = new ControllerActionDescriptorProvider(
                controllerTypeProvider,
                modelBuilder,
                new TestGlobalFilterProvider(),
                new MockMvcOptionsAccessor(),
                loggerFactory);

            return(provider);
        }
        public void BuildControllerModel_AddsControllerProperties()
        {
            // Arrange
            var builder = new DefaultControllerModelBuilder(new DefaultActionModelBuilder(null), null);
            var typeInfo = typeof(ModelBinderController).GetTypeInfo();

            // Act
            var model = builder.BuildControllerModel(typeInfo);

            // Assert
            Assert.Equal(2, model.ControllerProperties.Count);
            Assert.Equal("Bound", model.ControllerProperties[0].PropertyName);
            Assert.Equal(BindingSource.Query, model.ControllerProperties[0].BindingInfo.BindingSource);
            Assert.NotNull(model.ControllerProperties[0].Controller);
            var attribute = Assert.Single(model.ControllerProperties[0].Attributes);
            Assert.IsType<FromQueryAttribute>(attribute);
        }
        public void BuildControllerModel_EnableCorsAttributeAddsCorsAuthorizationFilterFactory()
        {
            // Arrange
            var corsOptions = new CorsOptions();
            corsOptions.AddPolicy("policy", new CorsPolicy());
            var mockOptions = new Mock<IOptions<CorsOptions>>();
            mockOptions.SetupGet(o => o.Options)
                       .Returns(corsOptions);
            var builder = new DefaultControllerModelBuilder(new DefaultActionModelBuilder(null),
                authorizationOptions: null);
            var typeInfo = typeof(CorsController).GetTypeInfo();

            // Act
            var model = builder.BuildControllerModel(typeInfo);

            // Assert
            Assert.Single(model.Filters, f => f is CorsAuthorizationFilterFactory);
        }
        private ControllerActionDescriptorProvider GetProvider(
            TypeInfo controllerTypeInfo,
            IEnumerable<IFilter> filters = null)
        {
            var controllerTypeProvider = new FixedSetControllerTypeProvider(new[] { controllerTypeInfo });
            var controllerModelBuilder = new DefaultControllerModelBuilder(new DefaultActionModelBuilder(null),
                                                                           NullLoggerFactory.Instance,
                                                                           null);

            var provider = new ControllerActionDescriptorProvider(
                controllerTypeProvider,
                controllerModelBuilder,
                new TestGlobalFilterProvider(filters),
                new MockMvcOptionsAccessor(),
                new NullLoggerFactory());

            return provider;
        }
        private ControllerActionDescriptorProvider GetActionDescriptorProvider()
        {
            var controllerTypes = typeof(DefaultActionSelectorTests)
                .GetNestedTypes(BindingFlags.NonPublic)
                .Select(t => t.GetTypeInfo())
                .ToList();

            var controllerTypeProvider = new FixedSetControllerTypeProvider(controllerTypes);
            var modelBuilder = new DefaultControllerModelBuilder(new DefaultActionModelBuilder(null),
                                                                 NullLoggerFactory.Instance,
                                                                 null);

            return new ControllerActionDescriptorProvider(
                                        controllerTypeProvider,
                                        modelBuilder,
                                        new TestGlobalFilterProvider(),
                                        new MockMvcOptionsAccessor(),
                                        new NullLoggerFactory());
        }
        private ControllerActionDescriptorProvider CreateProvider()
        {
            var assemblyProvider = new FixedSetAssemblyProvider();
            assemblyProvider.CandidateAssemblies.Add(GetType().GetTypeInfo().Assembly);
            var controllerTypeProvider = new NamespaceFilteredControllerTypeProvider(assemblyProvider);
            var modelBuilder = new DefaultControllerModelBuilder(new DefaultActionModelBuilder(null), null);

            var filterProvider = new Mock<IGlobalFilterProvider>();
            filterProvider
                .SetupGet(fp => fp.Filters)
                .Returns(new List<IFilter>());

            var options = new MvcOptions();

            var setup = new WebApiCompatShimOptionsSetup();
            setup.Configure(options);

            var optionsAccessor = new Mock<IOptions<MvcOptions>>();
            optionsAccessor
                .SetupGet(o => o.Options)
                .Returns(options);

            var provider = new ControllerActionDescriptorProvider(
                controllerTypeProvider,
                modelBuilder,
                filterProvider.Object,
                optionsAccessor.Object);

            return provider;
        }
        private ControllerActionDescriptorProvider GetProvider(
            ILoggerFactory loggerFactory, params TypeInfo[] controllerTypeInfo)
        {
            var controllerTypeProvider = new FixedSetControllerTypeProvider(controllerTypeInfo);
            var modelBuilder = new DefaultControllerModelBuilder(new DefaultActionModelBuilder(null),
                                                                 loggerFactory,
                                                                 null);

            var provider = new ControllerActionDescriptorProvider(
                controllerTypeProvider,
                modelBuilder,
                new TestGlobalFilterProvider(),
                new MockMvcOptionsAccessor(),
                loggerFactory);

            return provider;
        }
        private ControllerActionDescriptorProvider GetProvider(
            TypeInfo type,
            IApplicationModelConvention convention)
        {
            var controllerTypeProvider = new FixedSetControllerTypeProvider(new[] { type });
            var modelBuilder = new DefaultControllerModelBuilder(new DefaultActionModelBuilder(null),
                                                                 NullLoggerFactory.Instance,
                                                                 null);

            var options = new MockMvcOptionsAccessor();
            options.Options.Conventions.Add(convention);

            return new ControllerActionDescriptorProvider(
                controllerTypeProvider,
                modelBuilder,
                new TestGlobalFilterProvider(),
                options,
                new NullLoggerFactory());
        }
        public void BuildControllerModel_ClassWithHiddenInheritedRoutes()
        {
            // Arrange
            var builder = new DefaultControllerModelBuilder(new DefaultActionModelBuilder(null), null);
            var typeInfo = typeof(DerivedClassHidingRoutesController).GetTypeInfo();

            // Act
            var model = builder.BuildControllerModel(typeInfo);

            // Assert
            Assert.Equal(2, model.AttributeRoutes.Count);
            Assert.Equal(2, model.Attributes.Count);

            var route = Assert.Single(model.AttributeRoutes, r => r.Template == "C");
            Assert.Contains(route.Attribute, model.Attributes);

            route = Assert.Single(model.AttributeRoutes, r => r.Template == "D");
            Assert.Contains(route.Attribute, model.Attributes);
        }
        public void BuildControllerModel_ClassWithFilterInterfaces_UnsupportedType()
        {
            // Arrange
            var builder = new DefaultControllerModelBuilder(new DefaultActionModelBuilder(null), null);
            var typeInfo = typeof(UnsupportedFiltersController).GetTypeInfo();

            // Act
            var model = builder.BuildControllerModel(typeInfo);

            // Assert
            Assert.Empty(model.Filters);
        }
        public void BuildControllerModel_ClassWithFilterInterfaces_HasFilter()
        {
            // Arrange
            var builder = new DefaultControllerModelBuilder(new DefaultActionModelBuilder(null), null);
            var typeInfo = typeof(SomeFiltersController).GetTypeInfo();

            // Act
            var model = builder.BuildControllerModel(typeInfo);

            // Assert
            Assert.Single(model.Filters, f => f is ControllerActionFilter);
            Assert.Single(model.Filters, f => f is ControllerResultFilter);
        }
        public void BuildControllerModel_ClassWithoutFilterInterfaces_HasNoControllerFilter()
        {
            // Arrange
            var builder = new DefaultControllerModelBuilder(new DefaultActionModelBuilder(null), null);
            var typeInfo = typeof(NoFiltersController).GetTypeInfo();

            // Act
            var model = builder.BuildControllerModel(typeInfo);

            // Assert
            var filter = Assert.Single(model.Filters);
            Assert.IsType<ProducesAttribute>(filter);
        }
        private IEnumerable<ActionDescriptor> GetDescriptors(params TypeInfo[] controllerTypeInfos)
        {
            var controllerTypeProvider = new FixedSetControllerTypeProvider(controllerTypeInfos);
            var modelBuilder = new DefaultControllerModelBuilder(new DefaultActionModelBuilder(null),
                                                                 NullLoggerFactory.Instance,
                                                                 null);

            var provider = new ControllerActionDescriptorProvider(
                controllerTypeProvider,
                modelBuilder,
                new TestGlobalFilterProvider(),
                new MockMvcOptionsAccessor(),
                new NullLoggerFactory());

            return provider.GetDescriptors();
        }