public void Load_CreatesEndpoint_WithRoute()
        {
            // Arrange
            var descriptor = new PageActionDescriptor()
            {
                AttributeRouteInfo = new AttributeRouteInfo()
                {
                    Template = "/test",
                },
            };

            var transformer = new Mock <RoutePatternTransformer>();

            transformer
            .Setup(t => t.SubstituteRequiredValues(It.IsAny <RoutePattern>(), It.IsAny <object>()))
            .Returns <RoutePattern, object>((p, v) => p);

            var compilerProvider = GetCompilerProvider();

            var razorPagesOptions = Options.Create(new RazorPagesOptions());
            var mvcOptions        = Options.Create(new MvcOptions());
            var endpointFactory   = new ActionEndpointFactory(transformer.Object);

            var provider = new Mock <IPageApplicationModelProvider>();

            var pageApplicationModel = new PageApplicationModel(descriptor, typeof(object).GetTypeInfo(), Array.Empty <object>());

            provider.Setup(p => p.OnProvidersExecuting(It.IsAny <PageApplicationModelProviderContext>()))
            .Callback((PageApplicationModelProviderContext c) =>
            {
                Assert.Null(c.PageApplicationModel);
                c.PageApplicationModel = pageApplicationModel;
            })
            .Verifiable();

            var providers = new[]
            {
                provider.Object,
            };

            var loader = new DefaultPageLoader(
                providers,
                compilerProvider,
                endpointFactory,
                razorPagesOptions,
                mvcOptions);

            // Act
            var result = loader.Load(descriptor);

            // Assert
            Assert.NotNull(result.Endpoint);
        }
Example #2
0
        public void Load_InvokesApplicationModelProviders_WithTheRightOrder()
        {
            // Arrange
            var descriptor        = new PageActionDescriptor();
            var compilerProvider  = GetCompilerProvider();
            var razorPagesOptions = Options.Create(new RazorPagesOptions());
            var mvcOptions        = Options.Create(new MvcOptions());

            var provider1 = new Mock <IPageApplicationModelProvider>();

            provider1.SetupGet(p => p.Order).Returns(10);
            var provider2 = new Mock <IPageApplicationModelProvider>();

            provider2.SetupGet(p => p.Order).Returns(-5);

            var sequence = 0;

            provider1.Setup(p => p.OnProvidersExecuting(It.IsAny <PageApplicationModelProviderContext>()))
            .Callback((PageApplicationModelProviderContext c) =>
            {
                Assert.Equal(1, sequence++);
                c.PageApplicationModel = new PageApplicationModel(descriptor, typeof(object).GetTypeInfo(), Array.Empty <object>());
            })
            .Verifiable();

            provider2.Setup(p => p.OnProvidersExecuting(It.IsAny <PageApplicationModelProviderContext>()))
            .Callback((PageApplicationModelProviderContext c) =>
            {
                Assert.Equal(0, sequence++);
            })
            .Verifiable();

            provider1.Setup(p => p.OnProvidersExecuted(It.IsAny <PageApplicationModelProviderContext>()))
            .Callback((PageApplicationModelProviderContext c) =>
            {
                Assert.Equal(2, sequence++);
            })
            .Verifiable();

            provider2.Setup(p => p.OnProvidersExecuted(It.IsAny <PageApplicationModelProviderContext>()))
            .Callback((PageApplicationModelProviderContext c) =>
            {
                Assert.Equal(3, sequence++);
            })
            .Verifiable();

            var providers = new[]
            {
                provider1.Object, provider2.Object
            };

            var loader = new DefaultPageLoader(
                providers,
                compilerProvider,
                razorPagesOptions,
                mvcOptions);

            // Act
            var result = loader.Load(new PageActionDescriptor());

            // Assert
            provider1.Verify();
            provider2.Verify();
        }
        public void Load_InvokesApplicationModelProviders()
        {
            // Arrange
            var descriptor = new PageActionDescriptor();

            var compilerProvider = GetCompilerProvider();

            var razorPagesOptions = Options.Create(new RazorPagesOptions());
            var mvcOptions        = Options.Create(new MvcOptions());
            var endpointFactory   = new ActionEndpointFactory(Mock.Of <RoutePatternTransformer>());

            var provider1 = new Mock <IPageApplicationModelProvider>();
            var provider2 = new Mock <IPageApplicationModelProvider>();

            var sequence = 0;
            var pageApplicationModel1 = new PageApplicationModel(descriptor, typeof(object).GetTypeInfo(), Array.Empty <object>());
            var pageApplicationModel2 = new PageApplicationModel(descriptor, typeof(object).GetTypeInfo(), Array.Empty <object>());

            provider1.Setup(p => p.OnProvidersExecuting(It.IsAny <PageApplicationModelProviderContext>()))
            .Callback((PageApplicationModelProviderContext c) =>
            {
                Assert.Equal(0, sequence++);
                Assert.Null(c.PageApplicationModel);
                c.PageApplicationModel = pageApplicationModel1;
            })
            .Verifiable();

            provider2.Setup(p => p.OnProvidersExecuting(It.IsAny <PageApplicationModelProviderContext>()))
            .Callback((PageApplicationModelProviderContext c) =>
            {
                Assert.Equal(1, sequence++);
                Assert.Same(pageApplicationModel1, c.PageApplicationModel);
                c.PageApplicationModel = pageApplicationModel2;
            })
            .Verifiable();

            provider1.Setup(p => p.OnProvidersExecuted(It.IsAny <PageApplicationModelProviderContext>()))
            .Callback((PageApplicationModelProviderContext c) =>
            {
                Assert.Equal(3, sequence++);
                Assert.Same(pageApplicationModel2, c.PageApplicationModel);
            })
            .Verifiable();

            provider2.Setup(p => p.OnProvidersExecuted(It.IsAny <PageApplicationModelProviderContext>()))
            .Callback((PageApplicationModelProviderContext c) =>
            {
                Assert.Equal(2, sequence++);
                Assert.Same(pageApplicationModel2, c.PageApplicationModel);
            })
            .Verifiable();

            var providers = new[]
            {
                provider1.Object, provider2.Object
            };

            var loader = new DefaultPageLoader(
                providers,
                compilerProvider,
                endpointFactory,
                razorPagesOptions,
                mvcOptions);

            // Act
            var result = loader.Load(new PageActionDescriptor());

            // Assert
            provider1.Verify();
            provider2.Verify();
        }