private MvcEndpointInfo CreateEndpointInfo(
            string name,
            string template,
            RouteValueDictionary defaults            = null,
            IDictionary <string, object> constraints = null,
            RouteValueDictionary dataTokens          = null,
            IServiceProvider serviceProvider         = null)
        {
            if (serviceProvider == null)
            {
                var services = new ServiceCollection();
                services.AddRouting();
                services.AddSingleton(typeof(UpperCaseParameterTransform), new UpperCaseParameterTransform());

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

                serviceProvider = services.BuildServiceProvider();
            }

            var parameterPolicyFactory = serviceProvider.GetRequiredService <ParameterPolicyFactory>();

            return(new MvcEndpointInfo(name, template, defaults, constraints, dataTokens, parameterPolicyFactory));
        }
Example #2
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));
    }
Example #3
0
        public ActionEndpointFactoryTest()
        {
            var serviceCollection = new ServiceCollection();

            var routeOptionsSetup = new MvcCoreRouteOptionsSetup();

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

            Services = serviceCollection.BuildServiceProvider();
            Factory  = new ActionEndpointFactory(Services.GetRequiredService <RoutePatternTransformer>(), Enumerable.Empty <IRequestDelegateFactory>());
        }
        private MvcEndpointInfo CreateEndpointInfo(
            string name,
            string template,
            RouteValueDictionary defaults            = null,
            IDictionary <string, object> constraints = null,
            RouteValueDictionary dataTokens          = null)
        {
            var routeOptions      = new RouteOptions();
            var routeOptionsSetup = new MvcCoreRouteOptionsSetup();

            routeOptionsSetup.Configure(routeOptions);

            var constraintResolver = new DefaultInlineConstraintResolver(Options.Create <RouteOptions>(routeOptions));

            return(new MvcEndpointInfo(name, template, defaults, constraints, dataTokens, constraintResolver));
        }
Example #5
0
        public ActionEndpointFactoryTest()
        {
            var serviceCollection = new ServiceCollection();

            var routeOptionsSetup = new MvcCoreRouteOptionsSetup();

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

            Services       = serviceCollection.BuildServiceProvider();
            InvokerFactory = new Mock <IActionInvokerFactory>(MockBehavior.Strict);
            Factory        = new ActionEndpointFactory(
                Services.GetRequiredService <RoutePatternTransformer>(),
                new MvcEndpointInvokerFactory(InvokerFactory.Object));
        }
        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);
        }
Example #7
0
        public void ResolveFromServices_InjectsServiceProvider_HttpContextNotNeeded()
        {
            // Arrange
            var actionDescriptor = CreateActionDescriptor("testArea",
                                                          "testController",
                                                          "testAction");

            actionDescriptor.RouteValues.Add("randomKey", "testRandom");
            var descriptorCollectionProvider = CreateActionDescriptorCollectionProvider(actionDescriptor);

            var services = new ServiceCollection();

            services.AddRouting();
            services.AddSingleton(descriptorCollectionProvider);

            var routeOptionsSetup = new MvcCoreRouteOptionsSetup();

            services.Configure <RouteOptions>(routeOptionsSetup.Configure);

            var serviceProvider = services.BuildServiceProvider();

            var inlineConstraintResolver = serviceProvider.GetRequiredService <IInlineConstraintResolver>();
            var constraint = inlineConstraintResolver.ResolveConstraint("exists");

            var values = new RouteValueDictionary()
            {
                { "area", "testArea" },
                { "controller", "testController" },
                { "action", "testAction" },
                { "randomKey", "testRandom" }
            };

            // Act
            var knownRouteValueConstraint = Assert.IsType <KnownRouteValueConstraint>(constraint);
            var match = knownRouteValueConstraint.Match(httpContext: null, route: null, "area", values, RouteDirection.IncomingRequest);

            // Assert
            Assert.True(match);
        }