summaryrefslogtreecommitdiff
path: root/external/aspnetwebstack/test/System.Web.Http.Integration.Test/ExceptionHandling/IncludeErrorDetailTest.cs
blob: 564bdff6ed116e84a40f1a0939a38b1672ab360f (plain)
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
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.

using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using Newtonsoft.Json.Linq;
using Xunit;
using Xunit.Extensions;

namespace System.Web.Http
{
    public class IncludeErrorDetailTest
    {
        public static IEnumerable<object[]> Data
        {
            get
            {
                return new object[][]
                {
                    new object[] { "localhost", null, true },
                    new object[] { "127.0.0.1", null, true },
                    new object[] { "www.foo.com", null, false },
                    new object[] { "localhost", IncludeErrorDetailPolicy.LocalOnly, true },
                    new object[] { "www.foo.com", IncludeErrorDetailPolicy.LocalOnly, false },
                    new object[] { "localhost", IncludeErrorDetailPolicy.Always, true },
                    new object[] { "www.foo.com", IncludeErrorDetailPolicy.Always, true },
                    new object[] { "localhost", IncludeErrorDetailPolicy.Never, false },
                    new object[] { "www.foo.com", IncludeErrorDetailPolicy.Never, false }
                };
            }
        }

        [Theory]
        [PropertyData("Data")]
        public void ThrowingOnActionIncludesErrorDetail(string hostName, IncludeErrorDetailPolicy? includeErrorDetail, bool shouldIncludeErrorDetail)
        {
            string controllerName = "Exception";
            string requestUrl = String.Format("{0}/{1}/{2}", "http://" + hostName, controllerName, "ArgumentNull");
            ScenarioHelper.RunTest(
                controllerName,
                "/{action}",
                new HttpRequestMessage(HttpMethod.Post, requestUrl),
                (response) =>
                {
                    if (shouldIncludeErrorDetail)
                    {
                        AssertResponseIncludesErrorDetail(response);
                    }
                    else
                    {
                        AssertResponseDoesNotIncludeErrorDetail(response);
                    }
                },
                (config) =>
                {
                    if (includeErrorDetail.HasValue)
                    {
                        config.IncludeErrorDetailPolicy = includeErrorDetail.Value;
                    }
                }
            );
        }

        private void AssertResponseIncludesErrorDetail(HttpResponseMessage response)
        {
            Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
            dynamic json = JToken.Parse(response.Content.ReadAsStringAsync().Result);
            string result = json.ExceptionType;
            Assert.Equal(typeof(ArgumentNullException).FullName, result);
        }

        private void AssertResponseDoesNotIncludeErrorDetail(HttpResponseMessage response)
        {
            Assert.Equal(HttpStatusCode.InternalServerError, response.StatusCode);
            Assert.Null(response.Content);
        }
    }
}