Skip to content

Commit fbf0708

Browse files
authored
Fix DfaGraphWriter ISuppressMatchingMetadata check (#850)
1 parent 7b16053 commit fbf0708

File tree

2 files changed

+96
-1
lines changed

2 files changed

+96
-1
lines changed

src/Microsoft.AspNetCore.Routing/Internal/DfaGraphWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ public void Write(EndpointDataSource dataSource, TextWriter writer)
3838
var endpoints = dataSource.Endpoints;
3939
for (var i = 0; i < endpoints.Count; i++)
4040
{
41-
if (endpoints[i] is RouteEndpoint endpoint && endpoint.Metadata.GetMetadata<ISuppressMatchingMetadata>()?.SuppressMatching == true)
41+
if (endpoints[i] is RouteEndpoint endpoint && (endpoint.Metadata.GetMetadata<ISuppressMatchingMetadata>()?.SuppressMatching ?? false) == false)
4242
{
4343
builder.AddEndpoint(endpoint);
4444
}
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// Copyright (c) .NET Foundation. All rights reserved.
2+
// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
3+
4+
using System.IO;
5+
using Microsoft.AspNetCore.Http;
6+
using Microsoft.AspNetCore.Routing.Patterns;
7+
using Microsoft.Extensions.DependencyInjection;
8+
using Microsoft.Extensions.Primitives;
9+
using Xunit;
10+
11+
namespace Microsoft.AspNetCore.Routing.Internal
12+
{
13+
public class DfaGraphWriterTest
14+
{
15+
private DfaGraphWriter CreateGraphWriter()
16+
{
17+
ServiceCollection services = new ServiceCollection();
18+
services.AddLogging();
19+
services.AddRouting();
20+
21+
return new DfaGraphWriter(services.BuildServiceProvider());
22+
}
23+
24+
[Fact]
25+
public void Write_ExcludeNonRouteEndpoint()
26+
{
27+
// Arrange
28+
var graphWriter = CreateGraphWriter();
29+
var writer = new StringWriter();
30+
var endpointsDataSource = new DefaultEndpointDataSource(new Endpoint((context) => null, EndpointMetadataCollection.Empty, string.Empty));
31+
32+
// Act
33+
graphWriter.Write(endpointsDataSource, writer);
34+
35+
// Assert
36+
Assert.Equal(@"digraph DFA {
37+
0 [label=""/""]
38+
}
39+
", writer.ToString());
40+
}
41+
42+
[Fact]
43+
public void Write_ExcludeRouteEndpointWithSuppressMatchingMetadata()
44+
{
45+
// Arrange
46+
var graphWriter = CreateGraphWriter();
47+
var writer = new StringWriter();
48+
var endpointsDataSource = new DefaultEndpointDataSource(
49+
new RouteEndpoint(
50+
(context) => null,
51+
RoutePatternFactory.Parse("/"),
52+
0,
53+
new EndpointMetadataCollection(new SuppressMatchingMetadata()),
54+
string.Empty));
55+
56+
// Act
57+
graphWriter.Write(endpointsDataSource, writer);
58+
59+
// Assert
60+
Assert.Equal(@"digraph DFA {
61+
0 [label=""/""]
62+
}
63+
", writer.ToString());
64+
}
65+
66+
[Fact]
67+
public void Write_IncludeRouteEndpointWithPolicy()
68+
{
69+
// Arrange
70+
var graphWriter = CreateGraphWriter();
71+
var writer = new StringWriter();
72+
var endpointsDataSource = new DefaultEndpointDataSource(
73+
new RouteEndpoint(
74+
(context) => null,
75+
RoutePatternFactory.Parse("/"),
76+
0,
77+
new EndpointMetadataCollection(new HttpMethodMetadata(new[] { "GET" })),
78+
string.Empty));
79+
80+
// Act
81+
graphWriter.Write(endpointsDataSource, writer);
82+
83+
// Assert
84+
var sdf = writer.ToString();
85+
Assert.Equal(@"digraph DFA {
86+
0 [label=""/ HTTP: GET""]
87+
1 [label=""/ HTTP: *""]
88+
2 -> 0 [label=""HTTP: GET""]
89+
2 -> 1 [label=""HTTP: *""]
90+
2 [label=""/""]
91+
}
92+
", sdf);
93+
}
94+
}
95+
}

0 commit comments

Comments
 (0)