Skip to content
This repository was archived by the owner on Nov 27, 2018. It is now read-only.

Commit 29b50c7

Browse files
authored
Add test for link generation with high priority required parameter (#869)
1 parent e51da32 commit 29b50c7

File tree

1 file changed

+59
-0
lines changed

1 file changed

+59
-0
lines changed

test/Microsoft.AspNetCore.Routing.Tests/DefaultLinkGeneratorTest.cs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.Collections.Generic;
66
using System.Linq;
77
using Microsoft.AspNetCore.Http;
8+
using Microsoft.AspNetCore.Http.Features;
89
using Microsoft.AspNetCore.Routing.TestObjects;
910
using Microsoft.Extensions.DependencyInjection;
1011
using Xunit;
@@ -597,6 +598,64 @@ public void GetTemplateBinder_CanClearCache()
597598
Assert.NotSame(original, actual);
598599
}
599600

601+
[Fact]
602+
public void GetPathByRouteValues_UsesFirstTemplateThatSucceeds()
603+
{
604+
// Arrange
605+
var endpointControllerAction = EndpointFactory.CreateRouteEndpoint(
606+
"Home/Index",
607+
order: 3,
608+
defaults: new { controller = "Home", action = "Index", },
609+
metadata: new[] { new RouteValuesAddressMetadata(new RouteValueDictionary(new { controller = "Home", action = "Index", })) });
610+
var endpointController = EndpointFactory.CreateRouteEndpoint(
611+
"Home",
612+
order: 2,
613+
defaults: new { controller = "Home", action = "Index", },
614+
metadata: new[] { new RouteValuesAddressMetadata(new RouteValueDictionary(new { controller = "Home", action = "Index", })) });
615+
var endpointEmpty = EndpointFactory.CreateRouteEndpoint(
616+
"",
617+
order: 1,
618+
defaults: new { controller = "Home", action = "Index", },
619+
metadata: new[] { new RouteValuesAddressMetadata(new RouteValueDictionary(new { controller = "Home", action = "Index", })) });
620+
621+
// This endpoint should be used to generate the link when an id is present
622+
var endpointControllerActionParameter = EndpointFactory.CreateRouteEndpoint(
623+
"Home/Index/{id}",
624+
order: 0,
625+
defaults: new { controller = "Home", action = "Index", },
626+
metadata: new[] { new RouteValuesAddressMetadata(new RouteValueDictionary(new { controller = "Home", action = "Index", })) });
627+
628+
var linkGenerator = CreateLinkGenerator(endpointControllerAction, endpointController, endpointEmpty, endpointControllerActionParameter);
629+
630+
var context = new EndpointSelectorContext()
631+
{
632+
RouteValues = new RouteValueDictionary(new { controller = "Home", action = "Index", })
633+
};
634+
var httpContext = CreateHttpContext();
635+
httpContext.Features.Set<IRouteValuesFeature>(context);
636+
637+
// Act
638+
var pathWithoutId = linkGenerator.GetPathByRouteValues(
639+
httpContext,
640+
routeName: null,
641+
values: new RouteValueDictionary());
642+
643+
var pathWithId = linkGenerator.GetPathByRouteValues(
644+
httpContext,
645+
routeName: null,
646+
values: new RouteValueDictionary(new { id = "3" }));
647+
648+
var pathWithCustom = linkGenerator.GetPathByRouteValues(
649+
httpContext,
650+
routeName: null,
651+
values: new RouteValueDictionary(new { custom = "Custom" }));
652+
653+
// Assert
654+
Assert.Equal("/", pathWithoutId);
655+
Assert.Equal("/Home/Index/3", pathWithId);
656+
Assert.Equal("/?custom=Custom", pathWithCustom);
657+
}
658+
600659
protected override void AddAdditionalServices(IServiceCollection services)
601660
{
602661
services.AddSingleton<IEndpointAddressScheme<int>, IntAddressScheme>();

0 commit comments

Comments
 (0)