Add test for link generation with high priority required parameter (#869)
This commit is contained in:
parent
e51da32e9d
commit
29b50c7b64
|
|
@ -5,6 +5,7 @@ using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
using System.Linq;
|
using System.Linq;
|
||||||
using Microsoft.AspNetCore.Http;
|
using Microsoft.AspNetCore.Http;
|
||||||
|
using Microsoft.AspNetCore.Http.Features;
|
||||||
using Microsoft.AspNetCore.Routing.TestObjects;
|
using Microsoft.AspNetCore.Routing.TestObjects;
|
||||||
using Microsoft.Extensions.DependencyInjection;
|
using Microsoft.Extensions.DependencyInjection;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
@ -597,6 +598,64 @@ namespace Microsoft.AspNetCore.Routing
|
||||||
Assert.NotSame(original, actual);
|
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<IRouteValuesFeature>(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)
|
protected override void AddAdditionalServices(IServiceCollection services)
|
||||||
{
|
{
|
||||||
services.AddSingleton<IEndpointAddressScheme<int>, IntAddressScheme>();
|
services.AddSingleton<IEndpointAddressScheme<int>, IntAddressScheme>();
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue