示例#1
0
        public async Task CreateHandlerBinder_ValidatesTopLevelParameters()
        {
            // Arrange
            var type             = typeof(PageModelWithExecutors);
            var actionDescriptor = GetActionDescriptorWithHandlerMethod(type, nameof(PageModelWithExecutors.OnPostWithValidation));

            // Act

            var modelMetadataProvider = TestModelMetadataProvider.CreateDefaultProvider();
            var modelBinderFactory    = TestModelBinderFactory.CreateDefault();

            var parameterBinder = new ParameterBinder(
                modelMetadataProvider,
                modelBinderFactory,
                new DefaultObjectValidator(
                    modelMetadataProvider,
                    new[] { TestModelValidatorProvider.CreateDefaultProvider() }),
                NullLoggerFactory.Instance);

            var factory = PageBinderFactory.CreateHandlerBinder(
                parameterBinder,
                modelMetadataProvider,
                modelBinderFactory,
                actionDescriptor,
                actionDescriptor.HandlerMethods[0]);

            var page = new PageWithProperty
            {
                PageContext = GetPageContext()
            };

            var model     = new PageModelWithExecutors();
            var arguments = new Dictionary <string, object>();

            // Act
            await factory(page.PageContext, arguments);

            // Assert
            var modelState = page.PageContext.ModelState;

            Assert.False(modelState.IsValid);
            Assert.Collection(
                page.PageContext.ModelState,
                kvp =>
            {
                Assert.Equal("name", kvp.Key);
            });
        }
示例#2
0
        public async Task CreateHandlerBinder_BindsHandlerParameters()
        {
            // Arrange
            var type             = typeof(PageModelWithExecutors);
            var actionDescriptor = GetActionDescriptorWithHandlerMethod(type, nameof(PageModelWithExecutors.OnGet));

            // Act
            var parameterBinder = new TestParameterBinder(new Dictionary <string, object>()
            {
                { "id", "value" },
            });

            var modelMetadataProvider = TestModelMetadataProvider.CreateDefaultProvider();
            var modelBinderFactory    = TestModelBinderFactory.CreateDefault();

            var factory = PageBinderFactory.CreateHandlerBinder(
                parameterBinder,
                modelMetadataProvider,
                modelBinderFactory,
                actionDescriptor,
                actionDescriptor.HandlerMethods[0]);

            var page = new PageWithProperty
            {
                PageContext = GetPageContext()
            };

            var model     = new PageModelWithExecutors();
            var arguments = new Dictionary <string, object>();

            // Act
            await factory(page.PageContext, arguments);

            // Assert
            Assert.Collection(
                arguments,
                kvp =>
            {
                Assert.Equal("id", kvp.Key);
                Assert.Equal("value", kvp.Value);
            });
        }
示例#3
0
        private PageHandlerBinderDelegate[] GetHandlerBinders(CompiledPageActionDescriptor actionDescriptor)
        {
            if (actionDescriptor.HandlerMethods == null || actionDescriptor.HandlerMethods.Count == 0)
            {
                return(Array.Empty <PageHandlerBinderDelegate>());
            }

            var results = new PageHandlerBinderDelegate[actionDescriptor.HandlerMethods.Count];

            for (var i = 0; i < actionDescriptor.HandlerMethods.Count; i++)
            {
                results[i] = PageBinderFactory.CreateHandlerBinder(
                    _parameterBinder,
                    _modelMetadataProvider,
                    _modelBinderFactory,
                    actionDescriptor,
                    actionDescriptor.HandlerMethods[i]);
            }

            return(results);
        }
示例#4
0
        public async Task CreateHandlerBinder_SkipBindingParametersThatDisallowBinding()
        {
            // Arrange
            var type             = typeof(PageModelWithExecutors);
            var actionDescriptor = GetActionDescriptorWithHandlerMethod(type, nameof(PageModelWithExecutors.OnGetWithBindNever));

            // Act
            var parameterBinder = new TestParameterBinder(new Dictionary <string, object>()
            {
                { "id", "value" },
            });

            var modelMetadataProvider = TestModelMetadataProvider.CreateDefaultProvider();
            var modelBinderFactory    = TestModelBinderFactory.CreateDefault();

            var factory = PageBinderFactory.CreateHandlerBinder(
                parameterBinder,
                modelMetadataProvider,
                modelBinderFactory,
                actionDescriptor,
                actionDescriptor.HandlerMethods[0]);

            var page = new PageWithProperty
            {
                PageContext = GetPageContext()
            };

            var model     = new PageModelWithExecutors();
            var arguments = new Dictionary <string, object>();

            // Act
            await factory(page.PageContext, arguments);

            // Assert
            Assert.Empty(arguments);
        }