forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWebApiAppBuilderExtensionsTest.cs
More file actions
339 lines (288 loc) · 14.1 KB
/
Copy pathWebApiAppBuilderExtensionsTest.cs
File metadata and controls
339 lines (288 loc) · 14.1 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
// 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.Threading;
using System.Threading.Tasks;
using System.Web.Http.ExceptionHandling;
using System.Web.Http.Hosting;
using Microsoft.TestCommon;
using Moq;
using Owin;
namespace System.Web.Http.Owin
{
public class WebApiAppBuilderExtensionsTest
{
[Fact]
public void UseWebApiWithConfiguration_IfBuilderIsNull_Throws()
{
// Arrange
IAppBuilder builder = null;
using (HttpConfiguration configuration = CreateConfiguration())
{
// Act & Assert
Assert.ThrowsArgumentNull(() => WebApiAppBuilderExtensions.UseWebApi(builder, configuration),
"builder");
}
}
[Fact]
public void UseWebApiWithConfiguration_IfConfigurationIsNull_Throws()
{
// Arrange
IAppBuilder builder = CreateDummyAppBuilder();
HttpConfiguration configuration = null;
// Act & Assert
Assert.ThrowsArgumentNull(() => WebApiAppBuilderExtensions.UseWebApi(builder, configuration),
"configuration");
}
[Fact]
public void UseWebApiWithServer_IfBuilderIsNull_Throws()
{
// Arrange
IAppBuilder builder = null;
using (HttpServer httpServer = CreateServer())
{
// Act & Assert
Assert.ThrowsArgumentNull(() => WebApiAppBuilderExtensions.UseWebApi(builder, httpServer), "builder");
}
}
[Fact]
public void UseWebApiWithServer_IfServerIsNull_Throws()
{
// Arrange
IAppBuilder builder = CreateDummyAppBuilder();
HttpServer httpServer = null;
// Act & Assert
Assert.ThrowsArgumentNull(() => WebApiAppBuilderExtensions.UseWebApi(builder, httpServer), "httpServer");
}
[Fact]
public void UseWebApi_UsesAdapter()
{
var config = new HttpConfiguration();
var appBuilder = new Mock<IAppBuilder>();
appBuilder
.Setup(ab => ab.Use(
typeof(HttpMessageHandlerAdapter),
It.Is<HttpMessageHandlerOptions>((o) => ((HttpServer)o.MessageHandler).Configuration == config)))
.Returns(appBuilder.Object)
.Verifiable();
IAppBuilder returnedAppBuilder = appBuilder.Object.UseWebApi(config);
Assert.Equal(appBuilder.Object, returnedAppBuilder);
appBuilder.Verify();
}
[Fact]
public async Task UseWebApi_UsesAdapterAndConfigServices()
{
using (CancellationTokenSource tokenSource = CreateCancellationTokenSource())
{
var config = new HttpConfiguration();
var bufferPolicySelector = new Mock<IHostBufferPolicySelector>().Object;
Mock<IExceptionLogger> loggerMock = new Mock<IExceptionLogger>();
Mock<IExceptionHandler> handlerMock = new Mock<IExceptionHandler>();
config.Services.Replace(typeof(IHostBufferPolicySelector), bufferPolicySelector);
config.Services.Replace(typeof(IExceptionLogger), loggerMock.Object);
config.Services.Replace(typeof(IExceptionHandler), handlerMock.Object);
IExceptionLogger exceptionLogger = null;
IExceptionHandler exceptionHandler = null;
var appBuilder = new Mock<IAppBuilder>();
CancellationToken expectedAppDisposing = tokenSource.Token;
IDictionary<string, object> properties = new Dictionary<string, object>
{
{ "host.OnAppDisposing", expectedAppDisposing }
};
appBuilder.SetupGet(b => b.Properties).Returns(properties);
appBuilder
.Setup(ab => ab.Use(
typeof(HttpMessageHandlerAdapter),
It.Is<HttpMessageHandlerOptions>((o) => ((HttpServer)o.MessageHandler).Configuration == config
&& o.BufferPolicySelector == bufferPolicySelector
&& o.AppDisposing == expectedAppDisposing)))
.Callback<object, object[]>((i, args) =>
{
HttpMessageHandlerOptions options = (HttpMessageHandlerOptions)args[0];
exceptionLogger = options.ExceptionLogger;
exceptionHandler = options.ExceptionHandler;
})
.Returns(appBuilder.Object)
.Verifiable();
IAppBuilder returnedAppBuilder = appBuilder.Object.UseWebApi(config);
Assert.Equal(appBuilder.Object, returnedAppBuilder);
appBuilder.Verify();
await AssertDelegatesToAsync(loggerMock, exceptionLogger);
await AssertDelegatesToAsync(handlerMock, exceptionHandler);
}
}
[Fact]
public void UseWebApiWithHttpServer_UsesAdapter()
{
// Arrange
HttpServer httpServer = new Mock<HttpServer>().Object;
Mock<IAppBuilder> appBuilderMock = new Mock<IAppBuilder>();
appBuilderMock
.Setup(ab => ab.Use(
typeof(HttpMessageHandlerAdapter),
It.Is<HttpMessageHandlerOptions>((o) => o.MessageHandler == httpServer)))
.Returns(appBuilderMock.Object)
.Verifiable();
// Act
IAppBuilder returnedAppBuilder = appBuilderMock.Object.UseWebApi(httpServer);
// Assert
Assert.Equal(appBuilderMock.Object, returnedAppBuilder);
appBuilderMock.Verify();
}
[Fact]
public async Task UseWebApiWithHttpServer_UsesAdapterAndConfigServices()
{
// Arrange
using (CancellationTokenSource tokenSource = CreateCancellationTokenSource())
{
HttpConfiguration config = new HttpConfiguration();
IHostBufferPolicySelector bufferPolicySelector = new Mock<IHostBufferPolicySelector>().Object;
Mock<IExceptionLogger> loggerMock = new Mock<IExceptionLogger>();
Mock<IExceptionHandler> handlerMock = new Mock<IExceptionHandler>();
config.Services.Replace(typeof(IHostBufferPolicySelector), bufferPolicySelector);
config.Services.Replace(typeof(IExceptionLogger), loggerMock.Object);
config.Services.Replace(typeof(IExceptionHandler), handlerMock.Object);
HttpServer httpServer = new Mock<HttpServer>(config).Object;
IExceptionLogger exceptionLogger = null;
IExceptionHandler exceptionHandler = null;
Mock<IAppBuilder> appBuilderMock = new Mock<IAppBuilder>();
CancellationToken expectedAppDisposing = tokenSource.Token;
IDictionary<string, object> properties = new Dictionary<string, object>
{
{ "host.OnAppDisposing", expectedAppDisposing }
};
appBuilderMock.SetupGet(b => b.Properties).Returns(properties);
appBuilderMock
.Setup(ab => ab.Use(
typeof(HttpMessageHandlerAdapter),
It.Is<HttpMessageHandlerOptions>((o) => o.MessageHandler == httpServer
&& o.BufferPolicySelector == bufferPolicySelector
&& o.AppDisposing == expectedAppDisposing)))
.Callback<object, object[]>((i, args) =>
{
HttpMessageHandlerOptions options = (HttpMessageHandlerOptions)args[0];
exceptionLogger = options.ExceptionLogger;
exceptionHandler = options.ExceptionHandler;
})
.Returns(appBuilderMock.Object)
.Verifiable();
// Act
IAppBuilder returnedAppBuilder = appBuilderMock.Object.UseWebApi(httpServer);
// Assert
Assert.Equal(appBuilderMock.Object, returnedAppBuilder);
appBuilderMock.Verify();
await AssertDelegatesToAsync(loggerMock, exceptionLogger);
await AssertDelegatesToAsync(handlerMock, exceptionHandler);
}
}
[Fact]
public void GetOnAppDisposingProperty_IfPropertiesIsPresent_ReturnsSpecifiedValue()
{
// Arrange
using (CancellationTokenSource tokenSource = CreateCancellationTokenSource())
{
CancellationToken expectedOnAppDisposing = tokenSource.Token;
IDictionary<string, object> properties = CreateStubOnAppDisposingDictionary(expectedOnAppDisposing);
IAppBuilder builder = CreateStubAppBuilder(properties);
// Act
CancellationToken onAppDisposing = builder.GetOnAppDisposingProperty();
// Assert
Assert.Equal(expectedOnAppDisposing, onAppDisposing);
}
}
[Fact]
public void GetOnAppDisposingProperty_IfPropertiesIsNull_ReturnsCancellationTokenNone()
{
// Arrange
IDictionary<string, object> properties = null;
IAppBuilder builder = CreateStubAppBuilder(properties);
// Act
CancellationToken onAppDisposing = WebApiAppBuilderExtensions.GetOnAppDisposingProperty(builder);
// Assert
Assert.Equal(CancellationToken.None, onAppDisposing);
}
[Fact]
public void GetOnAppDisposingProperty_IfPropertyIsAbsent_ReturnsCancellationTokenNone()
{
// Arrange
IDictionary<string, object> properties = CreateStubOnAppDisposingDictionary(onAppDisposing: null, hasOnAppDisposing: false);
IAppBuilder builder = CreateStubAppBuilder(properties);
// Act
CancellationToken onAppDisposing = WebApiAppBuilderExtensions.GetOnAppDisposingProperty(builder);
// Assert
Assert.Equal(CancellationToken.None, onAppDisposing);
}
[Fact]
public void GetOnAppDisposingProperty_IfPropertyIsNonCancellationToken_ReturnsCancellationTokenNone()
{
// Arrange
object nonCancellationToken = new object();
IDictionary<string, object> properties = CreateStubOnAppDisposingDictionary(nonCancellationToken);
IAppBuilder builder = CreateStubAppBuilder(properties);
// Act
CancellationToken onAppDisposing = WebApiAppBuilderExtensions.GetOnAppDisposingProperty(builder);
// Assert
Assert.Equal(CancellationToken.None, onAppDisposing);
}
private static async Task AssertDelegatesToAsync(Mock<IExceptionHandler> expected, IExceptionHandler actual)
{
Assert.NotNull(actual);
ExceptionHandlerContext context = new ExceptionHandlerContext(new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer));
CancellationToken cancellationToken = CancellationToken.None;
expected
.Setup((l) => l.HandleAsync(It.IsAny<ExceptionHandlerContext>(), It.IsAny<CancellationToken>()))
.Returns(CreateCanceledTask());
await Assert.ThrowsAsync<TaskCanceledException>(() => actual.HandleAsync(context, cancellationToken));
expected.Verify((l) => l.HandleAsync(context, cancellationToken), Times.Once());
}
private static async Task AssertDelegatesToAsync(Mock<IExceptionLogger> expected, IExceptionLogger actual)
{
Assert.NotNull(actual);
ExceptionLoggerContext context = new ExceptionLoggerContext(new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer));
CancellationToken cancellationToken = CancellationToken.None;
expected
.Setup((l) => l.LogAsync(It.IsAny<ExceptionLoggerContext>(), It.IsAny<CancellationToken>()))
.Returns(CreateCanceledTask());
await Assert.ThrowsAsync<TaskCanceledException>(() => actual.LogAsync(context, cancellationToken));
expected.Verify((l) => l.LogAsync(context, cancellationToken), Times.Once());
}
private static Task CreateCanceledTask()
{
TaskCompletionSource<object> source = new TaskCompletionSource<object>();
source.SetCanceled();
return source.Task;
}
private static CancellationTokenSource CreateCancellationTokenSource()
{
return new CancellationTokenSource();
}
private static HttpConfiguration CreateConfiguration()
{
return new HttpConfiguration();
}
private static IAppBuilder CreateDummyAppBuilder()
{
return new Mock<IAppBuilder>(MockBehavior.Strict).Object;
}
private static HttpServer CreateServer()
{
return new HttpServer();
}
private static IAppBuilder CreateStubAppBuilder(IDictionary<string, object> properties)
{
Mock<IAppBuilder> mock = new Mock<IAppBuilder>(MockBehavior.Strict);
mock.SetupGet(b => b.Properties).Returns(properties);
return mock.Object;
}
private static IDictionary<string, object> CreateStubOnAppDisposingDictionary(object onAppDisposing)
{
return CreateStubOnAppDisposingDictionary(onAppDisposing, hasOnAppDisposing: true);
}
private static IDictionary<string, object> CreateStubOnAppDisposingDictionary(object onAppDisposing, bool hasOnAppDisposing)
{
Mock<IDictionary<string, object>> mock = new Mock<IDictionary<string, object>>();
mock.Setup(d => d.TryGetValue("host.OnAppDisposing", out onAppDisposing)).Returns(hasOnAppDisposing);
return mock.Object;
}
}
}