public void Should_use_url_conventions_that_apply()
        {
            var httpConfiguration = new HttpConfiguration();
            var actionMethod      = Type <UrlConventionHandler>
                                    .Expression(x => x.Get_Segment()).ToActionMethod();

            var urlConvention = new TestUrlConvention
            {
                AppliesToFunc = c => true,
                GetUrlsFunc   = c => new[] { "fark", "farker" }.Select(y => $@"{y}/{c.ActionMethod
                    .Method.Name}/{c.UrlSegments.Join("/")}").ToArray()
            };

            _urlConventions.Add(urlConvention);

            var descriptors = _routeConvention.GetRouteDescriptors(
                new RouteContext(null, httpConfiguration, actionMethod));

            descriptors.Count.ShouldEqual(3);

            descriptors[0].Url.ShouldEqual("Tests/Unit/Routing/Segment");
            descriptors[1].Url.ShouldEqual("fark/Get_Segment/Tests/Unit/Routing/Segment");
            descriptors[2].Url.ShouldEqual("farker/Get_Segment/Tests/Unit/Routing/Segment");

            var expectedSegments = new [] { "Tests", "Unit", "Routing", "Segment" };

            urlConvention.AppliesToCalled.ShouldBeTrue();
            urlConvention.AppliesToContext.HttpConfiguration.ShouldEqual(httpConfiguration);
            urlConvention.AppliesToContext.ActionMethod.ShouldEqual(actionMethod);
            urlConvention.AppliesToContext.HttpMethod.ShouldEqual("GET");
            urlConvention.AppliesToContext.UrlSegments.ToArray().ShouldEqual(expectedSegments);
            urlConvention.AppliesToContext.UrlParameters.ShouldBeEmpty();
            urlConvention.AppliesToContext.QuerystringParameters.ShouldBeEmpty();
            urlConvention.AppliesToContext.RequestParameter.ShouldEqual(null);
            urlConvention.AppliesToContext.ResponseBodyType.ShouldEqual(null);

            urlConvention.GetUrlsCalled.ShouldBeTrue();
            urlConvention.GetUrlsContext.HttpConfiguration.ShouldEqual(httpConfiguration);
            urlConvention.GetUrlsContext.ActionMethod.ShouldEqual(actionMethod);
            urlConvention.GetUrlsContext.HttpMethod.ShouldEqual("GET");
            urlConvention.GetUrlsContext.UrlSegments.ToArray().ShouldEqual(expectedSegments);
            urlConvention.GetUrlsContext.UrlParameters.ShouldBeEmpty();
            urlConvention.GetUrlsContext.QuerystringParameters.ShouldBeEmpty();
            urlConvention.GetUrlsContext.RequestParameter.ShouldEqual(null);
            urlConvention.GetUrlsContext.ResponseBodyType.ShouldEqual(null);
        }
Beispiel #2
0
        public void Should_not_use_url_conventions_if_no_plugin_definition_found()
        {
            var urlConvention = new TestUrlConvention
            {
                GetUrlsFunc = c => new[] { "fark", "farker" }
            };

            _urlConventions.Add(urlConvention);

            var descriptors = _routeConvention.GetRouteDescriptors(new RouteContext(
                                                                       ActionMethod.From <UrlConventionHandler>(x => x.Get_Segment())));

            descriptors.Count.ShouldEqual(1);

            urlConvention.AppliesToCalled.ShouldBeFalse();
            urlConvention.GetUrlsCalled.ShouldBeFalse();
        }
