9
9
using Microsoft . AspNetCore . Routing . Matching ;
10
10
using Microsoft . AspNetCore . Routing . TestObjects ;
11
11
using Microsoft . Extensions . DependencyInjection ;
12
+ using Microsoft . Extensions . Logging ;
13
+ using Microsoft . Extensions . Logging . Testing ;
12
14
using Xunit ;
13
15
14
16
namespace Microsoft . AspNetCore . Routing
@@ -23,48 +25,68 @@ public class DefaultLinkParserTest : LinkParserTestBase
23
25
public void ParsePathByAddresss_NoMatchingEndpoint_ReturnsNull ( )
24
26
{
25
27
// Arrange
26
- var endpoint = EndpointFactory . CreateRouteEndpoint ( "{controller}/{action}/{id?}" , metadata : new object [ ] { new IntMetadata ( 1 ) , } ) ;
28
+ var endpoint = EndpointFactory . CreateRouteEndpoint ( "{controller}/{action}/{id?}" , displayName : "Test1" , metadata : new object [ ] { new IntMetadata ( 1 ) , } ) ;
27
29
28
- var parser = CreateLinkParser ( endpoint ) ;
30
+ var sink = new TestSink ( ) ;
31
+ var loggerFactory = new TestLoggerFactory ( sink , enabled : true ) ;
32
+ var parser = CreateLinkParser ( services => { services . AddSingleton < ILoggerFactory > ( loggerFactory ) ; } , endpoint ) ;
29
33
30
34
// Act
31
35
var values = parser . ParsePathByAddress ( 0 , "/Home/Index/17" ) ;
32
36
33
37
// Assert
34
38
Assert . Null ( values ) ;
39
+
40
+ Assert . Collection (
41
+ sink . Writes ,
42
+ w => Assert . Equal ( "No endpoints found for address 0" , w . Message ) ) ;
35
43
}
36
44
37
45
[ Fact ]
38
46
public void ParsePathByAddresss_HasMatches_ReturnsNullWhenParsingFails ( )
39
47
{
40
48
// Arrange
41
- var endpoint1 = EndpointFactory . CreateRouteEndpoint ( "{controller}/{action}/{id}" , metadata : new object [ ] { new IntMetadata ( 1 ) , } ) ;
42
- var endpoint2 = EndpointFactory . CreateRouteEndpoint ( "{controller}/{action}/{id2}" , metadata : new object [ ] { new IntMetadata ( 0 ) , } ) ;
49
+ var endpoint1 = EndpointFactory . CreateRouteEndpoint ( "{controller}/{action}/{id}" , displayName : "Test1" , metadata : new object [ ] { new IntMetadata ( 1 ) , } ) ;
50
+ var endpoint2 = EndpointFactory . CreateRouteEndpoint ( "{controller}/{action}/{id2}" , displayName : "Test2" , metadata : new object [ ] { new IntMetadata ( 0 ) , } ) ;
43
51
44
- var parser = CreateLinkParser ( endpoint1 , endpoint2 ) ;
52
+ var sink = new TestSink ( ) ;
53
+ var loggerFactory = new TestLoggerFactory ( sink , enabled : true ) ;
54
+ var parser = CreateLinkParser ( services => { services . AddSingleton < ILoggerFactory > ( loggerFactory ) ; } , endpoint1 , endpoint2 ) ;
45
55
46
56
// Act
47
57
var values = parser . ParsePathByAddress ( 0 , "/" ) ;
48
58
49
59
// Assert
50
60
Assert . Null ( values ) ;
61
+
62
+ Assert . Collection (
63
+ sink . Writes ,
64
+ w => Assert . Equal ( "Found the endpoints Test2 for address 0" , w . Message ) ,
65
+ w => Assert . Equal ( "Path parsing failed for endpoints Test2 and URI path /" , w . Message ) ) ;
51
66
}
52
67
53
68
[ Fact ]
54
69
public void ParsePathByAddresss_HasMatches_ReturnsFirstSuccessfulParse ( )
55
70
{
56
71
// Arrange
57
- var endpoint0 = EndpointFactory . CreateRouteEndpoint ( "{controller}/{action}" , metadata : new object [ ] { new IntMetadata ( 0 ) , } ) ;
58
- var endpoint1 = EndpointFactory . CreateRouteEndpoint ( "{controller}/{action}/{id}" , metadata : new object [ ] { new IntMetadata ( 0 ) , } ) ;
59
- var endpoint2 = EndpointFactory . CreateRouteEndpoint ( "{controller}/{action}/{id2}" , metadata : new object [ ] { new IntMetadata ( 0 ) , } ) ;
72
+ var endpoint0 = EndpointFactory . CreateRouteEndpoint ( "{controller}/{action}" , displayName : "Test1" , metadata : new object [ ] { new IntMetadata ( 0 ) , } ) ;
73
+ var endpoint1 = EndpointFactory . CreateRouteEndpoint ( "{controller}/{action}/{id}" , displayName : "Test2" , metadata : new object [ ] { new IntMetadata ( 0 ) , } ) ;
74
+ var endpoint2 = EndpointFactory . CreateRouteEndpoint ( "{controller}/{action}/{id2}" , displayName : "Test3" , metadata : new object [ ] { new IntMetadata ( 0 ) , } ) ;
60
75
61
- var parser = CreateLinkParser ( endpoint0 , endpoint1 , endpoint2 ) ;
76
+ var sink = new TestSink ( ) ;
77
+ var loggerFactory = new TestLoggerFactory ( sink , enabled : true ) ;
78
+ var parser = CreateLinkParser ( services => { services . AddSingleton < ILoggerFactory > ( loggerFactory ) ; } , endpoint0 , endpoint1 , endpoint2 ) ;
62
79
63
80
// Act
64
81
var values = parser . ParsePathByAddress ( 0 , "/Home/Index/17" ) ;
65
82
66
83
// Assert
67
84
MatcherAssert . AssertRouteValuesEqual ( new { controller = "Home" , action = "Index" , id = "17" } , values ) ;
85
+
86
+ Assert . Collection (
87
+ sink . Writes ,
88
+ w => Assert . Equal ( "Found the endpoints Test1, Test2, Test3 for address 0" , w . Message ) ,
89
+ w => Assert . Equal ( "Path parsing succeeded for endpoint Test2 and URI path /Home/Index/17" , w . Message ) ) ;
68
90
}
69
91
70
92
[ Fact ]
0 commit comments