Exemplo n.º 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);
            });
        }
Exemplo n.º 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);
            });
        }
Exemplo n.º 3
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);
        }