public void GetPathByAddressReturnsCorrectODataPathWithPathBase(string pathBase)
        {
            // Arrange
            int address = 1;
            IPerRouteContainer container = new PerRouteContainer();

            container.AddRoute("odata", "ms/{data}");

            IServiceProvider serviceProvider = new ServiceCollection()
                                               .AddSingleton <IPerRouteContainer>(container)
                                               .BuildServiceProvider();

            HttpContext httpContext = new DefaultHttpContext
            {
                RequestServices = serviceProvider
            };

            httpContext.Request.PathBase = pathBase;
            httpContext.ODataFeature().RouteName = "odata";
            RouteValueDictionary values          = new RouteValueDictionary();

            values["odataPath"] = "";

            RouteValueDictionary ambientValues = new RouteValueDictionary();

            ambientValues["data"] = 2;

            // Act
            Mock <LinkGenerator>       mock          = new Mock <LinkGenerator>();
            ODataEndpointLinkGenerator linkGenerator = new ODataEndpointLinkGenerator(mock.Object);
            string path = linkGenerator.GetPathByAddress(httpContext, address, values, ambientValues);

            // Assert
            Assert.Equal(pathBase + "/ms/2", path);
        }
示例#2
0
        /// <summary>
        /// Get the per-route container from the configuration.
        /// </summary>
        /// <param name="configuration">The configuration.</param>
        /// <returns>The per-route container from the configuration</returns>
        internal static IPerRouteContainer GetPerRouteContainer(this HttpConfiguration configuration)
        {
            return((IPerRouteContainer)configuration.Properties.GetOrAdd(
                       PerRouteContainerKey,
                       key =>
            {
                IPerRouteContainer perRouteContainer = new PerRouteContainer(configuration);

                // Attach the build factory if there is one.
                object value;
                if (configuration.Properties.TryGetValue(ContainerBuilderFactoryKey, out value))
                {
                    Func <IContainerBuilder> builderFactory = (Func <IContainerBuilder>)value;
                    perRouteContainer.BuilderFactory = builderFactory;
                }

                return perRouteContainer;
            }));
        }
示例#3
0
        /// <summary>
        /// Get the per-route container from the configuration.
        /// </summary>
        /// <param name="configuration">The configuration.</param>
        /// <returns>The per-route container from the configuration</returns>
        internal static IPerRouteContainer GetPerRouteContainer(this HttpConfiguration configuration)
        {
            var perRouteContainerKey       = GetODataConstant("PerRouteContainerKey");
            var containerBuilderFactoryKey = GetODataConstant("ContainerBuilderFactoryKey");

            return((IPerRouteContainer)configuration.Properties.GetOrAdd(
                       perRouteContainerKey,
                       key =>
            {
                IPerRouteContainer perRouteContainer = new PerRouteContainer(configuration);

                // Attach the build factory if there is one.
                if (configuration.Properties.TryGetValue(containerBuilderFactoryKey, out var value))
                {
                    var builderFactory = (Func <IContainerBuilder>)value;
                    perRouteContainer.BuilderFactory = builderFactory;
                }

                return perRouteContainer;
            }));
        }
示例#4
0
        /// <summary>
        /// Test method to call constraint.Match using the proper arguments for each platform.
        /// </summary>
        /// <param name="constraint">The constraint object.</param>
        /// <param name="routeRequest">The abstracted request.</param>
        /// <param name="direction">The abstracted route direction.</param>
        /// <returns>Result from constraint.Match,</returns>
        private bool ConstraintMatch(ODataPathRouteConstraint constraint, TestRouteRequest routeRequest, Dictionary <string, object> values, RouteDirection direction)
        {
#if NETCORE
            IRouteBuilder config = RoutingConfigurationFactory.Create();
            if (routeRequest?.PathHandler != null && routeRequest?.Model != null)
            {
                config.MapODataServiceRoute(_routeName, "", builder =>
                                            builder.AddService(Microsoft.OData.ServiceLifetime.Singleton, sp => routeRequest.PathHandler)
                                            .AddService(Microsoft.OData.ServiceLifetime.Singleton, sp => routeRequest.Model));
            }
            else if (routeRequest?.PathHandler != null)
            {
                config.MapODataServiceRoute(_routeName, "", builder =>
                                            builder.AddService(Microsoft.OData.ServiceLifetime.Singleton, sp => routeRequest.PathHandler));
            }
            else if (routeRequest?.Model != null)
            {
                config.MapODataServiceRoute(_routeName, "", builder =>
                                            builder.AddService(Microsoft.OData.ServiceLifetime.Singleton, sp => routeRequest.Model));
            }
            else
            {
                config.MapODataServiceRoute(_routeName, "", builder => { });
            }

            HttpRequest request = (routeRequest != null)
                ? RequestFactory.Create(routeRequest.Method, routeRequest.Uri, config, _routeName)
                : RequestFactory.Create();

            // The RequestFactory will create a request container which most tests want but for checking the constraint,
            // we don't want a request container before the test runs since Match() creates one.
            request.DeleteRequestContainer(true);

            if (routeRequest != null)
            {
                routeRequest.InnerRequest = request;
            }

            AspNetCore.Routing.RouteDirection routeDirection = (direction == RouteDirection.UriResolution)
                ? AspNetCore.Routing.RouteDirection.IncomingRequest
                : AspNetCore.Routing.RouteDirection.UrlGeneration;

            RouteValueDictionary routeValues = new RouteValueDictionary(values);

            return(constraint.Match(request.HttpContext, null, null, routeValues, routeDirection));
#else
            HttpRequestMessage request = (routeRequest != null)
                ? new HttpRequestMessage(routeRequest.Method, routeRequest.Uri)
                : new HttpRequestMessage();

            var httpRouteCollection = new HttpRouteCollection
            {
                { _routeName, new HttpRoute() },
            };

            var configuration = new HttpConfiguration(httpRouteCollection);
            if (routeRequest != null && routeRequest.PathHandler != null && routeRequest.Model != null)
            {
                configuration.CreateODataRootContainer(_routeName, builder =>
                                                       builder.AddService(Microsoft.OData.ServiceLifetime.Singleton, sp => routeRequest.PathHandler)
                                                       .AddService(Microsoft.OData.ServiceLifetime.Singleton, sp => routeRequest.Model));
            }
            else if (routeRequest != null && routeRequest.PathHandler != null)
            {
                configuration.CreateODataRootContainer(_routeName, builder =>
                                                       builder.AddService(Microsoft.OData.ServiceLifetime.Singleton, sp => routeRequest.PathHandler));
            }
            else if (routeRequest != null && routeRequest.Model != null)
            {
                configuration.CreateODataRootContainer(_routeName, builder =>
                                                       builder.AddService(Microsoft.OData.ServiceLifetime.Singleton, sp => routeRequest.Model));
            }
            else
            {
                PerRouteContainer perRouteContainer = configuration.GetPerRouteContainer() as PerRouteContainer;
                perRouteContainer.SetODataRootContainer(_routeName, _rootContainer);
            }

            request.SetConfiguration(configuration);
            if (routeRequest != null)
            {
                routeRequest.InnerRequest = request;
            }

            HttpRouteDirection routeDirection = (direction == RouteDirection.UriResolution)
                ? HttpRouteDirection.UriResolution
                : HttpRouteDirection.UriGeneration;

            return(constraint.Match(request, null, null, values, routeDirection));
#endif
        }