static IList <IODataRoutingConvention> EnsureConventions(IList <IODataRoutingConvention> conventions)
        {
            Contract.Requires(conventions != null);
            Contract.Ensures(Contract.Result <IList <IODataRoutingConvention> >() != null);

            var hasVersionedMetadataConvention = false;

            for (var i = conventions.Count - 1; i >= 0; i--)
            {
                var convention = conventions[i];

                if (convention is AttributeRoutingConvention && convention.GetType().Equals(typeof(AttributeRoutingConvention)))
                {
                    conventions.RemoveAt(i);
                }
                else if (convention is MetadataRoutingConvention)
                {
                    conventions[i] = new VersionedMetadataRoutingConvention();
                    hasVersionedMetadataConvention = true;
                }
                else if (convention is VersionedMetadataRoutingConvention)
                {
                    hasVersionedMetadataConvention = true;
                }
            }

            if (!hasVersionedMetadataConvention)
            {
                conventions.Insert(0, new VersionedMetadataRoutingConvention());
            }

            return(conventions);
        }
Ejemplo n.º 2
0
        public void select_controller_should_return_expected_name(string requestUrl, string expected)
        {
            // arrange
            var odataPath         = ParseUrl(requestUrl);
            var request           = new HttpRequestMessage();
            var routingConvention = new VersionedMetadataRoutingConvention();

            // act
            var controllerName = routingConvention.SelectController(odataPath, request);

            // assert
            controllerName.Should().Be(expected);
        }
        static IList <IODataRoutingConvention> EnsureConventions(
            IList <IODataRoutingConvention> conventions,
            VersionedAttributeRoutingConvention?attributeRoutingConvention = default)
        {
            var hasVersionedAttributeConvention = false;
            var hasVersionedMetadataConvention  = false;

            for (var i = conventions.Count - 1; i >= 0; i--)
            {
                var convention = conventions[i];

                if (convention is AttributeRoutingConvention && convention.GetType().Equals(typeof(AttributeRoutingConvention)))
                {
                    if (attributeRoutingConvention == default)
                    {
                        conventions.RemoveAt(i);
                    }
                    else
                    {
                        conventions[i] = attributeRoutingConvention;
                        hasVersionedAttributeConvention = true;
                    }
                }
                else if (convention is MetadataRoutingConvention)
                {
                    conventions[i] = new VersionedMetadataRoutingConvention();
                    hasVersionedMetadataConvention = true;
                }
                else if (convention is VersionedMetadataRoutingConvention)
                {
                    hasVersionedMetadataConvention = true;
                }
            }

            if (!hasVersionedMetadataConvention)
            {
                conventions.Insert(0, new VersionedMetadataRoutingConvention());
            }

            if (!hasVersionedAttributeConvention && attributeRoutingConvention != default)
            {
                conventions.Insert(0, attributeRoutingConvention);
            }

            return(conventions);
        }
        public void select_action_should_return_expected_name(string requestUrl, string verb, string expected)
        {
            // arrange
            var odataPath         = ParseUrl(requestUrl);
            var request           = new HttpRequestMessage(new HttpMethod(verb), "http://localhost/$metadata");
            var controllerContext = new HttpControllerContext()
            {
                Request = request
            };
            var actionMap         = new Mock <ILookup <string, HttpActionDescriptor> >().Object;
            var routingConvention = new VersionedMetadataRoutingConvention();

            // act
            var actionName = routingConvention.SelectAction(odataPath, controllerContext, actionMap);

            // assert
            actionName.Should().Be(expected);
        }
        public void select_action_should_return_expected_name(string requestUrl, string verb, string expected)
        {
            // arrange
            var odataPath = ParseUrl(requestUrl);
            var feature   = new Mock <IODataFeature>();
            var features  = new Mock <IFeatureCollection>();
            var actionDescriptorCollectionProvider = new Mock <IActionDescriptorCollectionProvider>();
            var serviceProvider   = new Mock <IServiceProvider>();
            var request           = Mock.Of <HttpRequest>();
            var httpContext       = new Mock <HttpContext>();
            var routingConvention = new VersionedMetadataRoutingConvention();
            var items             = new ActionDescriptor[]
            {
                new ControllerActionDescriptor()
                {
                    ControllerName = "VersionedMetadata", ActionName = "GetServiceDocument"
                },
                new ControllerActionDescriptor()
                {
                    ControllerName = "VersionedMetadata", ActionName = "GetMetadata"
                },
                new ControllerActionDescriptor()
                {
                    ControllerName = "VersionedMetadata", ActionName = "GetOptions"
                },
            };

            feature.SetupProperty(f => f.Path, odataPath);
            features.Setup(f => f.Get <IODataFeature>()).Returns(feature.Object);
            actionDescriptorCollectionProvider.SetupGet(p => p.ActionDescriptors).Returns(new ActionDescriptorCollection(items, 0));
            serviceProvider.Setup(sp => sp.GetService(typeof(IActionDescriptorCollectionProvider))).Returns(actionDescriptorCollectionProvider.Object);
            request.Method = verb;
            httpContext.SetupGet(c => c.Features).Returns(features.Object);
            httpContext.SetupProperty(c => c.RequestServices, serviceProvider.Object);
            httpContext.SetupGet(c => c.Request).Returns(request);

            // act
            var actionName = routingConvention.SelectAction(new RouteContext(httpContext.Object))?.SingleOrDefault()?.ActionName;

            // assert
            actionName.Should().Be(expected);
        }
