Esempio n. 1
0
    private protected ActionEndpointDataSourceBase CreateDataSource(IActionDescriptorCollectionProvider actions = null)
    {
        if (actions == null)
        {
            actions = new DefaultActionDescriptorCollectionProvider(
                Array.Empty <IActionDescriptorProvider>(),
                Array.Empty <IActionDescriptorChangeProvider>());
        }

        var services = new ServiceCollection();

        services.AddSingleton(actions);

        var routeOptionsSetup = new MvcCoreRouteOptionsSetup();

        services.Configure <RouteOptions>(routeOptionsSetup.Configure);
        services.AddRouting(options =>
        {
            options.ConstraintMap["upper-case"] = typeof(UpperCaseParameterTransform);
        });

        var serviceProvider = services.BuildServiceProvider();

        var endpointFactory = new ActionEndpointFactory(serviceProvider.GetRequiredService <RoutePatternTransformer>(), Enumerable.Empty <IRequestDelegateFactory>());

        return(CreateDataSource(actions, endpointFactory));
    }
        private ActionConstraintMatcherPolicy CreateSelector(ActionDescriptor[] actions)
        {
            // We need to actually provide some actions with some action constraints metadata
            // or else the policy will No-op.
            var actionDescriptorProvider = new Mock <IActionDescriptorProvider>();

            actionDescriptorProvider
            .Setup(a => a.OnProvidersExecuted(It.IsAny <ActionDescriptorProviderContext>()))
            .Callback <ActionDescriptorProviderContext>(c =>
            {
                for (var i = 0; i < actions.Length; i++)
                {
                    c.Results.Add(actions[i]);
                }
            });

            var actionDescriptorCollectionProvider = new DefaultActionDescriptorCollectionProvider(
                new IActionDescriptorProvider[] { actionDescriptorProvider.Object, },
                Enumerable.Empty <IActionDescriptorChangeProvider>());

            var cache = new ActionConstraintCache(actionDescriptorCollectionProvider, new[]
            {
                new DefaultActionConstraintProvider(),
            });

            return(new ActionConstraintMatcherPolicy(cache));
        }
    public void ActionDescriptors_ReadsDescriptorsFromActionDescriptorProviders()
    {
        // Arrange
        var expected1 = new ActionDescriptor();
        var actionDescriptorProvider1 = GetActionDescriptorProvider(expected1);

        var expected2 = new ActionDescriptor();
        var expected3 = new ActionDescriptor();
        var actionDescriptorProvider2 = GetActionDescriptorProvider(expected2, expected3);

        var actionDescriptorCollectionProvider = new DefaultActionDescriptorCollectionProvider(
            new[] { actionDescriptorProvider1, actionDescriptorProvider2 },
            Enumerable.Empty <IActionDescriptorChangeProvider>());

        // Act
        var collection = actionDescriptorCollectionProvider.ActionDescriptors;

        // Assert
        Assert.Equal(0, collection.Version);
        Assert.Collection(
            collection.Items,
            descriptor => Assert.Same(expected1, descriptor),
            descriptor => Assert.Same(expected2, descriptor),
            descriptor => Assert.Same(expected3, descriptor));
    }
        private static ActionConstraintCache CreateCache(params IActionConstraintProvider[] providers)
        {
            var descriptorProvider = new DefaultActionDescriptorCollectionProvider(
                Enumerable.Empty <IActionDescriptorProvider>(),
                Enumerable.Empty <IActionDescriptorChangeProvider>());

            return(new ActionConstraintCache(descriptorProvider, providers));
        }
Esempio n. 5
0
        private static ActionConstraintCache GetActionConstraintCache(IActionConstraintProvider[] actionConstraintProviders = null)
        {
            var descriptorProvider = new DefaultActionDescriptorCollectionProvider(
                Enumerable.Empty <IActionDescriptorProvider>(),
                Enumerable.Empty <IActionDescriptorChangeProvider>());

            return(new ActionConstraintCache(descriptorProvider, actionConstraintProviders.AsEnumerable() ?? new List <IActionConstraintProvider>()));
        }
Esempio n. 6
0
        public ControllerActionDescriptor InvokeActionSelector(RouteContext context)
        {
            var actionDescriptorProvider           = GetActionDescriptorProvider();
            var actionDescriptorCollectionProvider = new DefaultActionDescriptorCollectionProvider(
                new[] { actionDescriptorProvider },
                Enumerable.Empty <IActionDescriptorChangeProvider>());

            var actionConstraintProviders = new[]
            {
                new DefaultActionConstraintProvider()
            };

            var actionSelector = CreateFinerGrainedActionSelector(actionDescriptorCollectionProvider, actionConstraintProviders);

            var candidates = actionSelector.SelectCandidates(context);

            return((ControllerActionDescriptor)actionSelector.SelectBestCandidate(context, candidates));
        }
