Пример #1
0
    public async Task ModelBinderFactory_BindsPropertiesOnPageModel()
    {
        // Arrange
        var type = typeof(PageModelWithProperty).GetTypeInfo();

        var actionDescriptor = new CompiledPageActionDescriptor
        {
            BoundProperties = new[]
            {
                new PageBoundPropertyDescriptor()
                {
                    Name          = nameof(PageModelWithProperty.Id),
                    ParameterType = typeof(int),
                    Property      = type.GetProperty(nameof(PageModelWithProperty.Id)),
                },
                new PageBoundPropertyDescriptor()
                {
                    Name          = nameof(PageModelWithProperty.RouteDifferentValue),
                    ParameterType = typeof(string),
                    Property      = type.GetProperty(nameof(PageModelWithProperty.RouteDifferentValue)),
                },
                new PageBoundPropertyDescriptor()
                {
                    Name          = nameof(PageModelWithProperty.PropertyWithNoValue),
                    ParameterType = typeof(string),
                    Property      = type.GetProperty(nameof(PageModelWithProperty.PropertyWithNoValue)),
                }
            },

            HandlerTypeInfo = typeof(PageModelWithProperty).GetTypeInfo(),
            PageTypeInfo    = typeof(PageWithProperty).GetTypeInfo(),
            ModelTypeInfo   = typeof(PageModelWithProperty).GetTypeInfo(),
        };

        var binder = new TestParameterBinder(new Dictionary <string, object>
        {
            { nameof(PageModelWithProperty.Id), 10 },
            { nameof(PageModelWithProperty.RouteDifferentValue), "route-value" }
        });

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

        var factory = PageBinderFactory.CreatePropertyBinder(binder, modelMetadataProvider, modelBinderFactory, actionDescriptor);

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

        var model = new PageModelWithProperty();

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

        // Assert
        // Verify that the page properties were not bound.
        Assert.Equal(default, page.Id);
Пример #2
0
    private PageActionInvokerCacheEntry CreateCacheEntry(
        CompiledPageActionDescriptor compiledActionDescriptor,
        FilterItem[] cachedFilters)
    {
        var viewDataFactory = ViewDataDictionaryFactory.CreateFactory(compiledActionDescriptor.DeclaredModelTypeInfo);

        var pageFactory    = _pageFactoryProvider.CreatePageFactory(compiledActionDescriptor);
        var pageDisposer   = _pageFactoryProvider.CreateAsyncPageDisposer(compiledActionDescriptor);
        var propertyBinder = PageBinderFactory.CreatePropertyBinder(
            _parameterBinder,
            _modelMetadataProvider,
            _modelBinderFactory,
            compiledActionDescriptor);

        Func <PageContext, object>?           modelFactory  = null;
        Func <PageContext, object, ValueTask>?modelReleaser = null;

        if (compiledActionDescriptor.ModelTypeInfo != compiledActionDescriptor.PageTypeInfo)
        {
            modelFactory  = _modelFactoryProvider.CreateModelFactory(compiledActionDescriptor);
            modelReleaser = _modelFactoryProvider.CreateAsyncModelDisposer(compiledActionDescriptor);
        }

        var viewStartFactories = GetViewStartFactories(compiledActionDescriptor);

        var handlerExecutors = GetHandlerExecutors(compiledActionDescriptor);
        var handlerBinders   = GetHandlerBinders(compiledActionDescriptor);

        return(new PageActionInvokerCacheEntry(
                   compiledActionDescriptor,
                   viewDataFactory,
                   pageFactory,
                   pageDisposer,
                   modelFactory,
                   modelReleaser,
                   propertyBinder,
                   handlerExecutors,
                   handlerBinders,
                   viewStartFactories,
                   cachedFilters));
    }
Пример #3
0
    public void GetModelBinderFactory_ReturnsNullIfPageHasNoVisibleBoundProperties()
    {
        // Arrange
        var actionDescriptor = new CompiledPageActionDescriptor
        {
            PageTypeInfo = typeof(PageWithNoVisibleBoundProperties).GetTypeInfo(),
        };
        var modelMetadataProvider = TestModelMetadataProvider.CreateDefaultProvider();
        var modelBinderFactory    = TestModelBinderFactory.CreateDefault();

        var binder = new ParameterBinder(
            modelMetadataProvider,
            modelBinderFactory,
            Mock.Of <IObjectModelValidator>(),
            _optionsAccessor,
            NullLoggerFactory.Instance);

        // Act
        var factory = PageBinderFactory.CreatePropertyBinder(binder, modelMetadataProvider, modelBinderFactory, actionDescriptor);

        // Assert
        Assert.Same(PageBinderFactory.NullPropertyBinder, factory);
    }
        public async Task BindModelAsync_WithBindPageProperty_EnforcesBindRequired(int?input, bool isValid)
        {
            // Arrange
            var propertyInfo       = typeof(TestPage).GetProperty(nameof(TestPage.BindRequiredProperty));
            var propertyDescriptor = new PageBoundPropertyDescriptor
            {
                BindingInfo = BindingInfo.GetBindingInfo(new[]
                {
                    new FromQueryAttribute {
                        Name = propertyInfo.Name
                    },
                }),
                Name          = propertyInfo.Name,
                ParameterType = propertyInfo.PropertyType,
                Property      = propertyInfo,
            };

            var typeInfo         = typeof(TestPage).GetTypeInfo();
            var actionDescriptor = new CompiledPageActionDescriptor
            {
                BoundProperties = new[] { propertyDescriptor },
                HandlerTypeInfo = typeInfo,
                ModelTypeInfo   = typeInfo,
                PageTypeInfo    = typeInfo,
            };

            var testContext = ModelBindingTestHelper.GetTestContext(request =>
            {
                request.Method = "POST";
                if (input.HasValue)
                {
                    request.QueryString = new QueryString($"?{propertyDescriptor.Name}={input.Value}");
                }
            });

            var modelMetadataProvider = TestModelMetadataProvider.CreateDefaultProvider();
            var parameterBinder       = ModelBindingTestHelper.GetParameterBinder(modelMetadataProvider);
            var modelBinderFactory    = ModelBindingTestHelper.GetModelBinderFactory(modelMetadataProvider);
            var modelMetadata         = modelMetadataProvider
                                        .GetMetadataForProperty(typeof(TestPage), propertyDescriptor.Name);

            var pageBinder = PageBinderFactory.CreatePropertyBinder(
                parameterBinder,
                modelMetadataProvider,
                modelBinderFactory,
                actionDescriptor);
            var pageContext = new PageContext
            {
                ActionDescriptor       = actionDescriptor,
                HttpContext            = testContext.HttpContext,
                RouteData              = testContext.RouteData,
                ValueProviderFactories = testContext.ValueProviderFactories,
            };

            var page = new TestPage();

            // Act
            await pageBinder(pageContext, page);

            // Assert
            Assert.Equal(isValid, pageContext.ModelState.IsValid);
            if (isValid)
            {
                Assert.Equal(input.Value, page.BindRequiredProperty);
            }
        }