Beispiel #3
0
        public void Should_use_url_conventions_that_apply()
        {
            var actionMethod  = ActionMethod.From <UrlConventionHandler>(x => x.Get_Segment());
            var urlConvention = new TestUrlConvention
            {
                AppliesToFunc = c => true,
                GetUrlsFunc   = c => new[] { "fark", "farker" }.Select(y =>
                                                                       $"{y}/{c.ActionMethod.MethodDescriptor.Name}/" +
                                                                       $"{c.ActionMethod.HandlerTypeDescriptor.Type.Namespace.Replace(".", "/")}/" +
                                                                       $"{c.MethodSegments.ToUrl()}").ToArray()
            };

            _urlConventions.Add(urlConvention);
            _configuration.UrlConventions.Configure(x => x.Append(urlConvention));

            var descriptors = _routeConvention.GetRouteDescriptors(
                new RouteContext(actionMethod));

            descriptors.Count.ShouldEqual(3);

            var defaultUrl = "Unit/Routing/Segment";

            descriptors[0].Url.ShouldEqual(defaultUrl);
            descriptors[1].Url.ShouldEqual("fark/Get_Segment/Tests/Unit/Routing/Segment");
            descriptors[2].Url.ShouldEqual("farker/Get_Segment/Tests/Unit/Routing/Segment");

            urlConvention.AppliesToCalled.ShouldBeTrue();
            urlConvention.AppliesToContext.ActionMethod.ShouldEqual(actionMethod);
            urlConvention.AppliesToContext.HttpMethod.ShouldEqual("GET");
            urlConvention.AppliesToContext.MethodSegments
            .ToUrl().ShouldEqual("Segment");
            urlConvention.AppliesToContext.UrlParameters.ShouldBeEmpty();
            urlConvention.AppliesToContext.Parameters.ShouldBeEmpty();
            urlConvention.AppliesToContext.RequestParameter.ShouldEqual(null);
            urlConvention.AppliesToContext.ResponseType.ShouldEqual(null);

            urlConvention.GetUrlsCalled.ShouldBeTrue();
            urlConvention.GetUrlsContext.ActionMethod.ShouldEqual(actionMethod);
            urlConvention.GetUrlsContext.HttpMethod.ShouldEqual("GET");
            urlConvention.GetUrlsContext.MethodSegments
            .ToUrl().ShouldEqual("Segment");
            urlConvention.GetUrlsContext.UrlParameters.ShouldBeEmpty();
            urlConvention.GetUrlsContext.Parameters.ShouldBeEmpty();
            urlConvention.GetUrlsContext.RequestParameter.ShouldEqual(null);
            urlConvention.GetUrlsContext.ResponseType.ShouldEqual(null);
        }
        public void Should_not_use_url_conventions_where_does_not_apply_in_configuration()
        {
            var urlConvention = new TestUrlConvention
            {
                GetUrlsFunc = c => new[] { "fark", "farker" }
            };

            _urlConventions.Add(urlConvention);
            _configuration.UrlConventions.Append <TestUrlConvention>(x => false);

            var descriptors = _routeConvention.GetRouteDescriptors(new RouteContext(null, null,
                                                                                    Type <UrlConventionHandler> .Expression(x => x.Get_Segment()).ToActionMethod()));

            descriptors.Count.ShouldEqual(1);

            urlConvention.AppliesToCalled.ShouldBeFalse();
            urlConvention.GetUrlsCalled.ShouldBeFalse();
        }
Beispiel #5
0
        public void Should_not_use_url_conventions_where_the_instance_does_not_apply()
        {
            var urlConvention = new TestUrlConvention
            {
                AppliesToFunc = c => false,
                GetUrlsFunc   = c => new[] { "fark", "farker" }
            };

            _urlConventions.Add(urlConvention);
            _configuration.UrlConventions.Configure(x => x.Append(urlConvention));

            var descriptors = _routeConvention.GetRouteDescriptors(new RouteContext(
                                                                       ActionMethod.From <UrlConventionHandler>(x => x.Get_Segment())));

            descriptors.Count.ShouldEqual(1);

            urlConvention.AppliesToCalled.ShouldBeTrue();
            urlConvention.GetUrlsCalled.ShouldBeFalse();
        }
