forked from aspnet/AspNetWebStack
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathIncludeErrorDetailTest.cs
More file actions
105 lines (96 loc) · 4.86 KB
/
Copy pathIncludeErrorDetailTest.cs
File metadata and controls
105 lines (96 loc) · 4.86 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
// 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.Tasks;
using System.Web.Http.Hosting;
using Microsoft.TestCommon;
using Newtonsoft.Json.Linq;
namespace System.Web.Http
{
public class IncludeErrorDetailTest
{
public static TheoryDataSet ThrowingOnActionIncludesErrorDetailData
{
get
{
return new TheoryDataSet<bool, IncludeErrorDetailPolicy, bool?, bool>()
{
// isLocal, includeErrorDetail, customErrors, expectErrorDetail
{ true, IncludeErrorDetailPolicy.LocalOnly, true, true },
{ false, IncludeErrorDetailPolicy.LocalOnly, true, false },
{ true, IncludeErrorDetailPolicy.LocalOnly, false, true },
{ false, IncludeErrorDetailPolicy.LocalOnly, false, false },
{ true, IncludeErrorDetailPolicy.LocalOnly, null, true },
{ false, IncludeErrorDetailPolicy.LocalOnly, null, false },
{ true, IncludeErrorDetailPolicy.Always, true, true },
{ false, IncludeErrorDetailPolicy.Always, true, true },
{ true, IncludeErrorDetailPolicy.Always, false, true },
{ false, IncludeErrorDetailPolicy.Always, false, true },
{ true, IncludeErrorDetailPolicy.Always, null, true },
{ false, IncludeErrorDetailPolicy.Always, null, true },
{ true, IncludeErrorDetailPolicy.Never, true, false },
{ false, IncludeErrorDetailPolicy.Never, true, false },
{ true, IncludeErrorDetailPolicy.Never, false, false },
{ false, IncludeErrorDetailPolicy.Never, false, false },
{ true, IncludeErrorDetailPolicy.Never, null, false },
{ false, IncludeErrorDetailPolicy.Never, null, false },
{ true, IncludeErrorDetailPolicy.Default, true, false },
{ false, IncludeErrorDetailPolicy.Default, true, false },
{ true, IncludeErrorDetailPolicy.Default, false, true },
{ false, IncludeErrorDetailPolicy.Default, false, true },
{ true, IncludeErrorDetailPolicy.Default, null, true },
{ false, IncludeErrorDetailPolicy.Default, null, false }
};
}
}
[Theory]
[PropertyData("ThrowingOnActionIncludesErrorDetailData")]
public async Task ThrowingOnActionIncludesErrorDetail(bool isLocal, IncludeErrorDetailPolicy includeErrorDetail, bool? customErrors, bool expectErrorDetail)
{
string controllerName = "Exception";
string requestUrl = String.Format("{0}/{1}/{2}", "http://www.foo.com", controllerName, "ArgumentNull");
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Post, requestUrl);
request.Properties[HttpPropertyKeys.IsLocalKey] = new Lazy<bool>(() => isLocal);
if (customErrors != null)
{
request.Properties[HttpPropertyKeys.IncludeErrorDetailKey] = new Lazy<bool>(() => !(bool)customErrors);
}
await ScenarioHelper.RunTestAsync(
controllerName,
"/{action}",
request,
async (response) =>
{
if (expectErrorDetail)
{
await AssertResponseIncludesErrorDetailAsync(response);
}
else
{
await AssertResponseDoesNotIncludeErrorDetailAsync(response);
}
},
(config) =>
{
config.IncludeErrorDetailPolicy = includeErrorDetail;
}
);
}
private async Task AssertResponseIncludesErrorDetailAsync(HttpResponseMessage response)
{
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
dynamic json = JToken.Parse(await response.Content.ReadAsStringAsync());
string result = json.ExceptionType;
Assert.Equal(typeof(ArgumentNullException).FullName, result);
}
private async Task AssertResponseDoesNotIncludeErrorDetailAsync(HttpResponseMessage response)
{
Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
JObject json = JToken.Parse(await response.Content.ReadAsStringAsync()) as JObject;
Assert.Single(json);
string errorMessage = ((JValue)json["Message"]).ToString();
Assert.Equal("An error has occurred.", errorMessage);
}
}
}