예제 #1
0
        public void GetPathByRouteValues_WithProtoContext_UsesAmbientValues()
        {
            // Arrange
            var endpoint1 = EndpointFactory.CreateRouteEndpoint(
                "Home/Index/{id}",
                defaults: new { controller = "Home", action = "Index", },
                requiredValues: new { controller = "Home", action = "Index", });
            var endpoint2 = EndpointFactory.CreateRouteEndpoint(
                "Home/Index/{id?}",
                defaults: new { controller = "Home", action = "Index", },
                requiredValues: new { controller = "Home", action = "Index", });

            var linkGenerator = CreateLinkGenerator(endpoint1, endpoint2);

            var context = new EndpointSelectorContext()
            {
                RouteValues = new RouteValueDictionary(new { action = "Index", })
            };
            var httpContext = CreateProtoContext();

            httpContext.Features.Set <IRouteValuesFeature>(context);
            httpContext.Request.PathBase = new PathString("/Foo/Bar?encodeme?");

            // Act
            var path = linkGenerator.GetPathByRouteValues(
                httpContext,
                routeName: null,
                values: new RouteValueDictionary(new { controller = "Home", query = "some?query" }),
                fragment: new FragmentString("#Fragment?"),
                options: new LinkOptions()
            {
                AppendTrailingSlash = true,
            });

            // Assert
            Assert.Equal("/Foo/Bar%3Fencodeme%3F/Home/Index/?query=some%3Fquery#Fragment?", path);
        }
예제 #2
0
        public void GetPathByAddress_WithProtoContext_ContextPassedToConstraint()
        {
            // Arrange
            var constraint = new TestRouteConstraint();

            var endpoint1 = EndpointFactory.CreateRouteEndpoint("{controller}/{action}/{id?}", policies: new { controller = constraint }, metadata: new object[] { new IntMetadata(1), });

            var linkGenerator = CreateLinkGenerator(endpoint1);

            var httpContext = CreateProtoContext();

            httpContext.Request.PathBase = "/Foo";

            // Act
            var uri = linkGenerator.GetPathByAddress(
                httpContext,
                1,
                values: new RouteValueDictionary(new { action = "Index", controller = "Home", }),
                pathBase: "/");

            // Assert
            Assert.Equal("/Home/Index", uri);
            Assert.True(constraint.HasProtoContext);
        }
예제 #3
0
        public void GetUriByAddress_WithProtoContext_WithPathBaseAndFragment()
        {
            // Arrange
            var endpoint1 = EndpointFactory.CreateRouteEndpoint("{controller}/{action}/{id}", metadata: new object[] { new IntMetadata(1), });
            var endpoint2 = EndpointFactory.CreateRouteEndpoint("{controller}/{action}/{id?}", metadata: new object[] { new IntMetadata(1), });

            var linkGenerator = CreateLinkGenerator(endpoint1, endpoint2);

            var httpContext = CreateProtoContext();

            httpContext.Request.Scheme   = "http";
            httpContext.Request.Host     = new HostString("example.com");
            httpContext.Request.PathBase = new PathString("/Foo/Bar?encodeme?");

            // Act
            var uri = linkGenerator.GetUriByAddress(
                httpContext,
                1,
                values: new RouteValueDictionary(new { controller = "Home", action = "In?dex", query = "some?query" }),
                fragment: new FragmentString("#Fragment?"));

            // Assert
            Assert.Equal("http://example.com/Foo/Bar%3Fencodeme%3F/Home/In%3Fdex?query=some%3Fquery#Fragment?", uri);
        }
예제 #4
0
        public void GetLink_ParameterTransformer_ForQueryString()
        {
            // Arrange
            var endpoint = EndpointFactory.CreateRouteEndpoint(
                "{controller:upper-case}/{name}",
                requiredValues: new { controller = "Home", name = "Test", },
                policies: new { c = new UpperCaseParameterTransform(), });

            Action <IServiceCollection> configure = (s) =>
            {
                s.Configure <RouteOptions>(o =>
                {
                    o.ConstraintMap["upper-case"] = typeof(UpperCaseParameterTransform);
                });
            };

            var linkGenerator = CreateLinkGenerator(configure, endpoint);

            // Act
            var link = linkGenerator.GetPathByRouteValues(routeName: null, new { controller = "Home", name = "Test", c = "hithere", });

            // Assert
            Assert.Equal("/HOME/Test?c=HITHERE", link);
        }
