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

            var provider = new ApiBehaviorApplicationModelProvider(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()));
            });
        }
        public void OnProvidersExecuting_AddsModelStateInvalidFilter_IfActionIsAnnotatedWithAttribute()
        {
            // Arrange
            var context = GetContext(typeof(SimpleController));
            var options = new TestOptionsManager <ApiBehaviorOptions>(new ApiBehaviorOptions
            {
                InvalidModelStateResponseFactory = _ => null,
            });

            var provider = new ApiBehaviorApplicationModelProvider(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()));
            });
        }
        public void OnProvidersExecuting_ThrowsIfControllerWithAttribute_HasActionsWithoutAttributeRouting()
        {
            // Arrange
            var context = GetContext(typeof(ActionsWithoutAttributeRouting));
            var options = new TestOptionsManager <ApiBehaviorOptions>(new ApiBehaviorOptions
            {
                InvalidModelStateResponseFactory = _ => null,
            });

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

            // Act & Assert
            var ex = Assert.Throws <InvalidOperationException>(() => provider.OnProvidersExecuting(context));

            Assert.Equal(
                "Action methods on controllers annotated with ApiControllerAttribute must have an attribute route.",
                ex.Message);
        }
        public void OnProvidersExecuting_DoesNotAddModelStateInvalidFilterToController_IfFeatureIsDisabledViaOptions()
        {
            // Arrange
            var context = GetContext(typeof(TestApiController));
            var options = new TestOptionsManager <ApiBehaviorOptions>(new ApiBehaviorOptions
            {
                EnableModelStateInvalidFilter = false,
            });

            var provider = new ApiBehaviorApplicationModelProvider(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()));
        }
        public void OnProvidersExecuting_AddsModelStateInvalidFilter_IfTypeIsAnnotatedWithAttribute()
        {
            // Arrange
            var context = GetContext(typeof(TestApiController));
            var options = new TestOptionsManager <ApiBehaviorOptions>(new ApiBehaviorOptions
            {
                InvalidModelStateResponseFactory = _ => null,
            });

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

            // Act
            provider.OnProvidersExecuting(context);

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

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