Exemple #1
0
    public void CreateDescriptor_CopiesPropertiesFromPageActionDescriptor()
    {
        // Arrange
        var actionDescriptor = new PageActionDescriptor
        {
            ActionConstraints  = new List <IActionConstraintMetadata>(),
            AttributeRouteInfo = new AttributeRouteInfo(),
            EndpointMetadata   = new List <object>(),
            FilterDescriptors  = new List <FilterDescriptor>(),
            RelativePath       = "/Foo",
            RouteValues        = new Dictionary <string, string>(),
            ViewEnginePath     = "/Pages/Foo",
        };
        var handlerTypeInfo      = typeof(object).GetTypeInfo();
        var pageApplicationModel = new PageApplicationModel(actionDescriptor, handlerTypeInfo, new object[0]);
        var globalFilters        = new FilterCollection();

        // Act
        var actual = CompiledPageActionDescriptorBuilder.Build(pageApplicationModel, globalFilters);

        // Assert
        Assert.Same(actionDescriptor.ActionConstraints, actual.ActionConstraints);
        Assert.Same(actionDescriptor.AttributeRouteInfo, actual.AttributeRouteInfo);
        Assert.Same(actionDescriptor.RelativePath, actual.RelativePath);
        Assert.Same(actionDescriptor.RouteValues, actual.RouteValues);
        Assert.Same(actionDescriptor.ViewEnginePath, actual.ViewEnginePath);
    }
Exemple #2
0
    public void CreateDescriptor_CombinesEndpointMetadataFromHandlerTypeAttributesAndAttributesOnModel()
    {
        // Arrange
        var metadata1 = "metadata1";
        var metadata2 = "metadata2";
        var metadata3 = "metadata3";
        var metadata4 = "metadata4";
        var metadata5 = "metadata5";
        var metadata6 = "metadata6";

        var actionDescriptor = new PageActionDescriptor
        {
            ActionConstraints  = new List <IActionConstraintMetadata>(),
            AttributeRouteInfo = new AttributeRouteInfo(),
            EndpointMetadata   = new List <object> {
                metadata3, metadata4,
            },
            FilterDescriptors = new List <FilterDescriptor>(),
            RelativePath      = "/Foo",
            RouteValues       = new Dictionary <string, string>(),
            ViewEnginePath    = "/Pages/Foo",
        };
        var handlerTypeInfo      = typeof(object).GetTypeInfo();
        var pageApplicationModel = new PageApplicationModel(actionDescriptor, handlerTypeInfo, new[] { metadata1, metadata2, });

        pageApplicationModel.EndpointMetadata.Add(metadata5);
        pageApplicationModel.EndpointMetadata.Add(metadata6);
        var globalFilters = new FilterCollection();

        // Act
        var actual = CompiledPageActionDescriptorBuilder.Build(pageApplicationModel, globalFilters);

        // Assert
        Assert.Equal(new[] { metadata1, metadata2, metadata3, metadata4, metadata5, metadata6 }, actual.EndpointMetadata);
    }
Exemple #3
0
        public CompiledPageActionDescriptor Load(PageActionDescriptor actionDescriptor)
        {
            if (actionDescriptor == null)
            {
                throw new ArgumentNullException(nameof(actionDescriptor));
            }

            var compileTask    = Compiler.CompileAsync(actionDescriptor.RelativePath);
            var viewDescriptor = compileTask.GetAwaiter().GetResult();

            var context = new PageApplicationModelProviderContext(actionDescriptor, viewDescriptor.Type.GetTypeInfo());

            for (var i = 0; i < _applicationModelProviders.Length; i++)
            {
                _applicationModelProviders[i].OnProvidersExecuting(context);
            }

            for (var i = _applicationModelProviders.Length - 1; i >= 0; i--)
            {
                _applicationModelProviders[i].OnProvidersExecuted(context);
            }

            ApplyConventions(_conventions, context.PageApplicationModel);

            return(CompiledPageActionDescriptorBuilder.Build(context.PageApplicationModel, _globalFilters));
        }
