/
ExceptionHandlerTests.cs
228 lines (195 loc) · 8.2 KB
/
ExceptionHandlerTests.cs
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using System;
using System.Linq.Expressions;
using System.Net;
using System.Net.Http;
using System.Security;
using System.Threading.Tasks;
using CloudNimble.EasyAF.Http.OData;
using FluentAssertions;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.OData;
using Microsoft.Restier.Breakdance;
using Microsoft.Restier.Core;
using Microsoft.Restier.Core.Query;
using Microsoft.Restier.Tests.Shared;
using Microsoft.VisualStudio.TestTools.UnitTesting;
#if NET6_0_OR_GREATER
namespace Microsoft.Restier.Tests.AspNetCore
#else
namespace Microsoft.Restier.Tests.AspNet
#endif
{
#if NET6_0_OR_GREATER
[TestClass]
[TestCategory("Endpoint Routing")]
public class ExceptionHandlerTests_EndpointRouting : ExceptionHandlerTests
{
public ExceptionHandlerTests_EndpointRouting() : base(true)
{
}
}
[TestClass]
[TestCategory("Legacy Routing")]
public class ExceptionHandlerTests_LegacyRouting : ExceptionHandlerTests
{
public ExceptionHandlerTests_LegacyRouting() : base(false)
{
}
}
/// <summary>
///
/// </summary>
[TestClass]
public abstract class ExceptionHandlerTests : RestierTestBase<StoreApi>
{
public ExceptionHandlerTests(bool useEndpointRouting) : base(useEndpointRouting)
{
//AddRestierAction = builder =>
//{
// builder.AddRestierApi<StoreApi>(services => services.AddEntityFrameworkServices<StoreContext>());
//};
//MapRestierAction = routeBuilder =>
//{
// routeBuilder.MapApiRoute<StoreApi>(WebApiConstants.RouteName, WebApiConstants.RoutePrefix, false);
//};
}
//[TestInitialize]
//public void ClaimsTestSetup() => TestSetup();
#else
/// <summary>
///
/// </summary>
[TestClass]
public class ExceptionHandlerTests : RestierTestBase
{
#endif
private const string conflictMessage = "Record could not be saved.";
private const string innerExceptionMessage = "More details about what happened.";
private const string securityError = "Security error.";
private const string somethingHappened = "Something happened.";
[TestMethod]
public async Task ODataException_Returns403()
{
static void di(IServiceCollection services)
{
services
.AddTestStoreApiServices()
.AddChainedService<IQueryExpressionSourcer>((sp, next) => new ODataExceptionSourcer());
}
var response = await RestierTestHelpers.ExecuteTestRequest<StoreApi>(HttpMethod.Get, resource: "/Products", serviceCollection: di, useEndpointRouting: UseEndpointRouting);
var content = await TestContext.LogAndReturnMessageContentAsync(response);
response.IsSuccessStatusCode.Should().BeFalse();
response.StatusCode.Should().Be(HttpStatusCode.BadRequest);
var result = await response.DeserializeResponseAsync<Product, ODataV4ErrorResponse>();
result.Should().NotBeNull();
result.Response.Should().BeNull();
result.ErrorContent.Should().NotBeNull();
result.ErrorContent.Error.Message.Should().Be(somethingHappened);
}
[TestMethod]
public async Task ShouldReturn403HandlerThrowsSecurityException()
{
static void di(IServiceCollection services)
{
services
.AddTestStoreApiServices()
.AddChainedService<IQueryExpressionSourcer>((sp, next) => new SecurityExceptionSourcer());
}
var response = await RestierTestHelpers.ExecuteTestRequest<StoreApi>(HttpMethod.Get, resource: "/Products", serviceCollection: di, useEndpointRouting: UseEndpointRouting);
response.IsSuccessStatusCode.Should().BeFalse();
response.StatusCode.Should().Be(HttpStatusCode.Forbidden);
var result = await response.DeserializeResponseAsync<Product, ODataV4ErrorResponse>();
result.Should().NotBeNull();
result.Response.Should().BeNull();
result.ErrorContent.Should().NotBeNull();
result.ErrorContent.Error.Message.Should().Be(securityError);
}
[TestMethod]
public async Task NullReferenceException_ReturnsProperPayload()
{
static void di(IServiceCollection services)
{
services
.AddTestStoreApiServices()
.AddChainedService<IQueryExpressionSourcer>((sp, next) => new NullReferenceExceptionSourcer());
}
var response = await RestierTestHelpers.ExecuteTestRequest<StoreApi>(HttpMethod.Get, resource: "/Products", serviceCollection: di, useEndpointRouting: UseEndpointRouting);
response.IsSuccessStatusCode.Should().BeFalse();
response.StatusCode.Should().Be(HttpStatusCode.InternalServerError);
var result = await response.DeserializeResponseAsync<Product, ODataV4ErrorResponse>();
result.Should().NotBeNull();
result.Response.Should().BeNull();
result.ErrorContent.Should().NotBeNull();
result.ErrorContent.Error.Message.Should().Contain("magic word");
}
#region Test Resources
/// <summary>
/// Throws an <see cref="ODataException"/> without an InnerException.
/// </summary>
private class ODataExceptionSourcer : IQueryExpressionSourcer
{
public Expression ReplaceQueryableSource(QueryExpressionContext context, bool embedded)
{
throw new ODataException(somethingHappened);
}
}
/// <summary>
/// Throws an <see cref="ODataException"/> with an InnerException.
/// </summary>
private class ODataInnerExceptionSourcer : IQueryExpressionSourcer
{
public Expression ReplaceQueryableSource(QueryExpressionContext context, bool embedded)
{
throw new ODataException(somethingHappened, new Exception(innerExceptionMessage));
}
}
/// <summary>
/// Throws a <see cref="SecurityException"/> without any parameters.
/// </summary>
private class NullReferenceExceptionSourcer : IQueryExpressionSourcer
{
public Expression ReplaceQueryableSource(QueryExpressionContext context, bool embedded)
{
throw new NullReferenceException("Ah ah ah, you didn't say the magic word!");
}
}
/// <summary>
/// Throws a <see cref="SecurityException"/> without any parameters.
/// </summary>
private class SecurityExceptionSourcer : IQueryExpressionSourcer
{
public Expression ReplaceQueryableSource(QueryExpressionContext context, bool embedded)
{
throw new SecurityException();
}
}
/// <summary>
/// Throws a <see cref="SecurityException"/> without any parameters.
/// </summary>
private class SecurityExceptionMessageSourcer : IQueryExpressionSourcer
{
public Expression ReplaceQueryableSource(QueryExpressionContext context, bool embedded)
{
throw new SecurityException(somethingHappened);
}
}
private class StatusCodeExceptionSourcer : IQueryExpressionSourcer
{
public Expression ReplaceQueryableSource(QueryExpressionContext context, bool embedded)
{
throw new StatusCodeException(HttpStatusCode.Conflict, conflictMessage);
}
}
private class StatusCodeInnerExceptionSourcer : IQueryExpressionSourcer
{
public Expression ReplaceQueryableSource(QueryExpressionContext context, bool embedded)
{
throw new StatusCodeException(HttpStatusCode.Conflict, conflictMessage,
new Exception(innerExceptionMessage));
}
}
#endregion
}
}