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
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Web.Http.Controllers;
using System.Web.Http.Dispatcher;
using System.Web.Http.Filters;
using Moq;
using Xunit;
using Assert = Microsoft.TestCommon.AssertEx;
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.Null(controllerDescriptor.HttpActionInvoker);
Assert.Null(controllerDescriptor.HttpActionSelector);
Assert.Null(controllerDescriptor.HttpControllerActivator);
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.HttpActionInvoker);
Assert.NotNull(controllerDescriptor.HttpActionSelector);
Assert.NotNull(controllerDescriptor.HttpControllerActivator);
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 HttpActionInvoker_Property()
{
IHttpActionInvoker invoker = new ApiControllerActionInvoker();
HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor();
Assert.Reflection.Property<HttpControllerDescriptor, IHttpActionInvoker>(
instance: controllerDescriptor,
propertyGetter: cd => cd.HttpActionInvoker,
expectedDefaultValue: null,
allowNull: false,
roundTripTestValue: invoker);
}
[Fact]
public void HttpActionSelector_Property()
{
IHttpActionSelector selector = new ApiControllerActionSelector();
HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor();
Assert.Reflection.Property<HttpControllerDescriptor, IHttpActionSelector>(
instance: controllerDescriptor,
propertyGetter: cd => cd.HttpActionSelector,
expectedDefaultValue: null,
allowNull: false,
roundTripTestValue: selector);
}
[Fact]
public void HttpControllerActivator_Property()
{
IHttpControllerActivator activator = new Mock<IHttpControllerActivator>().Object;
HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor();
Assert.Reflection.Property<HttpControllerDescriptor, IHttpControllerActivator>(
instance: controllerDescriptor,
propertyGetter: cd => cd.HttpControllerActivator,
expectedDefaultValue: null,
allowNull: false,
roundTripTestValue: activator);
}
[Fact]
public void ActionValueBinder_Property()
{
IActionValueBinder activator = new Mock<IActionValueBinder>().Object;
HttpControllerDescriptor controllerDescriptor = new HttpControllerDescriptor();
Assert.Reflection.Property<HttpControllerDescriptor, IActionValueBinder>(
instance: controllerDescriptor,
propertyGetter: cd => cd.ActionValueBinder,
expectedDefaultValue: null,
allowNull: false,
roundTripTestValue: activator);
}
[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();
}
}
}
|