forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExceptionHandlerExtensionsTests.cs
More file actions
191 lines (162 loc) · 7.43 KB
/
Copy pathExceptionHandlerExtensionsTests.cs
File metadata and controls
191 lines (162 loc) · 7.43 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
// 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.Threading;
using System.Threading.Tasks;
using Microsoft.TestCommon;
using Moq;
namespace System.Web.Http.ExceptionHandling
{
public class ExceptionHandlerExtensionsTests
{
[Fact]
public async Task HandleAsync_CallsInterfaceHandleAsync()
{
// Arrange
Mock<IExceptionHandler> mock = CreateStubHandlerMock();
IExceptionHandler handler = mock.Object;
using (CancellationTokenSource tokenSource = CreateCancellationTokenSource())
{
ExceptionContext expectedContext = CreateMinimalValidContext();
CancellationToken expectedCancellationToken = tokenSource.Token;
// Act
await ExceptionHandlerExtensions.HandleAsync(handler, expectedContext,
expectedCancellationToken);
// Assert
mock.Verify(h => h.HandleAsync(It.Is<ExceptionHandlerContext>(
c => c.ExceptionContext == expectedContext), expectedCancellationToken), Times.Once());
}
}
[Fact]
public async Task HandleAsync_IfResultIsNotSet_ReturnsCompletedTaskWithNullResponse()
{
// Arrange
IExceptionHandler handler = CreateStubHandler();
ExceptionContext context = CreateMinimalValidContext();
CancellationToken cancellationToken = CancellationToken.None;
// Act
HttpResponseMessage response = await ExceptionHandlerExtensions.HandleAsync(handler, context, cancellationToken);
// Assert
Assert.Null(response);
}
[Fact]
public async Task HandleAsync_IfResultIsSet_CallsResultExecuteAsync()
{
// Arrange
using (HttpResponseMessage response = CreateResponse())
{
Mock<IHttpActionResult> mock = new Mock<IHttpActionResult>(MockBehavior.Strict);
mock.Setup(r => r.ExecuteAsync(It.IsAny<CancellationToken>())).Returns(Task.FromResult(response));
IHttpActionResult result = mock.Object;
IExceptionHandler handler = CreateResultHandler(result);
using (CancellationTokenSource tokenSource = CreateCancellationTokenSource())
{
ExceptionContext context = CreateMinimalValidContext();
CancellationToken expectedCancellationToken = tokenSource.Token;
// Act
await ExceptionHandlerExtensions.HandleAsync(handler, context,
expectedCancellationToken);
// Assert
mock.Verify(h => h.ExecuteAsync(expectedCancellationToken), Times.Once());
}
}
}
[Fact]
public async Task HandleAsync_IfResultIsSet_ReturnsCompletedTaskWithResultResponse()
{
// Arrange
using (HttpResponseMessage expectedResponse = CreateResponse())
{
Mock<IHttpActionResult> mock = new Mock<IHttpActionResult>(MockBehavior.Strict);
mock.Setup(r => r.ExecuteAsync(It.IsAny<CancellationToken>())).Returns(Task.FromResult(expectedResponse));
IHttpActionResult result = mock.Object;
IExceptionHandler handler = CreateResultHandler(result);
ExceptionContext context = CreateMinimalValidContext();
CancellationToken cancellationToken = CancellationToken.None;
// Act
HttpResponseMessage response = await handler.HandleAsync(context, cancellationToken);
// Assert
Assert.Same(expectedResponse, response);
}
}
[Fact]
public void HandleAsync_IfHandlerIsNull_Throws()
{
// Arrange
IExceptionHandler handler = null;
ExceptionContext context = CreateMinimalValidContext();
CancellationToken cancellationToken = CancellationToken.None;
// Act & Assert
Assert.ThrowsArgumentNull(() =>
ExceptionHandlerExtensions.HandleAsync(handler, context, cancellationToken), "handler");
}
[Fact]
public void HandleAsync_IfContextIsNull_Throws()
{
// Arrange
IExceptionHandler handler = CreateDummyHandler();
ExceptionContext context = null;
CancellationToken cancellationToken = CancellationToken.None;
// Act & Assert
Assert.ThrowsArgumentNull(() =>
ExceptionHandlerExtensions.HandleAsync(handler, context, cancellationToken), "context");
}
[Fact]
public async Task HandleAsync_IfResultIsSetButReturnsNull_ReturnsFaultedTask()
{
// Arrange
Mock<IHttpActionResult> mock = new Mock<IHttpActionResult>(MockBehavior.Strict);
mock
.Setup(r => r.ExecuteAsync(It.IsAny<CancellationToken>()))
.Returns(Task.FromResult<HttpResponseMessage>(null));
IHttpActionResult result = mock.Object;
IExceptionHandler handler = CreateResultHandler(result);
ExceptionContext context = CreateMinimalValidContext();
CancellationToken cancellationToken = CancellationToken.None;
// Act & Assert
var exception = await Assert.ThrowsAsync<InvalidOperationException>(
() => ExceptionHandlerExtensions.HandleAsync(handler, context, cancellationToken));
Assert.Equal("IHttpActionResult.ExecuteAsync must not return null.", exception.Message);
}
private static CancellationTokenSource CreateCancellationTokenSource()
{
return new CancellationTokenSource();
}
private static ExceptionContext CreateMinimalValidContext()
{
return new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer);
}
private static IExceptionHandler CreateDummyHandler()
{
return new Mock<IExceptionHandler>(MockBehavior.Strict).Object;
}
private static HttpResponseMessage CreateResponse()
{
return new HttpResponseMessage();
}
private static IExceptionHandler CreateResultHandler(IHttpActionResult result)
{
Mock<IExceptionHandler> mock = new Mock<IExceptionHandler>(MockBehavior.Strict);
mock
.Setup(h => h.HandleAsync(It.IsAny<ExceptionHandlerContext>(), It.IsAny<CancellationToken>()))
.Returns<ExceptionHandlerContext, CancellationToken>((c, i) =>
{
c.Result = result;
return Task.FromResult(0);
});
return mock.Object;
}
private static IExceptionHandler CreateStubHandler()
{
return CreateStubHandlerMock().Object;
}
private static Mock<IExceptionHandler> CreateStubHandlerMock()
{
Mock<IExceptionHandler> mock = new Mock<IExceptionHandler>(MockBehavior.Strict);
mock
.Setup(h => h.HandleAsync(It.IsAny<ExceptionHandlerContext>(), It.IsAny<CancellationToken>()))
.Returns(Task.FromResult(0));
return mock;
}
}
}