forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultHttpControllerActivatorTest.cs
More file actions
175 lines (151 loc) · 8.15 KB
/
Copy pathDefaultHttpControllerActivatorTest.cs
File metadata and controls
175 lines (151 loc) · 8.15 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
// 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.Net.Http;
using System.Web.Http.Controllers;
using System.Web.Http.Dependencies;
using System.Web.Http.Hosting;
using Microsoft.TestCommon;
using Moq;
namespace System.Web.Http.Dispatcher
{
public class DefaultHttpControllerActivatorTest
{
// Create tests
[Fact]
public void Create_GuardClauses()
{
// Arrange
var config = new HttpConfiguration();
var request = new HttpRequestMessage();
request.SetConfiguration(config);
var descriptor = new HttpControllerDescriptor(config, "Simple", typeof(SimpleController));
var activator = new DefaultHttpControllerActivator();
// Act & assert
Assert.ThrowsArgumentNull(() => activator.Create(request: null, controllerDescriptor: descriptor, controllerType: typeof(SimpleController)), "request");
Assert.ThrowsArgumentNull(() => activator.Create(request, controllerDescriptor: null, controllerType: typeof(SimpleController)), "controllerDescriptor");
Assert.ThrowsArgumentNull(() => activator.Create(request, descriptor, controllerType: null), "controllerType");
Assert.Throws<InvalidOperationException>(
() => activator.Create(request, descriptor, typeof(AbstractController)),
"An error occurred when trying to create a controller of type 'AbstractController'. Make sure that the controller has a parameterless public constructor.");
Assert.Throws<InvalidOperationException>(
() => activator.Create(request, descriptor, typeof(ControllerWithCtorParams)),
"An error occurred when trying to create a controller of type 'ControllerWithCtorParams'. Make sure that the controller has a parameterless public constructor.");
}
[Fact]
public void Create_MakesInstanceOfController()
{
// Arrange
var config = new HttpConfiguration();
var request = new HttpRequestMessage();
request.SetConfiguration(config);
var descriptor = new HttpControllerDescriptor(config, "Simple", typeof(SimpleController));
var activator = new DefaultHttpControllerActivator();
// Act
IHttpController result = activator.Create(request, descriptor, typeof(SimpleController));
// Assert
Assert.NotNull(result);
Assert.IsType<SimpleController>(result);
}
[Fact]
public void Create_UsesControllerFromRequestLevelDependencyScope()
{
// Arrange
var controller = new ControllerWithCtorParams(42);
var mockScope = new Mock<IDependencyScope>();
mockScope.Setup(r => r.GetService(typeof(ControllerWithCtorParams))).Returns(controller).Verifiable();
var config = new HttpConfiguration();
var request = new HttpRequestMessage();
request.SetConfiguration(config);
request.Properties[HttpPropertyKeys.DependencyScope] = mockScope.Object;
var descriptor = new HttpControllerDescriptor(config, "Name", typeof(ControllerWithCtorParams));
var activator = new DefaultHttpControllerActivator();
// Act
IHttpController result = activator.Create(request, descriptor, typeof(ControllerWithCtorParams));
// Assert
Assert.Same(controller, result);
mockScope.Verify();
}
[Fact]
public void Create_DoesnotCacheControllerFromRequestLevelDependencyScope()
{
// Arrange
int count = 0;
var controller = new ControllerWithCtorParams(42);
var mockScope = new Mock<IDependencyScope>();
mockScope.Setup(r => r.GetService(typeof(ControllerWithCtorParams))).Returns(() =>
{
count++;
return new ControllerWithCtorParams(42);
}).Verifiable();
var config = new HttpConfiguration();
var request = new HttpRequestMessage();
request.SetConfiguration(config);
request.Properties[HttpPropertyKeys.DependencyScope] = mockScope.Object;
var descriptor = new HttpControllerDescriptor(config, "Name", typeof(ControllerWithCtorParams));
var activator = new DefaultHttpControllerActivator();
// Act
IHttpController result1 = activator.Create(request, descriptor, typeof(ControllerWithCtorParams));
IHttpController result2 = activator.Create(request, descriptor, typeof(ControllerWithCtorParams));
// Assert
Assert.NotEqual(result1, result2);
mockScope.Verify();
Assert.Equal(2, count);
}
[Fact]
public void Create_MixupInstanceCreationAndDependencyScope()
{
// Arrange
var controller = new ControllerWithCtorParams(42);
var mockScope = new Mock<IDependencyScope>();
mockScope.Setup(r => r.GetService(typeof(ControllerWithCtorParams))).Returns(controller).Verifiable();
var config = new HttpConfiguration();
var request = new HttpRequestMessage();
request.SetConfiguration(config);
request.Properties[HttpPropertyKeys.DependencyScope] = mockScope.Object;
var descriptorControllerWithCtorParamsResult = new HttpControllerDescriptor(config, "Name", typeof(ControllerWithCtorParams));
var descriptorSimpleController = new HttpControllerDescriptor(config, "Simple", typeof(SimpleController));
var activator = new DefaultHttpControllerActivator();
// Act
IHttpController simpleController = activator.Create(request, descriptorSimpleController, typeof(SimpleController));
IHttpController controllerWithCtorParamsResult = activator.Create(request, descriptorControllerWithCtorParamsResult, typeof(ControllerWithCtorParams));
// Assert
Assert.NotNull(simpleController);
Assert.IsType<SimpleController>(simpleController);
Assert.Same(controller, controllerWithCtorParamsResult);
mockScope.Verify();
}
[Fact]
public void Create_ThrowsForNullDependencyScope()
{
// Arrange
var config = new HttpConfiguration();
var mockResolver = new Mock<IDependencyResolver>();
mockResolver.Setup(resolver => resolver.BeginScope()).Returns((IDependencyScope)null).Verifiable();
config.DependencyResolver = mockResolver.Object;
var request = new HttpRequestMessage();
request.SetConfiguration(config);
var descriptorSimpleController = new HttpControllerDescriptor(config, "Simple", typeof(SimpleController));
var activator = new DefaultHttpControllerActivator();
// Act & Assert
InvalidOperationException exception = Assert.Throws<InvalidOperationException>(
() => activator.Create(request, descriptorSimpleController, typeof(SimpleController)));
Assert.Equal(
"An error occurred when trying to create a controller of type 'SimpleController'. Make sure that the controller has a parameterless public constructor.",
exception.Message);
Assert.NotNull(exception.InnerException);
Assert.Matches(
"A dependency resolver of type 'IDependencyResolverProxy' returned an invalid value of null from its " +
"BeginScope method. If the container does not have a concept of scope, consider returning a scope " +
"that resolves in the root of the container instead.",
exception.InnerException.Message);
mockResolver.Verify();
}
// Helper classes
abstract class AbstractController : ApiController { }
class SimpleController : ApiController { }
class ControllerWithCtorParams : ApiController
{
public ControllerWithCtorParams(int unused) { }
}
}
}