forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHttpControllerDescriptorTest.cs
More file actions
345 lines (281 loc) · 14.3 KB
/
Copy pathHttpControllerDescriptorTest.cs
File metadata and controls
345 lines (281 loc) · 14.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
// 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.
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Net.Http.Formatting;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using Microsoft.TestCommon;
using Moq;
namespace System.Web.Http
{
public class HttpControllerDescriptorTest
{
[Fact]
public void Default_Constructor()
{
HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor();
Assert.Null(controllerDescriptor.ControllerName);
Assert.Null(controllerDescriptor.Configuration);
Assert.Null(controllerDescriptor.ControllerType);
Assert.NotNull(controllerDescriptor.Properties);
}
[Fact]
public void Parameter_Constructor()
{
HttpConfiguration config = new HttpConfiguration();
string controllerName = "UsersController";
Type controllerType = typeof(UsersController);
HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor(config, controllerName, controllerType);
Assert.NotNull(controllerDescriptor.ControllerName);
Assert.NotNull(controllerDescriptor.Configuration);
Assert.NotNull(controllerDescriptor.ControllerType);
Assert.NotNull(controllerDescriptor.Properties);
Assert.Equal(config, controllerDescriptor.Configuration);
Assert.Equal(controllerName, controllerDescriptor.ControllerName);
Assert.Equal(controllerType, controllerDescriptor.ControllerType);
}
[Fact]
public void Constructor_Throws_IfConfigurationIsNull()
{
Assert.ThrowsArgumentNull(
() => new HttpControllerDescriptor(null, "UsersController", typeof(UsersController)),
"configuration");
}
[Fact]
public void Constructor_Throws_IfControllerNameIsNull()
{
Assert.ThrowsArgumentNull(
() => new HttpControllerDescriptor(new HttpConfiguration(), null, typeof(UsersController)),
"controllerName");
}
[Fact]
public void Constructor_Throws_IfControllerTypeIsNull()
{
Assert.ThrowsArgumentNull(
() => new HttpControllerDescriptor(new HttpConfiguration(), "UsersController", null),
"controllerType");
}
[Fact]
public void Configuration_Property()
{
HttpConfiguration config = new HttpConfiguration();
HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor();
Assert.Reflection.Property<HttpControllerDescriptor, HttpConfiguration>(
instance: controllerDescriptor,
propertyGetter: cd => cd.Configuration,
expectedDefaultValue: null,
allowNull: false,
roundTripTestValue: config);
}
[Fact]
public void ControllerName_Property()
{
string controllerName = "UsersController";
HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor();
Assert.Reflection.Property<HttpControllerDescriptor, string>(
instance: controllerDescriptor,
propertyGetter: cd => cd.ControllerName,
expectedDefaultValue: null,
allowNull: false,
roundTripTestValue: controllerName);
}
[Fact]
public void ControllerType_Property()
{
Type controllerType = typeof(UsersController);
HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor();
Assert.Reflection.Property<HttpControllerDescriptor, Type>(
instance: controllerDescriptor,
propertyGetter: cd => cd.ControllerType,
expectedDefaultValue: null,
allowNull: false,
roundTripTestValue: controllerType);
}
[Fact]
public void GetFilters_InvokesGetCustomAttributesMethod()
{
var descriptorMock = new Mock<HttpControllerDescriptor> { CallBase = true };
var filters = new Collection<IFilter>(new List<IFilter>());
descriptorMock.Setup(d => d.GetCustomAttributes<IFilter>()).Returns(filters).Verifiable();
var result = descriptorMock.Object.GetFilters();
Assert.Same(filters, result);
descriptorMock.Verify();
}
[Fact]
public void Initialize_In_InheritenceHierarchy()
{
// Verifies that initialization is run in order with , and that they all mutate on the same descriptor object
HttpConfiguration config = new HttpConfiguration();
// Act.
HttpControllerDescriptor desc = new HttpControllerDescriptor(config, "MyController", typeof(MyDerived2Controller));
// Assert
Assert.Same(MyDerived1Controller.SelectorBase, desc.Configuration.Services.GetActionSelector());
Assert.Same(MyDerived1Controller.ActionValueBinderDerived1, desc.Configuration.Services.GetActionValueBinder());
Assert.Same(config.Formatters, desc.Configuration.Formatters); // didn't override, stays the same
Assert.Same(config.ParameterBindingRules, desc.Configuration.ParameterBindingRules); // didn't override, stays the same
}
[Fact]
public void Initialize_In_InheritenceHierarchy_Branching()
{
// Verifies that initialization is run in order with, and that they all mutate on the same descriptor object
HttpConfiguration config = new HttpConfiguration();
// Act.
HttpControllerDescriptor desc = new HttpControllerDescriptor(config, "MyController", typeof(MyDerived3Controller));
// Assert
Assert.Same(MyDerived1Controller.SelectorBase, desc.Configuration.Services.GetActionSelector());
Assert.Same(MyDerived3Controller.ActionValueBinderDerived3, desc.Configuration.Services.GetActionValueBinder());
Assert.Same(config.Formatters, desc.Configuration.Formatters); // didn't override, stays the same
Assert.Same(config.ParameterBindingRules, desc.Configuration.ParameterBindingRules); // didn't override, stays the same
}
[Fact]
public void Initialize_GetsTheActualControllerDescriptor_In_InheritenceHierarchy()
{
HttpConfiguration config = new HttpConfiguration();
HttpControllerDescriptor desc = new HttpControllerDescriptor(config, "MyController", typeof(VerifyControllerDescriptorDerivedController));
Assert.Equal(typeof(VerifyControllerDescriptorDerivedController), VerifyControllerDescriptorAttribute.ControllerType);
desc = new HttpControllerDescriptor(config, "MyController", typeof(VerifyControllerDescriptorBaseController));
Assert.Equal(typeof(VerifyControllerDescriptorBaseController), VerifyControllerDescriptorAttribute.ControllerType);
}
[Fact]
public void EmptySetting_DoesNotChangeTheConfigurationInstance()
{
HttpConfiguration config = new HttpConfiguration();
HttpControllerDescriptor desc = new HttpControllerDescriptor(config, "MyController", typeof(NoopControllerConfigController));
Assert.Same(config, desc.Configuration); // didn't change anything, the config instance stays the same
}
[Fact]
public void GetCustomAttributes_GetsInheritedAttributes()
{
HttpConfiguration config = new HttpConfiguration();
HttpControllerDescriptor desc = new HttpControllerDescriptor(config, "MyController", typeof(MyDerived1Controller));
var attributes = desc.GetCustomAttributes<MyConfigBaseAttribute>();
Assert.Single(attributes);
}
[Fact]
public void GetCustomAttributesInheritTrue_GetsInheritedAttributes()
{
HttpConfiguration config = new HttpConfiguration();
HttpControllerDescriptor desc = new HttpControllerDescriptor(config, "MyController", typeof(MyDerived1Controller));
var attributes = desc.GetCustomAttributes<MyConfigBaseAttribute>(inherit: true);
Assert.Single(attributes);
}
[Fact]
public void GetCustomAttributesInheritFalse_DoesNotGetInheritedAttributes()
{
HttpConfiguration config = new HttpConfiguration();
HttpControllerDescriptor desc = new HttpControllerDescriptor(config, "MyController", typeof(MyDerived1Controller));
var attributes = desc.GetCustomAttributes<MyConfigBaseAttribute>(inherit: false);
Assert.Empty(attributes);
}
[Fact]
public void GetCustomAttributesInheritFalse_GetsDeclaredAttributes()
{
HttpConfiguration config = new HttpConfiguration();
HttpControllerDescriptor desc = new HttpControllerDescriptor(config, "MyController", typeof(MyDerived1Controller));
var attributes = desc.GetCustomAttributes<MyConfigDerived1Attribute>(inherit: false);
Assert.Single(attributes);
}
class NoopControllerConfigAttribute : Attribute, IControllerConfiguration
{
public void Initialize(HttpControllerSettings controllerSettings, HttpControllerDescriptor controllerDescriptor)
{
}
}
[NoopControllerConfig]
class NoopControllerConfigController : ApiController
{
}
class VerifyControllerDescriptorAttribute : Attribute, IControllerConfiguration
{
public static Type ControllerType;
public void Initialize(HttpControllerSettings settings, HttpControllerDescriptor controllerDescriptor)
{
ControllerType = controllerDescriptor.ControllerType;
}
}
[VerifyControllerDescriptor]
class VerifyControllerDescriptorBaseController : ApiController
{
}
class VerifyControllerDescriptorDerivedController : VerifyControllerDescriptorBaseController
{
}
class MyConfigBaseAttribute : Attribute, IControllerConfiguration
{
public void Initialize(HttpControllerSettings settings, HttpControllerDescriptor controllerDescriptor)
{
settings.Services.Replace(typeof(IActionValueBinder), MyBaseController.ActionValueBinderBase);
settings.Services.Replace(typeof(IHttpActionSelector), MyBaseController.SelectorBase);
}
}
[MyConfigBase]
class MyBaseController : ApiController
{
public static IHttpActionSelector SelectorBase = new Mock<IHttpActionSelector>().Object;
public static IActionValueBinder ActionValueBinderBase = new Mock<IActionValueBinder>().Object;
}
class MyConfigDerived1Attribute : Attribute, IControllerConfiguration
{
public void Initialize(HttpControllerSettings settings, HttpControllerDescriptor controllerDescriptor)
{
// Base runs first, so we should be able to see changes from the base.
Assert.Same(MyBaseController.ActionValueBinderBase, settings.Services.GetActionValueBinder());
// Also overwrite them
settings.Services.Replace(typeof(IActionValueBinder), MyDerived1Controller.ActionValueBinderDerived1);
}
}
class MyConfigDerived3Attribute : Attribute, IControllerConfiguration
{
public void Initialize(HttpControllerSettings settings, HttpControllerDescriptor controllerDescriptor)
{
// MyConfigDerived1 runs first, so we should be able to see changes from the MyConfigDerived1.
Assert.Same(MyDerived1Controller.ActionValueBinderDerived1, settings.Services.GetActionValueBinder());
// Also overwrite them
settings.Services.Replace(typeof(IActionValueBinder), MyDerived3Controller.ActionValueBinderDerived3);
}
}
[MyConfigDerived1]
class MyDerived1Controller : MyBaseController
{
public static IActionValueBinder ActionValueBinderDerived1 = new Mock<IActionValueBinder>().Object;
}
class MyDerived2Controller : MyDerived1Controller
{
}
[MyConfigDerived3]
class MyDerived3Controller : MyDerived1Controller
{
public static IActionValueBinder ActionValueBinderDerived3 = new Mock<IActionValueBinder>().Object;
}
[Fact]
public void Initialize_Append_A_Formatter()
{
// Verifies that controller inherit the formatter list from the global config, and can mutate it.
HttpConfiguration config = new HttpConfiguration();
MediaTypeFormatter globalFormatter = new Mock<MediaTypeFormatter>().Object;
config.Formatters.Clear();
config.Formatters.Add(globalFormatter);
// Act.
HttpControllerDescriptor desc = new HttpControllerDescriptor(config, "MyController", typeof(MyControllerWithCustomFormatter));
// Assert
Assert.Equal(2, desc.Configuration.Formatters.Count);
Assert.Same(globalFormatter, desc.Configuration.Formatters[0]);
Assert.Same(MyControllerWithCustomFormatter.CustomFormatter, desc.Configuration.Formatters[1]);
}
class MyControllerWithCustomFormatterConfigAttribute : Attribute, IControllerConfiguration
{
public void Initialize(HttpControllerSettings settings, HttpControllerDescriptor controllerDescriptor)
{
// Appends to existing list. Formatter list has copy-on-write semantics.
Assert.Single(settings.Formatters); // the one we already set
settings.Formatters.Add(MyControllerWithCustomFormatter.CustomFormatter);
}
}
[MyControllerWithCustomFormatterConfig]
class MyControllerWithCustomFormatter : ApiController
{
public static MediaTypeFormatter CustomFormatter = new Mock<MediaTypeFormatter>().Object;
}
}
}