From fbf0708f199056ea3a999a8de736e24280fe50b3 Mon Sep 17 00:00:00 2001 From: James Newton-King Date: Fri, 12 Oct 2018 16:08:50 +1300 Subject: [PATCH] Fix DfaGraphWriter ISuppressMatchingMetadata check (#850) --- .../Internal/DfaGraphWriter.cs | 2 +- .../Internal/DfaGraphWriterTest.cs | 95 +++++++++++++++++++ 2 files changed, 96 insertions(+), 1 deletion(-) create mode 100644 test/Microsoft.AspNetCore.Routing.Tests/Internal/DfaGraphWriterTest.cs diff --git a/src/Microsoft.AspNetCore.Routing/Internal/DfaGraphWriter.cs b/src/Microsoft.AspNetCore.Routing/Internal/DfaGraphWriter.cs index 033a56c7ee..32f1a314f8 100644 --- a/src/Microsoft.AspNetCore.Routing/Internal/DfaGraphWriter.cs +++ b/src/Microsoft.AspNetCore.Routing/Internal/DfaGraphWriter.cs @@ -38,7 +38,7 @@ namespace Microsoft.AspNetCore.Routing.Internal var endpoints = dataSource.Endpoints; for (var i = 0; i < endpoints.Count; i++) { - if (endpoints[i] is RouteEndpoint endpoint && endpoint.Metadata.GetMetadata()?.SuppressMatching == true) + if (endpoints[i] is RouteEndpoint endpoint && (endpoint.Metadata.GetMetadata()?.SuppressMatching ?? false) == false) { builder.AddEndpoint(endpoint); } diff --git a/test/Microsoft.AspNetCore.Routing.Tests/Internal/DfaGraphWriterTest.cs b/test/Microsoft.AspNetCore.Routing.Tests/Internal/DfaGraphWriterTest.cs new file mode 100644 index 0000000000..bcabc39d37 --- /dev/null +++ b/test/Microsoft.AspNetCore.Routing.Tests/Internal/DfaGraphWriterTest.cs @@ -0,0 +1,95 @@ +// 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 System.IO; +using Microsoft.AspNetCore.Http; +using Microsoft.AspNetCore.Routing.Patterns; +using Microsoft.Extensions.DependencyInjection; +using Microsoft.Extensions.Primitives; +using Xunit; + +namespace Microsoft.AspNetCore.Routing.Internal +{ + public class DfaGraphWriterTest + { + private DfaGraphWriter CreateGraphWriter() + { + ServiceCollection services = new ServiceCollection(); + services.AddLogging(); + services.AddRouting(); + + return new DfaGraphWriter(services.BuildServiceProvider()); + } + + [Fact] + public void Write_ExcludeNonRouteEndpoint() + { + // Arrange + var graphWriter = CreateGraphWriter(); + var writer = new StringWriter(); + var endpointsDataSource = new DefaultEndpointDataSource(new Endpoint((context) => null, EndpointMetadataCollection.Empty, string.Empty)); + + // Act + graphWriter.Write(endpointsDataSource, writer); + + // Assert + Assert.Equal(@"digraph DFA { +0 [label=""/""] +} +", writer.ToString()); + } + + [Fact] + public void Write_ExcludeRouteEndpointWithSuppressMatchingMetadata() + { + // Arrange + var graphWriter = CreateGraphWriter(); + var writer = new StringWriter(); + var endpointsDataSource = new DefaultEndpointDataSource( + new RouteEndpoint( + (context) => null, + RoutePatternFactory.Parse("/"), + 0, + new EndpointMetadataCollection(new SuppressMatchingMetadata()), + string.Empty)); + + // Act + graphWriter.Write(endpointsDataSource, writer); + + // Assert + Assert.Equal(@"digraph DFA { +0 [label=""/""] +} +", writer.ToString()); + } + + [Fact] + public void Write_IncludeRouteEndpointWithPolicy() + { + // Arrange + var graphWriter = CreateGraphWriter(); + var writer = new StringWriter(); + var endpointsDataSource = new DefaultEndpointDataSource( + new RouteEndpoint( + (context) => null, + RoutePatternFactory.Parse("/"), + 0, + new EndpointMetadataCollection(new HttpMethodMetadata(new[] { "GET" })), + string.Empty)); + + // Act + graphWriter.Write(endpointsDataSource, writer); + + // Assert + var sdf = writer.ToString(); + Assert.Equal(@"digraph DFA { +0 [label=""/ HTTP: GET""] +1 [label=""/ HTTP: *""] +2 -> 0 [label=""HTTP: GET""] +2 -> 1 [label=""HTTP: *""] +2 [label=""/""] +} +", sdf); + } + } +}