From 29b50c7b64593557214e915b67dd67ae50388dd5 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Thu, 18 Oct 2018 19:54:49 +1300 Subject: [PATCH] Add test for link generation with high priority required parameter (#869) --- .../DefaultLinkGeneratorTest.cs | 59 +++++++++++++++++++ 1 file changed, 59 insertions(+) diff --git a/test/Microsoft.AspNetCore.Routing.Tests/DefaultLinkGeneratorTest.cs b/test/Microsoft.AspNetCore.Routing.Tests/DefaultLinkGeneratorTest.cs index 831576c41c..1f76ef3045 100644 --- a/test/Microsoft.AspNetCore.Routing.Tests/DefaultLinkGeneratorTest.cs +++ b/test/Microsoft.AspNetCore.Routing.Tests/DefaultLinkGeneratorTest.cs @@ -5,6 +5,7 @@ using System; using System.Collections.Generic; using System.Linq; using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Http.Features; using Microsoft.AspNetCore.Routing.TestObjects; using Microsoft.Extensions.DependencyInjection; using Xunit; @@ -597,6 +598,64 @@ namespace Microsoft.AspNetCore.Routing Assert.NotSame(original, actual); } + [Fact] + public void GetPathByRouteValues_UsesFirstTemplateThatSucceeds() + { + // Arrange + var endpointControllerAction = EndpointFactory.CreateRouteEndpoint( + "Home/Index", + order: 3, + defaults: new { controller = "Home", action = "Index", }, + metadata: new[] { new RouteValuesAddressMetadata(new RouteValueDictionary(new { controller = "Home", action = "Index", })) }); + var endpointController = EndpointFactory.CreateRouteEndpoint( + "Home", + order: 2, + defaults: new { controller = "Home", action = "Index", }, + metadata: new[] { new RouteValuesAddressMetadata(new RouteValueDictionary(new { controller = "Home", action = "Index", })) }); + var endpointEmpty = EndpointFactory.CreateRouteEndpoint( + "", + order: 1, + defaults: new { controller = "Home", action = "Index", }, + metadata: new[] { new RouteValuesAddressMetadata(new RouteValueDictionary(new { controller = "Home", action = "Index", })) }); + + // This endpoint should be used to generate the link when an id is present + var endpointControllerActionParameter = EndpointFactory.CreateRouteEndpoint( + "Home/Index/{id}", + order: 0, + defaults: new { controller = "Home", action = "Index", }, + metadata: new[] { new RouteValuesAddressMetadata(new RouteValueDictionary(new { controller = "Home", action = "Index", })) }); + + var linkGenerator = CreateLinkGenerator(endpointControllerAction, endpointController, endpointEmpty, endpointControllerActionParameter); + + var context = new EndpointSelectorContext() + { + RouteValues = new RouteValueDictionary(new { controller = "Home", action = "Index", }) + }; + var httpContext = CreateHttpContext(); + httpContext.Features.Set(context); + + // Act + var pathWithoutId = linkGenerator.GetPathByRouteValues( + httpContext, + routeName: null, + values: new RouteValueDictionary()); + + var pathWithId = linkGenerator.GetPathByRouteValues( + httpContext, + routeName: null, + values: new RouteValueDictionary(new { id = "3" })); + + var pathWithCustom = linkGenerator.GetPathByRouteValues( + httpContext, + routeName: null, + values: new RouteValueDictionary(new { custom = "Custom" })); + + // Assert + Assert.Equal("/", pathWithoutId); + Assert.Equal("/Home/Index/3", pathWithId); + Assert.Equal("/?custom=Custom", pathWithCustom); + } + protected override void AddAdditionalServices(IServiceCollection services) { services.AddSingleton, IntAddressScheme>();