public async Task ModelBinderFactory_BindsPropertyWithoutSupportsGet_WhenRequestIsNotGet() { // Arrange var type = typeof(PageModelWithSupportsGetProperty).GetTypeInfo(); var actionDescriptor = new CompiledPageActionDescriptor { BoundProperties = new[] { new PageBoundPropertyDescriptor() { Name = nameof(PageModelWithSupportsGetProperty.SupportsGet), ParameterType = typeof(string), Property = type.GetProperty(nameof(PageModelWithSupportsGetProperty.SupportsGet)), BindingInfo = new BindingInfo() { RequestPredicate = ((IRequestPredicateProvider) new BindPropertyAttribute() { SupportsGet = true }).RequestPredicate, } }, new PageBoundPropertyDescriptor() { Name = nameof(PageModelWithSupportsGetProperty.Default), ParameterType = typeof(string), Property = type.GetProperty(nameof(PageModelWithSupportsGetProperty.Default)), }, }, HandlerTypeInfo = type, PageTypeInfo = typeof(PageWithProperty).GetTypeInfo(), ModelTypeInfo = type, }; var binder = new TestParameterBinder(new Dictionary <string, object>() { { "SupportsGet", "value" }, { "Default", "value" }, }); var modelMetadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var factory = PagePropertyBinderFactory.CreateBinder(binder, modelMetadataProvider, actionDescriptor); var page = new PageWithProperty { PageContext = GetPageContext() }; page.HttpContext.Request.Method = "Post"; var model = new PageModelWithSupportsGetProperty(); // Act await factory(page.PageContext, model); // Assert Assert.Equal("value", model.SupportsGet); Assert.Equal("value", model.Default); }
public async Task ModelBinderFactory_BindsPropertiesOnPage() { // Arrange var type = typeof(PageWithProperty).GetTypeInfo(); var actionDescriptor = new CompiledPageActionDescriptor { BoundProperties = new [] { new PageBoundPropertyDescriptor() { Name = nameof(PageWithProperty.Id), ParameterType = typeof(int), Property = type.GetProperty(nameof(PageWithProperty.Id)), }, new PageBoundPropertyDescriptor() { Name = nameof(PageWithProperty.RouteDifferentValue), ParameterType = typeof(string), Property = type.GetProperty(nameof(PageWithProperty.RouteDifferentValue)), }, new PageBoundPropertyDescriptor() { Name = nameof(PageWithProperty.PropertyWithNoValue), ParameterType = typeof(string), Property = type.GetProperty(nameof(PageWithProperty.PropertyWithNoValue)), } }, HandlerTypeInfo = type, PageTypeInfo = type, }; var modelMetadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var binder = new TestParameterBinder(new Dictionary <string, object> { { nameof(PageWithProperty.Id), 10 }, { nameof(PageWithProperty.RouteDifferentValue), "route-value" } }); var factory = PagePropertyBinderFactory.CreateBinder(binder, modelMetadataProvider, actionDescriptor); var page = new PageWithProperty { PageContext = new PageContext() { HttpContext = new DefaultHttpContext(), }, }; // Act await factory(page.PageContext, page); // Assert Assert.Equal(10, page.Id); Assert.Equal("route-value", page.RouteDifferentValue); Assert.Null(page.PropertyWithNoValue); }
public async Task ModelBinderFactory_PreservesExistingValueIfModelBindingFailed() { // Arrange var type = typeof(PageModelWithDefaultValue).GetTypeInfo(); var actionDescriptor = new CompiledPageActionDescriptor { BoundProperties = new[] { new PageBoundPropertyDescriptor() { Name = nameof(PageModelWithDefaultValue.PropertyWithDefaultValue), ParameterType = typeof(string), Property = type.GetProperty(nameof(PageModelWithDefaultValue.PropertyWithDefaultValue)), }, }, HandlerTypeInfo = type, PageTypeInfo = typeof(PageWithProperty).GetTypeInfo(), ModelTypeInfo = type, }; var binder = new TestParameterBinder(new Dictionary <string, object>()); var modelMetadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var factory = PagePropertyBinderFactory.CreateBinder(binder, modelMetadataProvider, actionDescriptor); var page = new PageWithProperty { PageContext = new PageContext() { HttpContext = new DefaultHttpContext(), } }; var model = new PageModelWithDefaultValue(); var defaultValue = model.PropertyWithDefaultValue; // Act await factory(page.PageContext, model); // Assert Assert.Equal(defaultValue, model.PropertyWithDefaultValue); }
public void GetModelBinderFactory_ReturnsNullIfPageHasNoVisibleBoundProperties() { // Arrange var actionDescriptor = new CompiledPageActionDescriptor { PageTypeInfo = typeof(PageWithNoVisibleBoundProperties).GetTypeInfo(), }; var modelMetadataProvider = TestModelMetadataProvider.CreateDefaultProvider(); var binder = new ParameterBinder( modelMetadataProvider, TestModelBinderFactory.CreateDefault(), Mock.Of <IObjectModelValidator>()); // Act var factory = PagePropertyBinderFactory.CreateBinder(binder, modelMetadataProvider, actionDescriptor); // Assert Assert.Null(factory); }
private PageActionInvokerCacheEntry CreateCacheEntry( ActionInvokerProviderContext context, FilterItem[] cachedFilters) { var compiledActionDescriptor = (CompiledPageActionDescriptor)context.ActionContext.ActionDescriptor; var viewDataFactory = ViewDataDictionaryFactory.CreateFactory(compiledActionDescriptor.DeclaredModelTypeInfo); var pageFactory = _pageFactoryProvider.CreatePageFactory(compiledActionDescriptor); var pageDisposer = _pageFactoryProvider.CreatePageDisposer(compiledActionDescriptor); var propertyBinder = PagePropertyBinderFactory.CreateBinder( _parameterBinder, _modelMetadataProvider, compiledActionDescriptor); Func <PageContext, object> modelFactory = null; Action <PageContext, object> modelReleaser = null; if (compiledActionDescriptor.ModelTypeInfo != compiledActionDescriptor.PageTypeInfo) { modelFactory = _modelFactoryProvider.CreateModelFactory(compiledActionDescriptor); modelReleaser = _modelFactoryProvider.CreateModelDisposer(compiledActionDescriptor); } var viewStartFactories = GetViewStartFactories(compiledActionDescriptor); var executors = GetExecutors(compiledActionDescriptor); return(new PageActionInvokerCacheEntry( compiledActionDescriptor, viewDataFactory, pageFactory, pageDisposer, modelFactory, modelReleaser, propertyBinder, executors, viewStartFactories, cachedFilters)); }