예제 #5
0
        public void GetPathByAddress_WithParameterTransformer()
        {
            // Arrange
            var endpoint1 = EndpointFactory.CreateRouteEndpoint("{controller:slugify}/{action}/{id}", metadata: new object[] { new IntMetadata(1), });
            var endpoint2 = EndpointFactory.CreateRouteEndpoint("{controller:slugify}/{action}/{id?}", metadata: new object[] { new IntMetadata(1), });

            Action <IServiceCollection> configureServices = s =>
            {
                s.Configure <RouteOptions>(o =>
                {
                    o.ConstraintMap["slugify"] = typeof(SlugifyParameterTransformer);
                });
            };

            var linkGenerator = CreateLinkGenerator(configureServices, endpoint1, endpoint2);

            // Act
            var path = linkGenerator.GetPathByAddress(
                1,
                values: new RouteValueDictionary(new { controller = "TestController", action = "Index", }));

            // Assert
            Assert.Equal("/test-controller/Index", path);
        }
예제 #6
0
        public void GetPathByName_WithoutProtoContext_WithPathBaseAndFragment()
        {
            // Arrange
            var endpoint1 = EndpointFactory.CreateRouteEndpoint("some-endpoint/{p}", metadata: new[] { new EndpointNameMetadata("name1"), });
            var endpoint2 = EndpointFactory.CreateRouteEndpoint("some#-other-endpoint/{p}", metadata: new[] { new EndpointNameMetadata("name2"), });

            var linkGenerator = CreateLinkGenerator(endpoint1, endpoint2);

            var values = new { p = "In?dex", query = "some?query", };

            // Act
            var path = linkGenerator.GetPathByName(
                endpointName: "name2",
                values,
                new PathString("/Foo/Bar?encodeme?"),
                new FragmentString("#Fragment?"),
                new LinkOptions()
            {
                AppendTrailingSlash = true,
            });

            // Assert
            Assert.Equal("/Foo/Bar%3Fencodeme%3F/some%23-other-endpoint/In%3Fdex/?query=some%3Fquery#Fragment?", path);
        }
