From 97f54c532bddfb584ebe2f6474ce985c09337410 Mon Sep 17 00:00:00 2001 From: Ryan Nowak Date: Wed, 15 Aug 2018 20:57:40 -0700 Subject: [PATCH] Support supression of matching for endpoints --- .../ISuppressMatchingMetadata.cs | 13 ++++++++++++ .../Internal/DfaGraphWriter.cs | 2 +- .../Matching/DataSourceDependentMatcher.cs | 2 +- .../SuppressMatchingMetadata.cs | 13 ++++++++++++ .../DataSourceDependentMatcherTest.cs | 21 +++++++++++++++++++ 5 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 src/Microsoft.AspNetCore.Routing/ISuppressMatchingMetadata.cs create mode 100644 src/Microsoft.AspNetCore.Routing/SuppressMatchingMetadata.cs diff --git a/src/Microsoft.AspNetCore.Routing/ISuppressMatchingMetadata.cs b/src/Microsoft.AspNetCore.Routing/ISuppressMatchingMetadata.cs new file mode 100644 index 0000000000..8baa0e0a77 --- /dev/null +++ b/src/Microsoft.AspNetCore.Routing/ISuppressMatchingMetadata.cs @@ -0,0 +1,13 @@ +// 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. + +namespace Microsoft.AspNetCore.Routing +{ + /// + /// Metadata used to prevent URL matching. The associated endpoint will not be + /// considered URL matching for incoming requests. + /// + public interface ISuppressMatchingMetadata + { + } +} \ No newline at end of file diff --git a/src/Microsoft.AspNetCore.Routing/Internal/DfaGraphWriter.cs b/src/Microsoft.AspNetCore.Routing/Internal/DfaGraphWriter.cs index 9a1c8773f3..2d036ac76b 100644 --- a/src/Microsoft.AspNetCore.Routing/Internal/DfaGraphWriter.cs +++ b/src/Microsoft.AspNetCore.Routing/Internal/DfaGraphWriter.cs @@ -39,7 +39,7 @@ namespace Microsoft.AspNetCore.Routing.Internal for (var i = 0; i < endpoints.Count; i++) { var endpoint = endpoints[i] as MatcherEndpoint; - if (endpoint != null) + if (endpoint != null && endpoint.Metadata.GetMetadata() == null) { builder.AddEndpoint(endpoint); } diff --git a/src/Microsoft.AspNetCore.Routing/Matching/DataSourceDependentMatcher.cs b/src/Microsoft.AspNetCore.Routing/Matching/DataSourceDependentMatcher.cs index 5ff901b96d..9dced3dc3d 100644 --- a/src/Microsoft.AspNetCore.Routing/Matching/DataSourceDependentMatcher.cs +++ b/src/Microsoft.AspNetCore.Routing/Matching/DataSourceDependentMatcher.cs @@ -40,7 +40,7 @@ namespace Microsoft.AspNetCore.Routing.Matching // register other endpoint types, which are non-routable, and it's // ok that we won't route to them. var endpoint = endpoints[i] as MatcherEndpoint; - if (endpoint != null) + if (endpoint != null && endpoint.Metadata.GetMetadata() == null) { builder.AddEndpoint(endpoint); } diff --git a/src/Microsoft.AspNetCore.Routing/SuppressMatchingMetadata.cs b/src/Microsoft.AspNetCore.Routing/SuppressMatchingMetadata.cs new file mode 100644 index 0000000000..da7a3bce1d --- /dev/null +++ b/src/Microsoft.AspNetCore.Routing/SuppressMatchingMetadata.cs @@ -0,0 +1,13 @@ +// 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. + +namespace Microsoft.AspNetCore.Routing +{ + /// + /// Metadata used to prevent URL matching. The associated endpoint will not be + /// considered URL matching for incoming requests. + /// + public sealed class SuppressMatchingMetadata : ISuppressMatchingMetadata + { + } +} diff --git a/test/Microsoft.AspNetCore.Routing.Tests/Matching/DataSourceDependentMatcherTest.cs b/test/Microsoft.AspNetCore.Routing.Tests/Matching/DataSourceDependentMatcherTest.cs index 07c1db9537..74b87b56fc 100644 --- a/test/Microsoft.AspNetCore.Routing.Tests/Matching/DataSourceDependentMatcherTest.cs +++ b/test/Microsoft.AspNetCore.Routing.Tests/Matching/DataSourceDependentMatcherTest.cs @@ -67,6 +67,27 @@ namespace Microsoft.AspNetCore.Routing.Matching Assert.Empty(inner.Endpoints); } + [Fact] + public void Matcher_Ignores_SuppressedEndpoint() + { + // Arrange + var dataSource = new DynamicEndpointDataSource(); + var endpoint = new MatcherEndpoint( + MatcherEndpoint.EmptyInvoker, + RoutePatternFactory.Parse("/"), + 0, + new EndpointMetadataCollection(new SuppressMatchingMetadata()), + "test"); + dataSource.AddEndpoint(endpoint); + + // Act + var matcher = new DataSourceDependentMatcher(dataSource, TestMatcherBuilder.Create); + + // Assert + var inner = Assert.IsType(matcher.CurrentMatcher); + Assert.Empty(inner.Endpoints); + } + [Fact] public void Cache_Reinitializes_WhenDataSourceChanges() {