forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionLoggerTests.cs
More file actions
313 lines (254 loc) · 11.2 KB
/
Copy pathExceptionLoggerTests.cs
File metadata and controls
313 lines (254 loc) · 11.2 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
// 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;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using Microsoft.TestCommon;
using Moq;
namespace System.Web.Http.ExceptionHandling
{
public class ExceptionLoggerTests
{
[Fact]
public void LogAsync_IfContextIsNull_Throws()
{
// Arrange
Mock<ExceptionLogger> mock = new Mock<ExceptionLogger> { CallBase = true };
IExceptionLogger product = mock.Object;
ExceptionLoggerContext context = null;
CancellationToken cancellationToken = CancellationToken.None;
// Act & Assert
Assert.ThrowsArgumentNull(() => product.LogAsync(context, cancellationToken), "context");
}
[Fact]
public void LogAsync_IfShouldLogReturnsTrue_DelegatesToLogAsyncCore()
{
// Arrange
Mock<ExceptionLogger> mock = new Mock<ExceptionLogger> { CallBase = true };
Task expectedTask = CreateCompletedTask();
mock.Setup(h => h.ShouldLog(It.IsAny<ExceptionLoggerContext>())).Returns(true);
mock
.Setup(h => h.LogAsync(It.IsAny<ExceptionLoggerContext>(), It.IsAny<CancellationToken>()))
.Returns(expectedTask);
IExceptionLogger product = mock.Object;
ExceptionLoggerContext expectedContext = CreateMinimalValidLoggerContext();
using (CancellationTokenSource tokenSource = CreateTokenSource())
{
CancellationToken expectedCancellationToken = tokenSource.Token;
// Act
Task task = product.LogAsync(expectedContext, expectedCancellationToken);
// Assert
Assert.Same(expectedTask, task);
mock.Verify(h => h.ShouldLog(expectedContext), Times.Once());
mock.Verify(h => h.LogAsync(expectedContext, expectedCancellationToken), Times.Once());
}
}
[Fact]
public void LogAsync_IfShouldLogReturnsFalse_ReturnsCompletedTask()
{
// Arrange
Mock<ExceptionLogger> mock = new Mock<ExceptionLogger> { CallBase = true };
mock.Setup(h => h.ShouldLog(It.IsAny<ExceptionLoggerContext>())).Returns(false);
IExceptionLogger product = mock.Object;
ExceptionLoggerContext expectedContext = CreateMinimalValidLoggerContext();
CancellationToken expectedCancellationToken = CancellationToken.None;
// Act
Task task = product.LogAsync(expectedContext, expectedCancellationToken);
// Assert
Assert.NotNull(task);
Assert.True(task.IsCompleted);
Assert.Equal(TaskStatus.RanToCompletion, task.Status);
mock.Verify(h => h.ShouldLog(expectedContext), Times.Once());
mock.Verify(h => h.LogAsync(It.IsAny<ExceptionLoggerContext>(), It.IsAny<CancellationToken>()),
Times.Never());
}
[Fact]
public async Task LogAsyncCore_DelegatesToLogCore_AndReturnsCompletedTask()
{
// Arrange
Mock<ExceptionLogger> mock = new Mock<ExceptionLogger>();
mock.CallBase = true;
ExceptionLogger product = mock.Object;
ExceptionLoggerContext expectedContext = CreateMinimalValidLoggerContext();
CancellationToken cancellationToken = CancellationToken.None;
// Act
await product.LogAsync(expectedContext, cancellationToken);
// Assert
mock.Verify(h => h.Log(expectedContext), Times.Once());
}
[Fact]
public void LogCore_DoesNotThrow()
{
// Arrange
Mock<ExceptionLogger> mock = new Mock<ExceptionLogger>();
mock.CallBase = true;
ExceptionLogger product = mock.Object;
ExceptionLoggerContext context = CreateMinimalValidLoggerContext();
// Act & Assert
Assert.DoesNotThrow(() => product.Log(context));
}
[Fact]
public void ShouldLog_IfContextIsNull_Throws()
{
// Arrange
Mock<ExceptionLogger> mock = new Mock<ExceptionLogger>();
mock.CallBase = true;
ExceptionLogger product = mock.Object;
ExceptionLoggerContext context = null;
// Act & Assert
Assert.ThrowsArgumentNull(() => product.ShouldLog(context), "context");
}
[Fact]
public void ShouldLog_IfExceptionDataIsNull_ReturnsTrue()
{
// Arrange
Mock<ExceptionLogger> mock = new Mock<ExceptionLogger>();
mock.CallBase = true;
ExceptionLogger product = mock.Object;
Exception exception = CreateException(data: null);
ExceptionLoggerContext context = CreateMinimalValidLoggerContext(exception);
// Act
bool shouldLog = product.ShouldLog(context);
// Assert
Assert.True(shouldLog);
}
[Fact]
public void LoggedByKey_IsSpecifiedValue()
{
// Act & Assert
Assert.Equal("MS_LoggedBy", ExceptionLogger.LoggedByKey);
}
[Fact]
public void ShouldLog_IfExceptionDataIsReadOnly_ReturnsTrue()
{
// Arrange
Mock<ExceptionLogger> mock = new Mock<ExceptionLogger>();
mock.CallBase = true;
ExceptionLogger product = mock.Object;
Mock<IDictionary> dataMock = new Mock<IDictionary>(MockBehavior.Strict);
dataMock.Setup(d => d.IsReadOnly).Returns(true);
IDictionary data = dataMock.Object;
Exception exception = CreateException(data);
ExceptionLoggerContext context = CreateMinimalValidLoggerContext(exception);
// Act
bool shouldLog = product.ShouldLog(context);
// Assert
Assert.True(shouldLog);
}
[Fact]
public void ShouldLog_IfExceptionDataIsEmpty_AddsLoggedByKeyWithLoggerValueAndReturnsTrue()
{
// Arrange
Mock<ExceptionLogger> mock = new Mock<ExceptionLogger>();
mock.CallBase = true;
ExceptionLogger product = mock.Object;
IDictionary data = new Dictionary();
Exception exception = CreateException(data);
ExceptionLoggerContext context = CreateMinimalValidLoggerContext(exception);
// Act
bool shouldLog = product.ShouldLog(context);
// Assert
Assert.True(shouldLog);
Assert.True(data.Contains(ExceptionLogger.LoggedByKey));
object loggedBy = data[ExceptionLogger.LoggedByKey];
ICollection<object> loggedByCollection = Assert.IsAssignableFrom<ICollection<object>>(loggedBy);
Assert.Contains(product, loggedByCollection);
}
[Fact]
public void ShouldLog_IfExceptionDataHasEmptyLoggedBy_AddsLoggerAndReturnsTrue()
{
// Arrange
Mock<ExceptionLogger> mock = new Mock<ExceptionLogger>();
mock.CallBase = true;
ExceptionLogger product = mock.Object;
ICollection<object> loggedBy = new List<object>();
IDictionary data = new Dictionary();
data.Add(ExceptionLogger.LoggedByKey, loggedBy);
Exception exception = CreateException(data);
ExceptionLoggerContext context = CreateMinimalValidLoggerContext(exception);
// Act
bool shouldLog = product.ShouldLog(context);
// Assert
Assert.True(shouldLog);
Assert.True(data.Contains(ExceptionLogger.LoggedByKey));
object updatedLoggedBy = data[ExceptionLogger.LoggedByKey];
Assert.Same(loggedBy, updatedLoggedBy);
Assert.Contains(product, loggedBy);
}
[Fact]
public void ShouldLog_IfExceptionDataContainsLoggedByLogger_ReturnsFalse()
{
// Arrange
Mock<ExceptionLogger> mock = new Mock<ExceptionLogger>();
mock.CallBase = true;
ExceptionLogger product = mock.Object;
IDictionary data = new Dictionary();
ICollection<object> loggedBy = new List<object>() { product };
data.Add(ExceptionLogger.LoggedByKey, loggedBy);
Exception exception = CreateException(data);
ExceptionLoggerContext context = CreateMinimalValidLoggerContext(exception);
// Act
bool shouldLog = product.ShouldLog(context);
// Assert
Assert.False(shouldLog);
Assert.True(data.Contains(ExceptionLogger.LoggedByKey));
object updatedLoggedBy = data[ExceptionLogger.LoggedByKey];
Assert.Same(loggedBy, updatedLoggedBy);
Assert.Equal(1, loggedBy.Count);
}
[Fact]
public void ShouldLog_IfExceptionDataContainsLoggedByOfIncompatibleType_ReturnsTrue()
{
// Arrange
Mock<ExceptionLogger> mock = new Mock<ExceptionLogger>();
mock.CallBase = true;
ExceptionLogger product = mock.Object;
IDictionary data = new Dictionary();
data.Add(ExceptionLogger.LoggedByKey, null);
Exception exception = CreateException(data);
ExceptionLoggerContext context = CreateMinimalValidLoggerContext(exception);
// Act
bool shouldLog = product.ShouldLog(context);
// Assert
Assert.True(shouldLog);
Assert.True(data.Contains(ExceptionLogger.LoggedByKey));
object updatedLoggedBy = data[ExceptionLogger.LoggedByKey];
Assert.Null(updatedLoggedBy);
}
private static Task CreateCompletedTask()
{
TaskCompletionSource<object> source = new TaskCompletionSource<object>();
return source.Task;
}
private static ExceptionLoggerContext CreateContext(ExceptionContext exceptionContext)
{
return new ExceptionLoggerContext(exceptionContext);
}
private static Exception CreateException(IDictionary data)
{
Mock<Exception> mock = new Mock<Exception>();
mock.Setup(e => e.Data).Returns(data);
return mock.Object;
}
private static ExceptionContext CreateMinimalValidExceptionContext()
{
return new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer);
}
private static CancellationTokenSource CreateTokenSource()
{
return new CancellationTokenSource();
}
private static ExceptionLoggerContext CreateMinimalValidLoggerContext()
{
return CreateMinimalValidLoggerContext(new Exception());
}
private static ExceptionLoggerContext CreateMinimalValidLoggerContext(Exception exception)
{
return CreateContext(new ExceptionContext(exception, ExceptionCatchBlocks.HttpServer));
}
private class Dictionary : DictionaryBase
{
}
}
}