forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGlobalConfigurationTest.cs
More file actions
175 lines (148 loc) · 5.76 KB
/
Copy pathGlobalConfigurationTest.cs
File metadata and controls
175 lines (148 loc) · 5.76 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.Dispatcher;
using System.Web.Http.ExceptionHandling;
using System.Web.Http.Hosting;
using System.Web.Http.WebHost;
using System.Web.Http.WebHost.Routing;
using Microsoft.TestCommon;
namespace System.Web.Http
{
public class GlobalConfigurationTest
{
[Fact]
public void ConfigurationRouteCollection_IsHostedHttpRouteCollection()
{
// Arrange
HttpConfiguration configuration = GlobalConfiguration.Configuration;
Assert.NotNull(configuration); // Guard
// Act
HttpRouteCollection routes = configuration.Routes;
// Assert
Assert.IsType<HostedHttpRouteCollection>(routes);
// Note that there is currently no easy way to test that it references RouteTable.Routes.
}
[Theory]
[InlineData(typeof(IAssembliesResolver), typeof(WebHostAssembliesResolver))]
[InlineData(typeof(IHttpControllerTypeResolver), typeof(WebHostHttpControllerTypeResolver))]
[InlineData(typeof(IHostBufferPolicySelector), typeof(WebHostBufferPolicySelector))]
[InlineData(typeof(IExceptionHandler), typeof(WebHostExceptionHandler))]
public void ConfigurationService_IsWebHost(Type serviceInterfaceType, Type expectedImplementationType)
{
// Arrange
HttpConfiguration configuration = GlobalConfiguration.Configuration;
Assert.NotNull(configuration); // Guard
Assert.NotNull(configuration.Services); // Guard
// Act
object service = configuration.Services.GetService(serviceInterfaceType);
// Assert
Assert.IsType(expectedImplementationType, service);
}
[Fact]
public void ExceptionHandler_WrapsDefaultExceptionHandler()
{
// Arrange
HttpConfiguration configuration = GlobalConfiguration.Configuration;
Assert.NotNull(configuration); // Guard
Assert.NotNull(configuration.Services); // Guard
Type defaultExceptionHandlerType;
using (HttpConfiguration standardConfiguration = new HttpConfiguration())
{
defaultExceptionHandlerType = standardConfiguration.Services.GetExceptionHandler().GetType();
}
// Act
object service = configuration.Services.GetService(typeof(IExceptionHandler));
// Assert
WebHostExceptionHandler typedHandler = Assert.IsType<WebHostExceptionHandler>(service); // Guard
Assert.IsType(defaultExceptionHandlerType, typedHandler.InnerHandler);
}
[Fact]
public void DefaultHandler_IsHttpRoutingDispatcher()
{
// Act
HttpMessageHandler handler = GlobalConfiguration.DefaultHandler;
// Assert
Assert.IsType<HttpRoutingDispatcher>(handler);
// Note that there is currently no easy way to test it references GlobalConfiguration.Configuration.
}
[Fact]
public void DefaultServer_ReferencesConfiguration()
{
// Act
HttpServer server = GlobalConfiguration.DefaultServer;
// Assert
Assert.NotNull(server);
Assert.Same(GlobalConfiguration.Configuration, server.Configuration);
}
[Fact]
public void DefaultServer_ReferencesDefaultHandler()
{
// Act
HttpServer server = GlobalConfiguration.DefaultServer;
// Assert
Assert.NotNull(server);
Assert.Same(GlobalConfiguration.DefaultHandler, server.Dispatcher);
}
[Fact]
public void Configure_Throws_WhenConfigurationCallbackIsNull()
{
// Arrange
using (new GlobalConfigurationContext())
{
// Act & Assert
Assert.ThrowsArgumentNull(() => { GlobalConfiguration.Configure(null); }, "configurationCallback");
}
}
[Fact]
public void Configure_CallsCallbackOnceWithGlobalConfiguration()
{
// Arrange
using (new GlobalConfigurationContext())
{
int calls = 0;
HttpConfiguration configuration = null;
Action<HttpConfiguration> callback = (c) =>
{
calls++;
configuration = c;
};
// Act
GlobalConfiguration.Configure(callback);
// Assert
Assert.Equal(1, calls);
Assert.Same(GlobalConfiguration.Configuration, configuration);
}
}
[Fact]
public void Configure_CallsConfigurationEnsureInitialized()
{
// Arrange
using (new GlobalConfigurationContext())
{
bool initialized = false;
GlobalConfiguration.Configuration.Initializer = (c) =>
{
initialized = true;
};
Action<HttpConfiguration> callback = (ignore) => { };
// Act
GlobalConfiguration.Configure(callback);
// Assert
Assert.True(initialized);
}
}
private sealed class GlobalConfigurationContext : IDisposable
{
bool disposed;
public void Dispose()
{
if (!disposed)
{
GlobalConfiguration.Reset();
disposed = true;
}
}
}
}
}