Exemple #4
0
        private async Task <CompiledPageActionDescriptor> LoadAsyncCore(PageActionDescriptor actionDescriptor)
        {
            var viewDescriptor = await Compiler.CompileAsync(actionDescriptor.RelativePath);

            var context = new PageApplicationModelProviderContext(actionDescriptor, viewDescriptor.Type.GetTypeInfo());

            for (var i = 0; i < _applicationModelProviders.Length; i++)
            {
                _applicationModelProviders[i].OnProvidersExecuting(context);
            }

            for (var i = _applicationModelProviders.Length - 1; i >= 0; i--)
            {
                _applicationModelProviders[i].OnProvidersExecuted(context);
            }

            ApplyConventions(_conventions, context.PageApplicationModel);

            var compiled = CompiledPageActionDescriptorBuilder.Build(context.PageApplicationModel, _globalFilters);

            // We need to create an endpoint for routing to use and attach it to the CompiledPageActionDescriptor...
            // routing for pages is two-phase. First we perform routing using the route info - we can do this without
            // compiling/loading the page. Then once we have a match we load the page and we can create an endpoint
            // with all of the information we get from the compiled action descriptor.
            var endpoints = new List <Endpoint>();

            _endpointFactory.AddEndpoints(endpoints, compiled, Array.Empty <ConventionalRouteEntry>(), Array.Empty <Action <EndpointBuilder> >());

            // In some test scenarios there's no route so the endpoint isn't created. This is fine because
            // it won't happen for real.
            compiled.Endpoint = endpoints.SingleOrDefault();

            return(compiled);
        }
        private async Task <CompiledPageActionDescriptor> LoadAsyncCore(PageActionDescriptor actionDescriptor, EndpointMetadataCollection endpointMetadata)
        {
            var viewDescriptor = await Compiler.CompileAsync(actionDescriptor.RelativePath);

            var context = new PageApplicationModelProviderContext(actionDescriptor, viewDescriptor.Type.GetTypeInfo());

            for (var i = 0; i < _applicationModelProviders.Length; i++)
            {
                _applicationModelProviders[i].OnProvidersExecuting(context);
            }

            for (var i = _applicationModelProviders.Length - 1; i >= 0; i--)
            {
                _applicationModelProviders[i].OnProvidersExecuted(context);
            }

            ApplyConventions(_conventions, context.PageApplicationModel);

            var compiled = CompiledPageActionDescriptorBuilder.Build(context.PageApplicationModel, _globalFilters);

            // We need to create an endpoint for routing to use and attach it to the CompiledPageActionDescriptor...
            // routing for pages is two-phase. First we perform routing using the route info - we can do this without
            // compiling/loading the page. Then once we have a match we load the page and we can create an endpoint
            // with all of the information we get from the compiled action descriptor.
            var endpoints = new List <Endpoint>();

            _endpointFactory.AddEndpoints(
                endpoints,
                routeNames: new HashSet <string>(StringComparer.OrdinalIgnoreCase),
                action: compiled,
                routes: Array.Empty <ConventionalRouteEntry>(),
                conventions: new Action <EndpointBuilder>[]
            {
                b =>
                {
                    // Metadata from PageActionDescriptor is less significant than the one discovered from the compiled type.
                    // Consequently, we'll insert it at the beginning.
                    for (var i = endpointMetadata.Count - 1; i >= 0; i--)
                    {
                        b.Metadata.Insert(0, endpointMetadata[i]);
                    }
                },
            },
                createInertEndpoints: false);

            // In some test scenarios there's no route so the endpoint isn't created. This is fine because
            // it won't happen for real.
            compiled.Endpoint = endpoints.SingleOrDefault();

            return(compiled);
        }
Exemple #6
0
    public void CreateDescriptor_CopiesPropertiesFromPageApplicationModel()
    {
        // Arrange
        var actionDescriptor = new PageActionDescriptor
        {
            ActionConstraints  = new List <IActionConstraintMetadata>(),
            AttributeRouteInfo = new AttributeRouteInfo(),
            FilterDescriptors  = new List <FilterDescriptor>(),
            RelativePath       = "/Foo",
            RouteValues        = new Dictionary <string, string>(),
            ViewEnginePath     = "/Pages/Foo",
        };
        var handlerTypeInfo      = typeof(TestModel).GetTypeInfo();
        var pageApplicationModel = new PageApplicationModel(actionDescriptor, typeof(TestModel).GetTypeInfo(), handlerTypeInfo, new object[0])
        {
            PageType  = typeof(TestPage).GetTypeInfo(),
            ModelType = typeof(TestModel).GetTypeInfo(),
            Filters   =
            {
                Mock.Of <IFilterMetadata>(),
                Mock.Of <IFilterMetadata>(),
            },
            HandlerMethods =
            {
                new PageHandlerModel(handlerTypeInfo.GetMethod(nameof(TestModel.OnGet)), new object[0]),
            },
            HandlerProperties =
            {
                new PagePropertyModel(handlerTypeInfo.GetProperty(nameof(TestModel.Property)), new object[0])
                {
                    BindingInfo = new BindingInfo(),
                },
            }
        };
        var globalFilters = new FilterCollection();

        // Act
        var actual = CompiledPageActionDescriptorBuilder.Build(pageApplicationModel, globalFilters);

        // Assert
        Assert.Same(pageApplicationModel.PageType, actual.PageTypeInfo);
        Assert.Same(pageApplicationModel.DeclaredModelType, actual.DeclaredModelTypeInfo);
        Assert.Same(pageApplicationModel.ModelType, actual.ModelTypeInfo);
        Assert.Same(pageApplicationModel.HandlerType, actual.HandlerTypeInfo);
        Assert.Same(pageApplicationModel.Properties, actual.Properties);
        Assert.Equal(pageApplicationModel.Filters, actual.FilterDescriptors.Select(f => f.Filter));
        Assert.Equal(pageApplicationModel.HandlerMethods.Select(p => p.MethodInfo), actual.HandlerMethods.Select(p => p.MethodInfo));
        Assert.Equal(pageApplicationModel.HandlerProperties.Select(p => p.PropertyName), actual.BoundProperties.Select(p => p.Name));
    }
