1
+ namespace Microsoft . AspNetCore . Mvc . Versioning
2
+ {
3
+ using FluentAssertions ;
4
+ using Microsoft . AspNetCore . Mvc . Abstractions ;
5
+ using Microsoft . AspNetCore . Mvc . Controllers ;
6
+ using System . Collections . Generic ;
7
+ using System . Linq ;
8
+ using Xunit ;
9
+
10
+ public class ApiVersionCollatorTest
11
+ {
12
+ [ Theory ]
13
+ [ MemberData ( nameof ( ActionDescriptorProviderContexts ) ) ]
14
+ public void on_providers_executed_should_aggregate_api_version_models_by_controller ( ActionDescriptorProviderContext context )
15
+ {
16
+ // arrange
17
+ var collator = new ApiVersionCollator ( ) ;
18
+ var expected = new [ ] { new ApiVersion ( 1 , 0 ) , new ApiVersion ( 2 , 0 ) , new ApiVersion ( 3 , 0 ) } ;
19
+
20
+ // act
21
+ collator . OnProvidersExecuted ( context ) ;
22
+
23
+ // assert
24
+ var actions = context . Results . Where ( a => a . GetProperty < ApiVersionModel > ( ) != null ) ;
25
+
26
+ actions . All ( a => a . GetProperty < ApiVersionModel > ( ) . SupportedApiVersions . SequenceEqual ( expected ) ) . Should ( ) . BeTrue ( ) ;
27
+ }
28
+
29
+ public static IEnumerable < object [ ] > ActionDescriptorProviderContexts
30
+ {
31
+ get
32
+ {
33
+ yield return new object [ ] { ActionsWithRouteValues } ;
34
+ yield return new object [ ] { ActionsByControllerName } ;
35
+ }
36
+ }
37
+
38
+ private static ActionDescriptorProviderContext ActionsWithRouteValues =>
39
+ new ActionDescriptorProviderContext ( )
40
+ {
41
+ Results =
42
+ {
43
+ new ActionDescriptor ( )
44
+ {
45
+ RouteValues = new Dictionary < string , string > ( )
46
+ {
47
+ [ "controller" ] = "Values" ,
48
+ [ "action" ] = "Get" ,
49
+ } ,
50
+ Properties = new Dictionary < object , object > ( )
51
+ {
52
+ [ typeof ( ApiVersionModel ) ] = new ApiVersionModel ( new ApiVersion ( 1 , 0 ) ) ,
53
+ } ,
54
+ } ,
55
+ new ActionDescriptor ( )
56
+ {
57
+ RouteValues = new Dictionary < string , string > ( )
58
+ {
59
+ [ "page" ] = "/Some/Page" ,
60
+ } ,
61
+ } ,
62
+ new ActionDescriptor ( )
63
+ {
64
+ RouteValues = new Dictionary < string , string > ( )
65
+ {
66
+ [ "controller" ] = "Values" ,
67
+ [ "action" ] = "Get" ,
68
+ } ,
69
+ Properties = new Dictionary < object , object > ( )
70
+ {
71
+ [ typeof ( ApiVersionModel ) ] = new ApiVersionModel ( new ApiVersion ( 2 , 0 ) ) ,
72
+ } ,
73
+ } ,
74
+ new ActionDescriptor ( )
75
+ {
76
+ RouteValues = new Dictionary < string , string > ( )
77
+ {
78
+ [ "controller" ] = "Values" ,
79
+ [ "action" ] = "Get" ,
80
+ } ,
81
+ Properties = new Dictionary < object , object > ( )
82
+ {
83
+ [ typeof ( ApiVersionModel ) ] = new ApiVersionModel ( new ApiVersion ( 3 , 0 ) ) ,
84
+ } ,
85
+ } ,
86
+ } ,
87
+ } ;
88
+
89
+ private static ActionDescriptorProviderContext ActionsByControllerName =>
90
+ new ActionDescriptorProviderContext ( )
91
+ {
92
+ Results =
93
+ {
94
+ new ControllerActionDescriptor ( )
95
+ {
96
+ ControllerName = "Values" ,
97
+ RouteValues = new Dictionary < string , string > ( )
98
+ {
99
+ [ "action" ] = "Get" ,
100
+ } ,
101
+ Properties = new Dictionary < object , object > ( )
102
+ {
103
+ [ typeof ( ApiVersionModel ) ] = new ApiVersionModel ( new ApiVersion ( 1 , 0 ) ) ,
104
+ } ,
105
+ } ,
106
+ new ActionDescriptor ( )
107
+ {
108
+ RouteValues = new Dictionary < string , string > ( )
109
+ {
110
+ [ "page" ] = "/Some/Page" ,
111
+ } ,
112
+ } ,
113
+ new ControllerActionDescriptor ( )
114
+ {
115
+ ControllerName = "Values2" ,
116
+ RouteValues = new Dictionary < string , string > ( )
117
+ {
118
+ [ "action" ] = "Get" ,
119
+ } ,
120
+ Properties = new Dictionary < object , object > ( )
121
+ {
122
+ [ typeof ( ApiVersionModel ) ] = new ApiVersionModel ( new ApiVersion ( 2 , 0 ) ) ,
123
+ } ,
124
+ } ,
125
+ new ControllerActionDescriptor ( )
126
+ {
127
+ ControllerName = "Values3" ,
128
+ RouteValues = new Dictionary < string , string > ( )
129
+ {
130
+ [ "action" ] = "Get" ,
131
+ } ,
132
+ Properties = new Dictionary < object , object > ( )
133
+ {
134
+ [ typeof ( ApiVersionModel ) ] = new ApiVersionModel ( new ApiVersion ( 3 , 0 ) ) ,
135
+ } ,
136
+ } ,
137
+ } ,
138
+ } ;
139
+ }
140
+ }
0 commit comments