Esempio n. 7
0
    private ControllerActionDescriptor InvokeActionSelector(RouteContext context)
    {
        var actionDescriptorProvider           = GetActionDescriptorProvider();
        var actionDescriptorCollectionProvider = new DefaultActionDescriptorCollectionProvider(
            new[] { actionDescriptorProvider },
            Enumerable.Empty <IActionDescriptorChangeProvider>());

        var actionConstraintProviders = new[]
        {
            new DefaultActionConstraintProvider(),
        };

        var actionSelector = new ActionSelector(
            actionDescriptorCollectionProvider,
            GetActionConstraintCache(actionConstraintProviders),
            NullLoggerFactory.Instance);

        var candidates = actionSelector.SelectCandidates(context);

        return((ControllerActionDescriptor)actionSelector.SelectBestCandidate(context, candidates));
    }
Esempio n. 8
0
        private MvcEndpointDataSource CreateMvcEndpointDataSource(
            IActionDescriptorCollectionProvider actionDescriptorCollectionProvider = null,
            MvcEndpointInvokerFactory mvcEndpointInvokerFactory = null)
        {
            if (actionDescriptorCollectionProvider == null)
            {
                actionDescriptorCollectionProvider = new DefaultActionDescriptorCollectionProvider(
                    Array.Empty <IActionDescriptorProvider>(),
                    Array.Empty <IActionDescriptorChangeProvider>());
            }

            var services = new ServiceCollection();

            services.AddSingleton(actionDescriptorCollectionProvider);

            var dataSource = new MvcEndpointDataSource(
                actionDescriptorCollectionProvider,
                mvcEndpointInvokerFactory ?? new MvcEndpointInvokerFactory(new ActionInvokerFactory(Array.Empty <IActionInvokerProvider>())));

            return(dataSource);
        }
        private MvcEndpointDataSource CreateMvcEndpointDataSource(
            IActionDescriptorCollectionProvider actionDescriptorCollectionProvider = null,
            MvcEndpointInvokerFactory mvcEndpointInvokerFactory = null)
        {
            if (actionDescriptorCollectionProvider == null)
            {
                actionDescriptorCollectionProvider = new DefaultActionDescriptorCollectionProvider(
                    Array.Empty <IActionDescriptorProvider>(),
                    Array.Empty <IActionDescriptorChangeProvider>());
            }

            var services = new ServiceCollection();

            services.AddSingleton(actionDescriptorCollectionProvider);

            var routeOptionsSetup = new MvcCoreRouteOptionsSetup();

            services.Configure <RouteOptions>(routeOptionsSetup.Configure);
            services.AddRouting(options =>
            {
                options.ConstraintMap["upper-case"] = typeof(UpperCaseParameterTransform);
            });

            var serviceProvider = services.BuildServiceProvider();

            var dataSource = new MvcEndpointDataSource(
                actionDescriptorCollectionProvider,
                mvcEndpointInvokerFactory ?? new MvcEndpointInvokerFactory(new ActionInvokerFactory(Array.Empty <IActionInvokerProvider>())),
                serviceProvider.GetRequiredService <ParameterPolicyFactory>(),
                serviceProvider.GetRequiredService <RoutePatternTransformer>());

            var defaultEndpointConventionBuilder = new DefaultEndpointConventionBuilder();

            dataSource.AttributeRoutingConventionResolvers.Add((actionDescriptor) =>
            {
                return(defaultEndpointConventionBuilder);
            });

            return(dataSource);
        }
Esempio n. 10
0
        private static IActionDescriptorCollectionProvider CreateActionDescriptorCollectionProvider(ActionDescriptor actionDescriptor)
        {
            var actionProvider = new Mock <IActionDescriptorProvider>(MockBehavior.Strict);

            actionProvider
            .SetupGet(p => p.Order)
            .Returns(-1000);

            actionProvider
            .Setup(p => p.OnProvidersExecuting(It.IsAny <ActionDescriptorProviderContext>()))
            .Callback <ActionDescriptorProviderContext>(c => c.Results.Add(actionDescriptor));

            actionProvider
            .Setup(p => p.OnProvidersExecuted(It.IsAny <ActionDescriptorProviderContext>()))
            .Verifiable();

            var descriptorCollectionProvider = new DefaultActionDescriptorCollectionProvider(
                new[] { actionProvider.Object },
                Enumerable.Empty <IActionDescriptorChangeProvider>());

            return(descriptorCollectionProvider);
        }