Ejemplo n.º 6
0
        public void select_action_should_return_expected_name(string requestUrl, string verb, string expected)
        {
            // arrange
            var odataPath = ParseUrl(requestUrl);
            var feature   = new Mock <IODataFeature>();
            var features  = new FeatureCollection();
            var actionDescriptorCollectionProvider = new Mock <IActionDescriptorCollectionProvider>();
            var serviceProvider   = new Mock <IServiceProvider>();
            var request           = new Mock <HttpRequest>();
            var httpContext       = new Mock <HttpContext>();
            var routingConvention = new VersionedMetadataRoutingConvention();
            var items             = new ActionDescriptor[]
            {
                new ControllerActionDescriptor()
                {
                    ControllerName = "VersionedMetadata", ActionName = "GetServiceDocument"
                },
                new ControllerActionDescriptor()
                {
                    ControllerName = "VersionedMetadata", ActionName = "GetMetadata"
                },
                new ControllerActionDescriptor()
                {
                    ControllerName = "VersionedMetadata", ActionName = "GetOptions"
                },
            };

            feature.SetupProperty(f => f.Path, odataPath);
            feature.SetupGet(f => f.RequestContainer).Returns(() =>
            {
                var sp       = new Mock <IServiceProvider>();
                var selector = new Mock <IEdmModelSelector>();

                selector.SetupGet(s => s.ApiVersions).Returns(new[] { ApiVersion.Default });
                sp.Setup(sp => sp.GetService(typeof(IEdmModelSelector))).Returns(selector.Object);

                return(sp.Object);
            });
            features.Set <IApiVersioningFeature>(new ApiVersioningFeature(httpContext.Object));
            features.Set(feature.Object);
            actionDescriptorCollectionProvider.SetupGet(p => p.ActionDescriptors).Returns(new ActionDescriptorCollection(items, 0));
            serviceProvider.Setup(sp => sp.GetService(typeof(IApiVersionReader))).Returns(new QueryStringApiVersionReader());
            serviceProvider.Setup(sp => sp.GetService(typeof(IActionDescriptorCollectionProvider))).Returns(actionDescriptorCollectionProvider.Object);
            serviceProvider.Setup(sp => sp.GetService(typeof(IApiVersionSelector))).Returns(Mock.Of <IApiVersionSelector>);
            serviceProvider.Setup(sp => sp.GetService(typeof(IODataRouteCollectionProvider)))
            .Returns(() =>
            {
                var provider = new Mock <IODataRouteCollectionProvider>();
                provider.SetupGet(p => p.Items).Returns(Mock.Of <IODataRouteCollection>);
                return(provider.Object);
            });
            request.SetupProperty(r => r.Method, verb);
            request.SetupGet(r => r.HttpContext).Returns(() => httpContext.Object);
            request.SetupProperty(r => r.Query, Mock.Of <IQueryCollection>());
            request.SetupGet(r => r.HttpContext).Returns(() => httpContext.Object);
            httpContext.SetupGet(c => c.Features).Returns(features);
            httpContext.SetupProperty(c => c.RequestServices, serviceProvider.Object);
            httpContext.SetupGet(c => c.Request).Returns(request.Object);

            // act
            var actionName = routingConvention.SelectAction(new RouteContext(httpContext.Object))?.SingleOrDefault()?.ActionName;

            // assert
            actionName.Should().Be(expected);
        }