Esempio n. 1
0
        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 ActionDescriptorCollectionProvider(
                new IActionDescriptorProvider[] { actionDescriptorProvider.Object, },
                Enumerable.Empty <IActionDescriptorChangeProvider>());

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

            return(new ActionConstraintMatcherPolicy(cache));
        }
Esempio n. 2
0
        private static HttpContext GetHttpContext(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 ActionDescriptorCollectionProvider(
                new[] { actionProvider.Object },
                Enumerable.Empty <IActionDescriptorChangeProvider>());

            var context = new Mock <HttpContext>();

            context.Setup(o => o.RequestServices
                          .GetService(typeof(IActionDescriptorCollectionProvider)))
            .Returns(descriptorCollectionProvider);
            return(context.Object);
        }
Esempio n. 3
0
        private static ActionConstraintCache GetActionConstraintCache(IActionConstraintProvider[] actionConstraintProviders = null)
        {
            var descriptorProvider = new ActionDescriptorCollectionProvider(
                Enumerable.Empty <IActionDescriptorProvider>(),
                Enumerable.Empty <IActionDescriptorChangeProvider>());

            return(new ActionConstraintCache(descriptorProvider, actionConstraintProviders.AsEnumerable() ?? new List <IActionConstraintProvider>()));
        }
 /// <summary>
 /// Initializes a new instance of the <see cref="GenerateBlocklyFilesHostedService"/> class.
 /// </summary>
 /// <param name="prov"></param>
 /// <param name="api">The API.</param>
 public GenerateBlocklyFilesHostedService(IActionDescriptorCollectionProvider prov, IApiDescriptionGroupCollectionProvider api)
 {
     this.api = api;
     this.cp  = prov as ActionDescriptorCollectionProvider;
     registerCallback();
     this.swaggers = new Dictionary <string, BlocklyFileGenerator>();
     this.oDatas   = new Dictionary <string, BlocklyFileGenerator>();
     this.GraphQLs = new Dictionary <string, BlocklyFileGenerator>();
 }
        private static IServiceProvider GetServices()
        {
            var services = new Mock <IServiceProvider>();

            var routeOptions = new RouteOptions();

            routeOptions.ConstraintMap.Add("exists", typeof(KnownRouteValueConstraint));

            var optionsAccessor = new Mock <IOptions <RouteOptions> >();

            optionsAccessor
            .SetupGet(o => o.Value)
            .Returns(routeOptions);
            services
            .Setup(s => s.GetService(typeof(IOptions <RouteOptions>)))
            .Returns(optionsAccessor.Object);

            services
            .Setup(s => s.GetService(typeof(IInlineConstraintResolver)))
            .Returns(new DefaultInlineConstraintResolver(optionsAccessor.Object));

            services
            .Setup(s => s.GetService(typeof(ILoggerFactory)))
            .Returns(new LoggerFactory());

            services
            .Setup(s => s.GetService(typeof(IActionContextAccessor)))
            .Returns(new ActionContextAccessor()
            {
                ActionContext = new ActionContext()
                {
                    HttpContext = new DefaultHttpContext()
                    {
                        RequestServices = services.Object,
                    },
                    RouteData = new RouteData(),
                },
            });

            var actionDescriptorCollectionProvider = new ActionDescriptorCollectionProvider(services.Object);

            services
            .Setup(s => s.GetService(typeof(IActionDescriptorCollectionProvider)))
            .Returns(actionDescriptorCollectionProvider);

            services
            .Setup(s => s.GetService(typeof(IEnumerable <IActionDescriptorProvider>)))
            .Returns(TestInit.GetActionDescriptorProviders());

            services
            .Setup(s => s.GetService(typeof(RoutingMarkerService)))
            .Returns(new RoutingMarkerService());

            services
            .Setup(s => s.GetService(typeof(UrlEncoder)))
            .Returns(UrlEncoder.Default);

            services
            .Setup(s => s.GetService(typeof(IExpressionRouteHelper)))
            .Returns(new ExpressionRouteHelper(actionDescriptorCollectionProvider, new UniqueRouteKeysProvider()));

            var objectPoolProvider = new DefaultObjectPoolProvider();
            var objectPolicy       = new UriBuilderContextPooledObjectPolicy(UrlEncoder.Default);
            var objectPool         = objectPoolProvider.Create(objectPolicy);

            services
            .Setup(s => s.GetService(typeof(ObjectPool <UriBuildingContext>)))
            .Returns(objectPool);

            return(services.Object);
        }