Beispiel #6
0
        public void ParseCompositeKeyReference(TestUrlConvention testUrlConvention, string fullUrl)
        {
            var model = new EdmModel();

            var customer   = new EdmEntityType("Test", "Customer", null, false, true);
            var customerId = customer.AddStructuralProperty("id", EdmPrimitiveTypeKind.String, false);

            customer.AddKeys(customerId);
            model.AddElement(customer);

            var order           = new EdmEntityType("Test", "Order", null, false, true);
            var orderCustomerId = order.AddStructuralProperty("customerId", EdmPrimitiveTypeKind.String, true);
            var orderOrderId    = order.AddStructuralProperty("orderId", EdmPrimitiveTypeKind.String, true);

            order.AddKeys(orderCustomerId, orderOrderId);
            model.AddElement(order);

            var customerOrders = customer.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
            {
                ContainsTarget      = true,
                Name                = "orders",
                Target              = order,
                TargetMultiplicity  = EdmMultiplicity.Many,
                DependentProperties = new[] { customerId },
                PrincipalProperties = new[] { orderCustomerId }
            });

            var detail           = new EdmEntityType("Test", "Detail");
            var detailCustomerId = detail.AddStructuralProperty("customerId", EdmPrimitiveTypeKind.String);
            var detailOrderId    = detail.AddStructuralProperty("orderId", EdmPrimitiveTypeKind.String);

            detail.AddKeys(detailCustomerId, detailOrderId,
                           detail.AddStructuralProperty("id", EdmPrimitiveTypeKind.Int32, false));
            model.AddElement(detail);

            var detailedOrder        = new EdmEntityType("Test", "DetailedOrder", order);
            var detailedOrderDetails = detailedOrder.AddUnidirectionalNavigation(
                new EdmNavigationPropertyInfo
            {
                ContainsTarget     = true,
                Target             = detail,
                TargetMultiplicity = EdmMultiplicity.Many,
                Name = "details",
                DependentProperties = new[] { orderOrderId, orderCustomerId },
                PrincipalProperties = new[] { detailOrderId, detailCustomerId }
            });

            model.AddElement(detailedOrder);

            var container = new EdmEntityContainer("Test", "Container");
            var customers = container.AddEntitySet("customers", customer);

            model.AddElement(container);

            var parser = new ODataUriParser(model, new Uri("http://host"), new Uri(fullUrl));

            switch (testUrlConvention)
            {
            case TestUrlConvention.Default:
                parser.UrlConventions = ODataUrlConventions.Default;
                break;

            case TestUrlConvention.KeyAsSegment:
                parser.UrlConventions = ODataUrlConventions.KeyAsSegment;
                break;

            case TestUrlConvention.ODataSimplified:
                parser.UrlConventions = ODataUrlConventions.ODataSimplified;
                break;

            default:
                Assert.True(false, "Unreachable code path");
                break;
            }

            var path = parser.ParsePath().ToList();

            path[0].ShouldBeEntitySetSegment(customers);
            path[1].ShouldBeKeySegment(new KeyValuePair <string, object>("id", "customerId"));
            path[2].ShouldBeNavigationPropertySegment(customerOrders);
            path[3].ShouldBeKeySegment(new KeyValuePair <string, object>("customerId", "customerId"),
                                       new KeyValuePair <string, object>("orderId", "orderId"));
            if (path.Count > 4)
            {
                // For tests with a type cast and a second-level navigation property.
                path[4].ShouldBeTypeSegment(detailedOrder);
                path[5].ShouldBeNavigationPropertySegment(detailedOrderDetails);
                path[6].ShouldBeKeySegment(new KeyValuePair <string, object>("customerId", "customerId"),
                                           new KeyValuePair <string, object>("orderId", "orderId"),
                                           new KeyValuePair <string, object>("id", 1));
            }
        }
        public void ParseCompositeKeyReference(TestUrlConvention testUrlConvention, string fullUrl)
        {
            var model = new EdmModel();

            var customer = new EdmEntityType("Test", "Customer", null, false, true);
            var customerId = customer.AddStructuralProperty("id", EdmPrimitiveTypeKind.String, false);
            customer.AddKeys(customerId);
            model.AddElement(customer);

            var order = new EdmEntityType("Test", "Order", null, false, true);
            var orderCustomerId = order.AddStructuralProperty("customerId", EdmPrimitiveTypeKind.String, true);
            var orderOrderId = order.AddStructuralProperty("orderId", EdmPrimitiveTypeKind.String, true);
            order.AddKeys(orderCustomerId, orderOrderId);
            model.AddElement(order);

            var customerOrders = customer.AddUnidirectionalNavigation(new EdmNavigationPropertyInfo
            {
                ContainsTarget = true,
                Name = "orders",
                Target = order,
                TargetMultiplicity = EdmMultiplicity.Many,
                DependentProperties = new[] { customerId },
                PrincipalProperties = new[] { orderCustomerId }
            });

            var detail = new EdmEntityType("Test", "Detail");
            var detailCustomerId = detail.AddStructuralProperty("customerId", EdmPrimitiveTypeKind.String);
            var detailOrderId = detail.AddStructuralProperty("orderId", EdmPrimitiveTypeKind.String);
            detail.AddKeys(detailCustomerId, detailOrderId,
                detail.AddStructuralProperty("id", EdmPrimitiveTypeKind.Int32, false));
            model.AddElement(detail);

            var detailedOrder = new EdmEntityType("Test", "DetailedOrder", order);
            var detailedOrderDetails = detailedOrder.AddUnidirectionalNavigation(
                new EdmNavigationPropertyInfo
                {
                    ContainsTarget = true,
                    Target = detail,
                    TargetMultiplicity = EdmMultiplicity.Many,
                    Name = "details",
                    DependentProperties = new[] { orderOrderId, orderCustomerId },
                    PrincipalProperties = new[] { detailOrderId, detailCustomerId }
                });
            model.AddElement(detailedOrder);

            var container = new EdmEntityContainer("Test", "Container");
            var customers = container.AddEntitySet("customers", customer);
            model.AddElement(container);

            var parser = new ODataUriParser(model, new Uri("http://host"), new Uri(fullUrl));
            switch (testUrlConvention)
            {
                case TestUrlConvention.Default:
                    parser.UrlConventions = ODataUrlConventions.Default;
                    break;
                case TestUrlConvention.KeyAsSegment:
                    parser.UrlConventions = ODataUrlConventions.KeyAsSegment;
                    break;
                case TestUrlConvention.ODataSimplified:
                    parser.UrlConventions = ODataUrlConventions.ODataSimplified;
                    break;
                default:
                    Assert.True(false, "Unreachable code path");
                    break;
            }

            var path = parser.ParsePath().ToList();
            path[0].ShouldBeEntitySetSegment(customers);
            path[1].ShouldBeKeySegment(new KeyValuePair<string, object>("id", "customerId"));
            path[2].ShouldBeNavigationPropertySegment(customerOrders);
            path[3].ShouldBeKeySegment(new KeyValuePair<string, object>("customerId", "customerId"),
                new KeyValuePair<string, object>("orderId", "orderId"));
            if (path.Count > 4)
            {
                // For tests with a type cast and a second-level navigation property.
                path[4].ShouldBeTypeSegment(detailedOrder);
                path[5].ShouldBeNavigationPropertySegment(detailedOrderDetails);
                path[6].ShouldBeKeySegment(new KeyValuePair<string, object>("customerId", "customerId"),
                new KeyValuePair<string, object>("orderId", "orderId"),
                new KeyValuePair<string, object>("id", 1));
            }
        }