Fix Endpoint order in TreeMatcher (#542)

This commit is contained in:
James Newton-King 2018-06-08 16:19:56 +12:00 committed by GitHub
parent cc9a50e6d4
commit 1b470f3d3b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 58 additions and 1 deletions

View File

@ -194,7 +194,7 @@ namespace Microsoft.AspNetCore.Routing.Matchers
foreach (var group in groups)
{
var template = TemplateParser.Parse(group.Key.Template);
var entryExists = entries.Any(item => item.RouteTemplate.TemplateText == template.TemplateText);
var entryExists = entries.Any(item => item.RouteTemplate.TemplateText == template.TemplateText && item.Order == group.Key.Order);
if (!entryExists)
{
entries.Add(MapInbound(template, group.Value.ToArray(), group.Key.Order));

View File

@ -0,0 +1,57 @@
// Copyright (c) .NET Foundation. All rights reserved.
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Logging.Abstractions;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using Xunit;
namespace Microsoft.AspNetCore.Routing.Matchers
{
public class TreeMatcherTests
{
private MatcherEndpoint CreateEndpoint(string template, int order, object values = null)
{
return new MatcherEndpoint((next) => null, template, values, order, EndpointMetadataCollection.Empty, template);
}
private TreeMatcher CreateTreeMatcher(EndpointDataSource endpointDataSource)
{
var defaultInlineConstraintResolver = new DefaultInlineConstraintResolver(Options.Create(new RouteOptions()));
return new TreeMatcher(defaultInlineConstraintResolver, NullLogger.Instance, endpointDataSource);
}
[Fact]
public async Task MatchAsync_DuplicateTemplatesAndDifferentOrder_LowerOrderEndpointMatched()
{
// Arrange
var defaultInlineConstraintResolver = new DefaultInlineConstraintResolver(Options.Create(new RouteOptions()));
var higherOrderEndpoint = CreateEndpoint("/Teams", 1);
var lowerOrderEndpoint = CreateEndpoint("/Teams", 0);
var endpointDataSource = new DefaultEndpointDataSource(new List<Endpoint>
{
higherOrderEndpoint,
lowerOrderEndpoint
});
var treeMatcher = CreateTreeMatcher(endpointDataSource);
var httpContext = new DefaultHttpContext();
httpContext.Request.Path = "/Teams";
var endpointFeature = new EndpointFeature();
// Act
await treeMatcher.MatchAsync(httpContext, endpointFeature);
// Assert
Assert.Equal(lowerOrderEndpoint, endpointFeature.Endpoint);
}
}
}