forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionHandlerTests.cs
More file actions
172 lines (138 loc) · 6.23 KB
/
Copy pathExceptionHandlerTests.cs
File metadata and controls
172 lines (138 loc) · 6.23 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
// 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.Threading;
using System.Threading.Tasks;
using Microsoft.TestCommon;
using Moq;
namespace System.Web.Http.ExceptionHandling
{
public class ExceptionHandlerTests
{
[Fact]
public void HandleAsync_IfContextIsNull_Throws()
{
// Arrange
Mock<ExceptionHandler> mock = new Mock<ExceptionHandler> { CallBase = true };
IExceptionHandler product = mock.Object;
ExceptionHandlerContext context = null;
CancellationToken cancellationToken = CancellationToken.None;
// Act & Assert
Assert.ThrowsArgumentNull(() => product.HandleAsync(context, cancellationToken), "context");
}
[Fact]
public void HandleAsync_IfShouldHandleReturnsTrue_DelegatesToHandleAsyncCore()
{
// Arrange
Mock<ExceptionHandler> mock = new Mock<ExceptionHandler> { CallBase = true };
Task expectedTask = CreateCompletedTask();
mock.Setup(h => h.ShouldHandle(It.IsAny<ExceptionHandlerContext>())).Returns(true);
mock
.Setup(h => h.HandleAsync(It.IsAny<ExceptionHandlerContext>(), It.IsAny<CancellationToken>()))
.Returns(expectedTask);
IExceptionHandler product = mock.Object;
ExceptionHandlerContext expectedContext = CreateMinimalValidHandlerContext();
using (CancellationTokenSource tokenSource = CreateTokenSource())
{
CancellationToken expectedCancellationToken = tokenSource.Token;
// Act
Task task = product.HandleAsync(expectedContext, expectedCancellationToken);
// Assert
Assert.Same(expectedTask, task);
mock.Verify(h => h.ShouldHandle(expectedContext), Times.Once());
mock.Verify(h => h.HandleAsync(expectedContext, expectedCancellationToken), Times.Once());
}
}
[Fact]
public void HandleAsync_IfShouldHandleReturnsFalse_ReturnsCompletedTask()
{
// Arrange
Mock<ExceptionHandler> mock = new Mock<ExceptionHandler> { CallBase = true };
mock.Setup(h => h.ShouldHandle(It.IsAny<ExceptionHandlerContext>())).Returns(false);
IExceptionHandler product = mock.Object;
ExceptionHandlerContext expectedContext = CreateMinimalValidHandlerContext();
CancellationToken expectedCancellationToken = CancellationToken.None;
// Act
Task task = product.HandleAsync(expectedContext, expectedCancellationToken);
// Assert
Assert.NotNull(task);
Assert.True(task.IsCompleted);
Assert.Equal(TaskStatus.RanToCompletion, task.Status);
mock.Verify(h => h.ShouldHandle(expectedContext), Times.Once());
mock.Verify(h => h.HandleAsync(It.IsAny<ExceptionHandlerContext>(), It.IsAny<CancellationToken>()),
Times.Never());
}
[Fact]
public async Task HandleAsyncCore_DelegatesToHandleCore_AndReturnsCompletedTask()
{
// Arrange
Mock<ExceptionHandler> mock = new Mock<ExceptionHandler>();
mock.CallBase = true;
ExceptionHandler product = mock.Object;
ExceptionHandlerContext expectedContext = CreateMinimalValidHandlerContext();
CancellationToken cancellationToken = CancellationToken.None;
// Act
await product.HandleAsync(expectedContext, cancellationToken);
// Assert
mock.Verify(h => h.Handle(expectedContext), Times.Once());
}
[Fact]
public void HandleCore_DoesNotThrow()
{
// Arrange
Mock<ExceptionHandler> mock = new Mock<ExceptionHandler>();
mock.CallBase = true;
ExceptionHandler product = mock.Object;
ExceptionHandlerContext context = CreateMinimalValidHandlerContext();
// Act & Assert
Assert.DoesNotThrow(() => product.Handle(context));
}
[Fact]
public void ShouldHandle_IfContextIsNull_Throws()
{
// Arrange
Mock<ExceptionHandler> mock = new Mock<ExceptionHandler>();
mock.CallBase = true;
ExceptionHandler product = mock.Object;
ExceptionHandlerContext context = null;
// Act & Assert
Assert.ThrowsArgumentNull(() => product.ShouldHandle(context), "context");
}
[Theory]
[InlineData(true)]
[InlineData(false)]
public void ShouldHandle_ReturnsIsTopLevelCatchBlock(bool isTopLevelCatchBlock)
{
// Arrange
Mock<ExceptionHandler> mock = new Mock<ExceptionHandler>();
mock.CallBase = true;
ExceptionHandler product = mock.Object;
ExceptionHandlerContext context = CreateContext(new ExceptionContext(new Exception(),
new ExceptionContextCatchBlock("IgnoreCaughtAt", isTopLevelCatchBlock, callsHandler: false)));
// Act
bool shouldHandle = product.ShouldHandle(context);
// Assert
Assert.Equal(isTopLevelCatchBlock, shouldHandle);
}
private static Task CreateCompletedTask()
{
TaskCompletionSource<object> source = new TaskCompletionSource<object>();
return source.Task;
}
private static ExceptionHandlerContext CreateContext(ExceptionContext exceptionContext)
{
return new ExceptionHandlerContext(exceptionContext);
}
private static ExceptionContext CreateMinimalValidExceptionContext()
{
return new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer);
}
private static CancellationTokenSource CreateTokenSource()
{
return new CancellationTokenSource();
}
private static ExceptionHandlerContext CreateMinimalValidHandlerContext()
{
return new ExceptionHandlerContext(CreateMinimalValidExceptionContext());
}
}
}