1
1
namespace Microsoft . AspNetCore . Mvc . Routing
2
2
{
3
3
using AspNetCore . Routing ;
4
+ using Builder ;
5
+ using Extensions . DependencyInjection ;
6
+ using Extensions . ObjectPool ;
4
7
using FluentAssertions ;
5
8
using Http ;
6
9
using Moq ;
10
+ using System ;
7
11
using System . Collections . Generic ;
12
+ using System . Text . Encodings . Web ;
13
+ using System . Threading . Tasks ;
8
14
using Xunit ;
9
15
using static AspNetCore . Routing . RouteDirection ;
16
+ using static System . String ;
10
17
11
18
public class ApiVersionRouteConstraintTest
12
19
{
13
- [ Fact ]
14
- public void match_should_return_false_for_url_generation ( )
20
+ private class PassThroughRouter : IRouter
21
+ {
22
+ public VirtualPathData GetVirtualPath ( VirtualPathContext context ) => null ;
23
+
24
+ public Task RouteAsync ( RouteContext context )
25
+ {
26
+ context . Handler = c => Task . CompletedTask ;
27
+ return Task . CompletedTask ;
28
+ }
29
+ }
30
+
31
+ private static ServiceCollection CreateServices ( )
32
+ {
33
+ var services = new ServiceCollection ( ) ;
34
+
35
+ services . AddOptions ( ) ;
36
+ services . AddLogging ( ) ;
37
+ services . AddRouting ( ) ;
38
+ services . AddSingleton < ObjectPoolProvider , DefaultObjectPoolProvider > ( )
39
+ . AddSingleton ( UrlEncoder . Default ) ;
40
+
41
+ return services ;
42
+ }
43
+
44
+ private static IRouteBuilder CreateRouteBuilder ( IServiceProvider services )
45
+ {
46
+ var app = new Mock < IApplicationBuilder > ( ) ;
47
+ app . SetupGet ( a => a . ApplicationServices ) . Returns ( services ) ;
48
+ return new RouteBuilder ( app . Object ) { DefaultHandler = new PassThroughRouter ( ) } ;
49
+ }
50
+
51
+ [ Theory ]
52
+ [ InlineData ( "apiVersion" , "1" , true ) ]
53
+ [ InlineData ( "apiVersion" , null , false ) ]
54
+ [ InlineData ( "apiVersion" , "" , false ) ]
55
+ [ InlineData ( null , "" , false ) ]
56
+ public void match_should_return_expected_result_for_url_generation ( string key , string value , bool expected )
15
57
{
16
58
// arrange
17
59
var httpContext = new Mock < HttpContext > ( ) . Object ;
@@ -20,11 +62,16 @@ public void match_should_return_false_for_url_generation()
20
62
var routeDirection = UrlGeneration ;
21
63
var constraint = new ApiVersionRouteConstraint ( ) ;
22
64
65
+ if ( ! IsNullOrEmpty ( key ) )
66
+ {
67
+ values [ key ] = value ;
68
+ }
69
+
23
70
// act
24
- var matched = constraint . Match ( httpContext , route , null , values , routeDirection ) ;
71
+ var matched = constraint . Match ( httpContext , route , key , values , routeDirection ) ;
25
72
26
73
// assert
27
- matched . Should ( ) . BeFalse ( ) ;
74
+ matched . Should ( ) . Be ( expected ) ;
28
75
}
29
76
30
77
[ Fact ]
@@ -87,5 +134,27 @@ public void match_should_return_true_when_matched()
87
134
// assert
88
135
matched . Should ( ) . BeTrue ( ) ;
89
136
}
137
+
138
+ [ Fact ]
139
+ public void url_helper_should_create_route_link_with_api_version_constriant ( )
140
+ {
141
+ // arrange
142
+ var services = CreateServices ( ) . AddApiVersioning ( ) ;
143
+ var provider = services . BuildServiceProvider ( ) ;
144
+ var routeBuilder = CreateRouteBuilder ( provider ) ;
145
+ var actionContext = new ActionContext ( ) { HttpContext = new DefaultHttpContext ( ) { RequestServices = provider } } ;
146
+
147
+ routeBuilder . MapRoute ( "default" , "v{version:apiVersion}/{controller}/{action}" ) ;
148
+ actionContext . RouteData = new RouteData ( ) ;
149
+ actionContext . RouteData . Routers . Add ( routeBuilder . Build ( ) ) ;
150
+
151
+ var urlHelper = new UrlHelper ( actionContext ) ;
152
+
153
+ // act
154
+ var url = urlHelper . Link ( "default" , new { version = "1" , controller = "Store" , action = "Buy" } ) ;
155
+
156
+ // assert
157
+ url . Should ( ) . Be ( "/v1/Store/Buy" ) ;
158
+ }
90
159
}
91
- }
160
+ }
0 commit comments