Esempio n. 11
0
        private MvcEndpointDataSource CreateMvcEndpointDataSource(
            IActionDescriptorCollectionProvider actionDescriptorCollectionProvider = null,
            MvcEndpointInvokerFactory mvcEndpointInvokerFactory = null)
        {
            if (actionDescriptorCollectionProvider == null)
            {
                actionDescriptorCollectionProvider = new DefaultActionDescriptorCollectionProvider(
                    Array.Empty <IActionDescriptorProvider>(),
                    Array.Empty <IActionDescriptorChangeProvider>());
            }

            var serviceProviderMock = new Mock <IServiceProvider>();

            serviceProviderMock.Setup(m => m.GetService(typeof(IActionDescriptorCollectionProvider))).Returns(actionDescriptorCollectionProvider);

            var dataSource = new MvcEndpointDataSource(
                actionDescriptorCollectionProvider,
                mvcEndpointInvokerFactory ?? new MvcEndpointInvokerFactory(new ActionInvokerFactory(Array.Empty <IActionInvokerProvider>())),
                serviceProviderMock.Object);

            return(dataSource);
        }
    public void ActionDescriptors_CachesValuesByDefault()
    {
        // Arrange
        var actionDescriptorProvider = GetActionDescriptorProvider(new ActionDescriptor());

        var actionDescriptorCollectionProvider = new DefaultActionDescriptorCollectionProvider(
            new[] { actionDescriptorProvider },
            Enumerable.Empty <IActionDescriptorChangeProvider>());

        // Act - 1
        var collection1 = actionDescriptorCollectionProvider.ActionDescriptors;

        // Assert - 1
        Assert.Equal(0, collection1.Version);

        // Act - 2
        var collection2 = actionDescriptorCollectionProvider.ActionDescriptors;

        // Assert - 2
        Assert.Same(collection1, collection2);
        Mock.Get(actionDescriptorProvider)
        .Verify(v => v.OnProvidersExecuting(It.IsAny <ActionDescriptorProviderContext>()), Times.Once());
    }
    public void ActionDescriptors_UpdatesAndResubscribes_WhenChangeTokenTriggers()
    {
        // Arrange
        var actionDescriptorProvider = new Mock <IActionDescriptorProvider>();
        var expected1 = new ActionDescriptor();
        var expected2 = new ActionDescriptor();
        var expected3 = new ActionDescriptor();

        var invocations = 0;

        actionDescriptorProvider
        .Setup(p => p.OnProvidersExecuting(It.IsAny <ActionDescriptorProviderContext>()))
        .Callback((ActionDescriptorProviderContext context) =>
        {
            if (invocations == 0)
            {
                context.Results.Add(expected1);
            }
            else if (invocations == 1)
            {
                context.Results.Add(expected2);
            }
            else
            {
                context.Results.Add(expected3);
            }

            invocations++;
        });
        var changeProvider = new TestChangeProvider();
        var actionDescriptorCollectionProvider = new DefaultActionDescriptorCollectionProvider(
            new[] { actionDescriptorProvider.Object },
            new[] { changeProvider },
            NullLogger <DefaultActionDescriptorCollectionProvider> .Instance);

        // Act - 1
        var changeToken1 = actionDescriptorCollectionProvider.GetChangeToken();
        var collection1  = actionDescriptorCollectionProvider.ActionDescriptors;

        ActionDescriptorCollection captured = null;

        changeToken1.RegisterChangeCallback((_) =>
        {
            captured = actionDescriptorCollectionProvider.ActionDescriptors;
        }, null);

        // Assert - 1
        Assert.False(changeToken1.HasChanged);
        Assert.Equal(0, collection1.Version);
        Assert.Collection(collection1.Items,
                          item => Assert.Same(expected1, item));

        // Act - 2
        changeProvider.TokenSource.Cancel();
        var changeToken2 = actionDescriptorCollectionProvider.GetChangeToken();
        var collection2  = actionDescriptorCollectionProvider.ActionDescriptors;

        changeToken2.RegisterChangeCallback((_) =>
        {
            captured = actionDescriptorCollectionProvider.ActionDescriptors;
        }, null);

        // Assert - 2
        Assert.NotSame(changeToken1, changeToken2);
        Assert.True(changeToken1.HasChanged);
        Assert.False(changeToken2.HasChanged);

        Assert.NotSame(collection1, collection2);
        Assert.NotNull(captured);
        Assert.Same(captured, collection2);
        Assert.Equal(1, collection2.Version);
        Assert.Collection(collection2.Items,
                          item => Assert.Same(expected2, item));

        // Act - 3
        changeProvider.TokenSource.Cancel();
        var changeToken3 = actionDescriptorCollectionProvider.GetChangeToken();
        var collection3  = actionDescriptorCollectionProvider.ActionDescriptors;

        // Assert - 3
        Assert.NotSame(changeToken2, changeToken3);
        Assert.True(changeToken2.HasChanged);
        Assert.False(changeToken3.HasChanged);

        Assert.NotSame(collection2, collection3);
        Assert.NotNull(captured);
        Assert.Same(captured, collection3);
        Assert.Equal(2, collection3.Version);
        Assert.Collection(collection3.Items,
                          item => Assert.Same(expected3, item));
    }