Add test for link generation with high priority required parameter (#869)

This commit is contained in:
James Newton-King 2018-10-18 19:54:49 +13:00 committed by GitHub
parent e51da32e9d
commit 29b50c7b64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 59 additions and 0 deletions

View File

@ -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<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)
{
services.AddSingleton<IEndpointAddressScheme<int>, IntAddressScheme>();