forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDefaultExceptionHandlerTests.cs
More file actions
140 lines (117 loc) · 5.46 KB
/
Copy pathDefaultExceptionHandlerTests.cs
File metadata and controls
140 lines (117 loc) · 5.46 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
// 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;
using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Controllers;
using System.Web.Http.Results;
using Microsoft.TestCommon;
namespace System.Web.Http.ExceptionHandling
{
public class DefaultExceptionHandlerTests
{
[Fact]
public void HandleAsync_IfContextIsNull_Throws()
{
// Arrange
IExceptionHandler product = CreateProductUnderTest();
ExceptionHandlerContext context = null;
CancellationToken cancellationToken = CancellationToken.None;
// Act & Assert
Assert.ThrowsArgumentNull(() => product.HandleAsync(context, cancellationToken), "context");
}
[Fact]
public void HandleAsync_IfRequestIsNull_Throws()
{
// Arrange
IExceptionHandler product = CreateProductUnderTest();
ExceptionHandlerContext context = new ExceptionHandlerContext(new ExceptionContext(CreateException(), ExceptionCatchBlocks.HttpServer));
Assert.Null(context.ExceptionContext.Request); // Guard
CancellationToken cancellationToken = CancellationToken.None;
// Act & Assert
Assert.ThrowsArgument(() => product.HandleAsync(context, cancellationToken), "context",
"ExceptionContext.Request must not be null.");
}
[Fact]
public async Task HandleAsync_HandlesExceptionViaCreateErrorResponse()
{
IExceptionHandler product = CreateProductUnderTest();
// Arrange
using (HttpRequestMessage expectedRequest = CreateRequest())
{
expectedRequest.SetRequestContext(new HttpRequestContext { IncludeErrorDetail = true });
ExceptionHandlerContext context = CreateValidContext(expectedRequest);
CancellationToken cancellationToken = CancellationToken.None;
// Act
await product.HandleAsync(context, cancellationToken);
// Assert
IHttpActionResult result = context.Result;
ResponseMessageResult typedResult = Assert.IsType<ResponseMessageResult>(result);
using (HttpResponseMessage response = typedResult.Response)
{
Assert.NotNull(response);
using (HttpResponseMessage expectedResponse = expectedRequest.CreateErrorResponse(
HttpStatusCode.InternalServerError, context.ExceptionContext.Exception))
{
AssertErrorResponse(expectedResponse, response);
}
Assert.Same(expectedRequest, response.RequestMessage);
}
}
}
[Fact]
public async Task HandleAsync_IfCatchBlockIsIExceptionFilter_LeavesExceptionUnhandled()
{
IExceptionHandler product = CreateProductUnderTest();
// Arrange
using (HttpRequestMessage request = CreateRequest())
{
ExceptionHandlerContext context = CreateValidContext(request, ExceptionCatchBlocks.IExceptionFilter);
CancellationToken cancellationToken = CancellationToken.None;
// Act
await product.HandleAsync(context, cancellationToken);
// Assert
Assert.Null(context.Result);
}
}
private static void AssertErrorResponse(HttpResponseMessage expected, HttpResponseMessage actual)
{
Assert.NotNull(expected); // Guard
ObjectContent<HttpError> expectedContent = Assert.IsType<ObjectContent<HttpError>>(expected.Content); // Guard
Assert.NotNull(expectedContent.Formatter); // Guard
Assert.NotNull(actual);
Assert.Equal(expected.StatusCode, actual.StatusCode);
ObjectContent<HttpError> actualContent = Assert.IsType<ObjectContent<HttpError>>(actual.Content);
Assert.NotNull(actualContent.Formatter);
Assert.Same(expectedContent.Formatter.GetType(), actualContent.Formatter.GetType());
Assert.Equal(expectedContent.Value, actualContent.Value);
Assert.Same(expected.RequestMessage, actual.RequestMessage);
}
private static ExceptionHandlerContext CreateContext(ExceptionContext exceptionContext)
{
return new ExceptionHandlerContext(exceptionContext);
}
private static Exception CreateException()
{
return new NotSupportedException();
}
private static DefaultExceptionHandler CreateProductUnderTest()
{
return new DefaultExceptionHandler();
}
private static HttpRequestMessage CreateRequest()
{
return new HttpRequestMessage();
}
private static ExceptionHandlerContext CreateValidContext(HttpRequestMessage request)
{
return CreateContext(new ExceptionContext(CreateException(), ExceptionCatchBlocks.HttpServer, request));
}
private static ExceptionHandlerContext CreateValidContext(HttpRequestMessage request,
ExceptionContextCatchBlock catchBlock)
{
return CreateContext(new ExceptionContext(CreateException(), catchBlock, request));
}
}
}