예제 #1
0
        public void OnProvidersExecuting_SkipsAddingFilterToActionIfFeatureIsDisabledUsingOptions()
        {
            // Arrange
            var context = GetContext(typeof(SimpleController));
            var options = new TestOptionsManager <ApiBehaviorOptions>(new ApiBehaviorOptions
            {
                EnableModelStateInvalidFilter = false,
            });

            var provider = new ApiControllerApplicationModelProvider(options, NullLoggerFactory.Instance);

            // Act
            provider.OnProvidersExecuting(context);

            // Assert
            Assert.Collection(
                Assert.Single(context.Result.Controllers).Actions.OrderBy(a => a.ActionName),
                action =>
            {
                Assert.DoesNotContain(typeof(ModelStateInvalidFilter), action.Filters.Select(f => f.GetType()));
            },
                action =>
            {
                Assert.DoesNotContain(typeof(ModelStateInvalidFilter), action.Filters.Select(f => f.GetType()));
            });
        }
예제 #2
0
        public void OnProvidersExecuting_AddsModelStateInvalidFilter_IfActionIsAnnotatedWithAttribute()
        {
            // Arrange
            var context = GetContext(typeof(SimpleController));
            var options = new TestOptionsManager <ApiBehaviorOptions>(new ApiBehaviorOptions
            {
                InvalidModelStateResponseFactory = _ => null,
            });

            var provider = new ApiControllerApplicationModelProvider(options, NullLoggerFactory.Instance);

            // Act
            provider.OnProvidersExecuting(context);

            // Assert
            Assert.Collection(
                Assert.Single(context.Result.Controllers).Actions.OrderBy(a => a.ActionName),
                action =>
            {
                Assert.Contains(typeof(ModelStateInvalidFilter), action.Filters.Select(f => f.GetType()));
            },
                action =>
            {
                Assert.DoesNotContain(typeof(ModelStateInvalidFilter), action.Filters.Select(f => f.GetType()));
            });
        }
예제 #3
0
        public void OnProvidersExecuting_DoesNotAddModelStateInvalidFilterToController_IfFeatureIsDisabledViaOptions()
        {
            // Arrange
            var context = GetContext(typeof(TestApiController));
            var options = new TestOptionsManager <ApiBehaviorOptions>(new ApiBehaviorOptions
            {
                EnableModelStateInvalidFilter = false,
            });

            var provider = new ApiControllerApplicationModelProvider(options, NullLoggerFactory.Instance);

            // Act
            provider.OnProvidersExecuting(context);

            // Assert
            var controllerModel = Assert.Single(context.Result.Controllers);

            Assert.DoesNotContain(typeof(ModelStateInvalidFilter), controllerModel.Filters.Select(f => f.GetType()));
        }
예제 #4
0
        public void OnProvidersExecuting_AddsModelStateInvalidFilter_IfTypeIsAnnotatedWithAttribute()
        {
            // Arrange
            var context = GetContext(typeof(TestApiController));
            var options = new TestOptionsManager <ApiBehaviorOptions>(new ApiBehaviorOptions
            {
                InvalidModelStateResponseFactory = _ => null,
            });

            var provider = new ApiControllerApplicationModelProvider(options, NullLoggerFactory.Instance);

            // Act
            provider.OnProvidersExecuting(context);

            // Assert
            var controllerModel = Assert.Single(context.Result.Controllers);

            Assert.IsType <ModelStateInvalidFilter>(controllerModel.Filters.Last());
        }