Exemple #7
0
    public void CreateDescriptor_AddsGlobalFiltersWithTheRightScope()
    {
        // Arrange
        var actionDescriptor = new PageActionDescriptor
        {
            ActionConstraints  = new List <IActionConstraintMetadata>(),
            AttributeRouteInfo = new AttributeRouteInfo(),
            FilterDescriptors  = new List <FilterDescriptor>(),
            RelativePath       = "/Foo",
            RouteValues        = new Dictionary <string, string>(),
            ViewEnginePath     = "/Pages/Foo",
        };
        var handlerTypeInfo      = typeof(TestModel).GetTypeInfo();
        var pageApplicationModel = new PageApplicationModel(actionDescriptor, handlerTypeInfo, new object[0])
        {
            PageType  = typeof(TestPage).GetTypeInfo(),
            ModelType = typeof(TestModel).GetTypeInfo(),
            Filters   =
            {
                Mock.Of <IFilterMetadata>(),
            },
        };
        var globalFilters = new FilterCollection
        {
            Mock.Of <IFilterMetadata>(),
        };

        // Act
        var compiledPageActionDescriptor = CompiledPageActionDescriptorBuilder.Build(pageApplicationModel, globalFilters);

        // Assert
        Assert.Collection(
            compiledPageActionDescriptor.FilterDescriptors,
            filterDescriptor =>
        {
            Assert.Same(globalFilters[0], filterDescriptor.Filter);
            Assert.Equal(FilterScope.Global, filterDescriptor.Scope);
        },
            filterDescriptor =>
        {
            Assert.Same(pageApplicationModel.Filters[0], filterDescriptor.Filter);
            Assert.Equal(FilterScope.Action, filterDescriptor.Scope);
        });
    }
Exemple #8
0
    public void CreateDescriptor_ThrowsIfModelIsNotCompatibleWithDeclaredModel()
    {
        // Arrange
        var actionDescriptor = new PageActionDescriptor
        {
            ActionConstraints  = new List <IActionConstraintMetadata>(),
            AttributeRouteInfo = new AttributeRouteInfo(),
            FilterDescriptors  = new List <FilterDescriptor>(),
            RelativePath       = "/Foo",
            RouteValues        = new Dictionary <string, string>(),
            ViewEnginePath     = "/Pages/Foo",
        };
        var handlerTypeInfo      = typeof(TestModel).GetTypeInfo();
        var pageApplicationModel = new PageApplicationModel(actionDescriptor, typeof(TestModel).GetTypeInfo(), handlerTypeInfo, new object[0])
        {
            PageType  = typeof(TestPage).GetTypeInfo(),
            ModelType = typeof(string).GetTypeInfo(),
            Filters   =
            {
                Mock.Of <IFilterMetadata>(),
                Mock.Of <IFilterMetadata>(),
            },
            HandlerMethods =
            {
                new PageHandlerModel(handlerTypeInfo.GetMethod(nameof(TestModel.OnGet)), new object[0]),
            },
            HandlerProperties =
            {
                new PagePropertyModel(handlerTypeInfo.GetProperty(nameof(TestModel.Property)), new object[0])
                {
                    BindingInfo = new BindingInfo(),
                },
            }
        };
        var globalFilters = new FilterCollection();

        // Act & Assert
        var actual = Assert.Throws <InvalidOperationException>(() =>
                                                               CompiledPageActionDescriptorBuilder.Build(pageApplicationModel, globalFilters));
    }
        public CompiledPageActionDescriptor CreateCompiledDescriptor(
            PageActionDescriptor actionDescriptor,
            CompiledViewDescriptor viewDescriptor)
        {
            var context = new PageApplicationModelProviderContext(actionDescriptor, viewDescriptor.Type.GetTypeInfo());

            for (var i = 0; i < _applicationModelProviders.Length; i++)
            {
                _applicationModelProviders[i].OnProvidersExecuting(context);
            }

            for (var i = _applicationModelProviders.Length - 1; i >= 0; i--)
            {
                _applicationModelProviders[i].OnProvidersExecuted(context);
            }

            ApplyConventions(_conventions, context.PageApplicationModel);

            var compiled = CompiledPageActionDescriptorBuilder.Build(context.PageApplicationModel, _globalFilters);

            actionDescriptor.CompiledPageDescriptor = compiled;

            return(compiled);
        }