예제 #7
0
        public void AddressScheme_UpdatesWhenDataSourceChanges()
        {
            var endpoint1 = EndpointFactory.CreateRouteEndpoint(
                "/a",
                metadata: new object[] { new EndpointNameMetadata("name1"), });
            var dynamicDataSource = new DynamicEndpointDataSource(new[] { endpoint1 });

            // Act 1
            var addressScheme = CreateAddressScheme(dynamicDataSource);

            // Assert 1
            var match = Assert.Single(addressScheme.Entries);

            Assert.Same(endpoint1, match.Value.Single());

            // Arrange 2
            var endpoint2 = EndpointFactory.CreateRouteEndpoint(
                "/b",
                metadata: new object[] { new EndpointNameMetadata("name2"), });

            // Act 2
            // Trigger change
            dynamicDataSource.AddEndpoint(endpoint2);

            // Assert 2
            Assert.Collection(
                addressScheme.Entries.OrderBy(kvp => kvp.Key),
                (m) =>
            {
                Assert.Same(endpoint1, m.Value.Single());
            },
                (m) =>
            {
                Assert.Same(endpoint2, m.Value.Single());
            });
        }
        public LinkGeneratorIntegrationTest()
        {
            var endpoints = new List <Endpoint>()
            {
                // Attribute routed endpoint 1
                EndpointFactory.CreateRouteEndpoint(
                    RoutePatternFactory.Parse(
                        "api/Pets/{id}",
                        defaults: new { controller = "Pets", action = "GetById", },
                        parameterPolicies: null,
                        requiredValues: new { controller = "Pets", action = "GetById", area = (string)null, page = (string)null, }),
                    order: 0),

                // Attribute routed endpoint 2
                EndpointFactory.CreateRouteEndpoint(
                    RoutePatternFactory.Parse(
                        "api/Pets",
                        defaults: new { controller = "Pets", action = "GetAll", },
                        parameterPolicies: null,
                        requiredValues: new { controller = "Pets", action = "GetAll", area = (string)null, page = (string)null, }),
                    order: 0),

                // Attribute routed endpoint 2
                EndpointFactory.CreateRouteEndpoint(
                    RoutePatternFactory.Parse(
                        "api/Pets/{id}",
                        defaults: new { controller = "Pets", action = "Update", },
                        parameterPolicies: null,
                        requiredValues: new { controller = "Pets", action = "Update", area = (string)null, page = (string)null, }),
                    order: 0),

                // Attribute routed endpoint 4
                EndpointFactory.CreateRouteEndpoint(
                    RoutePatternFactory.Parse(
                        "api/Inventory/{searchTerm}/{page}",
                        defaults: new { controller = "Inventory", action = "Search", },
                        parameterPolicies: null,
                        requiredValues: new { controller = "Inventory", action = "Search", area = (string)null, page = (string)null, }),
                    order: 0),

                // Conventional routed endpoint 1
                EndpointFactory.CreateRouteEndpoint(
                    RoutePatternFactory.Parse(
                        "{controller=Home}/{action=Index}/{id?}",
                        defaults: null,
                        parameterPolicies: null,
                        requiredValues: new { controller = "Home", action = "Index", area = (string)null, page = (string)null, }),
                    order: 2000,
                    metadata: new object[] { new SuppressLinkGenerationMetadata(), }),

                // Conventional routed endpoint 2
                EndpointFactory.CreateRouteEndpoint(
                    RoutePatternFactory.Parse(
                        "{controller=Home}/{action=Index}/{id?}",
                        defaults: null,
                        parameterPolicies: null,
                        requiredValues: new { controller = "Home", action = "About", area = (string)null, page = (string)null, }),
                    order: 2000,
                    metadata: new object[] { new SuppressLinkGenerationMetadata(), }),

                // Conventional routed endpoint 3
                EndpointFactory.CreateRouteEndpoint(
                    RoutePatternFactory.Parse(
                        "{controller=Home}/{action=Index}/{id?}",
                        defaults: null,
                        parameterPolicies: null,
                        requiredValues: new { controller = "Store", action = "Browse", area = (string)null, page = (string)null, }),
                    order: 2000,
                    metadata: new object[] { new SuppressLinkGenerationMetadata(), }),

                // Conventional routed link generation route 1
                EndpointFactory.CreateRouteEndpoint(
                    RoutePatternFactory.Parse(
                        "{controller=Home}/{action=Index}/{id?}",
                        defaults: null,
                        parameterPolicies: null,
                        requiredValues: new { controller = RoutePattern.RequiredValueAny, action = RoutePattern.RequiredValueAny, area = (string)null, page = (string)null, }),
                    order: 2000,
                    metadata: new object[] { new SuppressMatchingMetadata(), }),

                // Conventional routed endpoint 4 (with area)
                EndpointFactory.CreateRouteEndpoint(
                    RoutePatternFactory.Parse(
                        "Admin/{controller=Home}/{action=Index}/{id?}",
                        defaults: new { area = "Admin", },
                        parameterPolicies: new { controller = "Admin", },
                        requiredValues: new { area = "Admin", controller = "Users", action = "Add", page = (string)null, }),
                    order: 1000,
                    metadata: new object[] { new SuppressLinkGenerationMetadata(), }),

                // Conventional routed endpoint 5 (with area)
                EndpointFactory.CreateRouteEndpoint(
                    RoutePatternFactory.Parse(
                        "Admin/{controller=Home}/{action=Index}/{id?}",
                        defaults: new { area = "Admin", },
                        parameterPolicies: new { controller = "Admin", },
                        requiredValues: new { area = "Admin", controller = "Users", action = "Remove", page = (string)null, }),
                    order: 1000,
                    metadata: new object[] { new SuppressLinkGenerationMetadata(), }),

                // Conventional routed link generation route 2
                EndpointFactory.CreateRouteEndpoint(
                    RoutePatternFactory.Parse(
                        "Admin/{controller=Home}/{action=Index}/{id?}",
                        defaults: new { area = "Admin", },
                        parameterPolicies: new { area = "Admin", },
                        requiredValues: new { controller = RoutePattern.RequiredValueAny, action = RoutePattern.RequiredValueAny, area = "Admin", page = (string)null, }),
                    order: 1000,
                    metadata: new object[] { new SuppressMatchingMetadata(), }),

                // Conventional routed link generation route 3 - this doesn't match any actions.
                EndpointFactory.CreateRouteEndpoint(
                    RoutePatternFactory.Parse(
                        "api/{controller}/{id?}",
                        defaults: new { },
                        parameterPolicies: new { },
                        requiredValues: new { controller = RoutePattern.RequiredValueAny, action = (string)null, area = (string)null, page = (string)null, }),
                    order: 3000,
                    metadata: new object[] { new SuppressMatchingMetadata(), new RouteNameMetadata("custom"), }),

                // Conventional routed link generation route 3 - this doesn't match any actions.
                EndpointFactory.CreateRouteEndpoint(
                    RoutePatternFactory.Parse(
                        "api/Foo/{custom2}",
                        defaults: new { },
                        parameterPolicies: new { },
                        requiredValues: new { controller = (string)null, action = (string)null, area = (string)null, page = (string)null, }),
                    order: 3000,
                    metadata: new object[] { new SuppressMatchingMetadata(), new RouteNameMetadata("custom2"), }),

                // Razor Page 1 primary endpoint
                EndpointFactory.CreateRouteEndpoint(
                    RoutePatternFactory.Parse(
                        "Pages",
                        defaults: new { page = "/Pages/Index", },
                        parameterPolicies: null,
                        requiredValues: new { controller = (string)null, action = (string)null, area = (string)null, page = "/Pages/Index", }),
                    order: 0),

                // Razor Page 1 secondary endpoint
                EndpointFactory.CreateRouteEndpoint(
                    RoutePatternFactory.Parse(
                        "Pages/Index",
                        defaults: new { page = "/Pages/Index", },
                        parameterPolicies: null,
                        requiredValues: new { controller = (string)null, action = (string)null, area = (string)null, page = "/Pages/Index", }),
                    order: 0,
                    metadata: new object[] { new SuppressLinkGenerationMetadata(), }),

                // Razor Page 2 primary endpoint
                EndpointFactory.CreateRouteEndpoint(
                    RoutePatternFactory.Parse(
                        "Pages/Help/{id?}",
                        defaults: new { page = "/Pages/Help", },
                        parameterPolicies: null,
                        requiredValues: new { controller = (string)null, action = (string)null, area = (string)null, page = "/Pages/Help", }),
                    order: 0),

                // Razor Page 3 primary endpoint
                EndpointFactory.CreateRouteEndpoint(
                    RoutePatternFactory.Parse(
                        "Pages/About/{id?}",
                        defaults: new { page = "/Pages/About", },
                        parameterPolicies: null,
                        requiredValues: new { controller = (string)null, action = (string)null, area = (string)null, page = "/Pages/About", }),
                    order: 0),

                // Razor Page 4 with area primary endpoint
                EndpointFactory.CreateRouteEndpoint(
                    RoutePatternFactory.Parse(
                        "Admin/Pages",
                        defaults: new { page = "/Pages/Index", area = "Admin", },
                        parameterPolicies: null,
                        requiredValues: new { controller = (string)null, action = (string)null, area = "Admin", page = "/Pages/Index", }),
                    order: 0),

                // Razor Page 4 with area secondary endpoint
                EndpointFactory.CreateRouteEndpoint(
                    RoutePatternFactory.Parse(
                        "Admin/Pages/Index",
                        defaults: new { page = "/Pages/Index", area = "Admin", },
                        parameterPolicies: null,
                        requiredValues: new { controller = (string)null, action = (string)null, area = "Admin", page = "/Pages/Index", }),
                    order: 0,
                    metadata: new object[] { new SuppressLinkGenerationMetadata(), }),
            };

            Endpoints     = endpoints;
            LinkGenerator = CreateLinkGenerator(endpoints.ToArray());
        }