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
|
// Copyright (c) Microsoft Corporation. All rights reserved. See License.txt in the project root for license information.
using System.Collections.Generic;
using System.IO;
using System.Net;
using System.Net.Http;
using System.Net.Http.Formatting;
using System.Net.Http.Headers;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Http.Controllers;
using System.Web.Http.Filters;
using Xunit;
using Xunit.Extensions;
namespace System.Web.Http.ExceptionHandling
{
public class HttpResponseExceptionTest
{
[Theory]
[InlineData("DoNotThrow")]
[InlineData("ActionMethod")]
// TODO : 332683 - HttpResponseExceptions in message handlers
//[InlineData("RequestMessageHandler")]
//[InlineData("ResponseMessageHandler")]
[InlineData("RequestAuthorization")]
[InlineData("BeforeActionExecuted")]
[InlineData("AfterActionExecuted")]
[InlineData("ContentNegotiatorNegotiate")]
[InlineData("ActionMethodAndExceptionFilter")]
[InlineData("MediaTypeFormatterReadFromStreamAsync")]
public void HttpResponseExceptionWithExplicitStatusCode(string throwAt)
{
HttpRequestMessage request = new HttpRequestMessage();
request.RequestUri = new Uri(ScenarioHelper.BaseAddress + "/ExceptionTests/ReturnString");
request.Method = HttpMethod.Post;
request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
request.Content = new StringContent("\"" + throwAt + "\"", Encoding.UTF8, "application/json");
ScenarioHelper.RunTest(
"ExceptionTests",
"/{action}",
request,
response =>
{
Assert.NotNull(response.Content);
Assert.NotNull(response.Content.Headers.ContentType);
Assert.Equal(response.Content.Headers.ContentType.MediaType, "application/json");
if (throwAt == "DoNotThrow")
{
Assert.Equal(HttpStatusCode.OK, response.StatusCode);
Assert.Equal("Hello World!", response.Content.ReadAsAsync<string>(new List<MediaTypeFormatter>() { new JsonMediaTypeFormatter() }).Result);
}
else
{
Assert.Equal(HttpStatusCode.NotFound, response.StatusCode);
Assert.Equal(String.Format("Error at {0}", throwAt),
response.Content.ReadAsAsync<string>(new List<MediaTypeFormatter>() { new JsonMediaTypeFormatter() }).Result);
}
},
config =>
{
config.Services.Replace(typeof(IContentNegotiator), new CustomContentNegotiator(throwAt));
config.MessageHandlers.Add(new CustomMessageHandler(throwAt));
config.Filters.Add(new CustomActionFilterAttribute(throwAt));
config.Filters.Add(new CustomAuthorizationFilterAttribute(throwAt));
config.Filters.Add(new CustomExceptionFilterAttribute(throwAt));
config.Formatters.Clear();
config.Formatters.Add(new CustomJsonMediaTypeFormatter(throwAt));
}
);
}
}
public class CustomMessageHandler : DelegatingHandler
{
private string _throwAt;
public CustomMessageHandler(string throwAt)
{
_throwAt = throwAt;
}
protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
{
ExceptionTestsUtility.CheckForThrow(_throwAt, "RequestMessageHandler");
return base.SendAsync(request, cancellationToken).ContinueWith<HttpResponseMessage>((tsk) =>
{
ExceptionTestsUtility.CheckForThrow(_throwAt, "ResponseMessageHandler");
HttpResponseMessage response = tsk.Result;
return response;
});
}
}
public class ExceptionTestsController : ApiController
{
[HttpPost]
public string ReturnString([FromBody] string throwAt)
{
string message = "Hello World!";
// check if the test wants to throw from here
ExceptionTestsUtility.CheckForThrow(throwAt, "ActionMethod");
// NOTE: this indicates that we want to throw from here & after this gets intercepted
// by the ExceptionFilter, we want to throw from there too
ExceptionTestsUtility.CheckForThrow(throwAt, "ActionMethodAndExceptionFilter");
return message;
}
}
public class CustomAuthorizationFilterAttribute : AuthorizationFilterAttribute
{
private string _throwAt;
public CustomAuthorizationFilterAttribute(string throwAt)
{
_throwAt = throwAt;
}
public override void OnAuthorization(HttpActionContext context)
{
ExceptionTestsUtility.CheckForThrow(_throwAt, "RequestAuthorization");
}
}
public class CustomActionFilterAttribute : ActionFilterAttribute
{
private string _throwAt;
public CustomActionFilterAttribute(string throwAt)
{
_throwAt = throwAt;
}
public override void OnActionExecuting(HttpActionContext context)
{
ExceptionTestsUtility.CheckForThrow(_throwAt, "BeforeActionExecuted");
}
public override void OnActionExecuted(HttpActionExecutedContext context)
{
ExceptionTestsUtility.CheckForThrow(_throwAt, "AfterActionExecuted");
}
}
public class CustomExceptionFilterAttribute : ExceptionFilterAttribute
{
private string _throwAt;
public CustomExceptionFilterAttribute(string throwAt)
{
_throwAt = throwAt;
}
public override void OnException(HttpActionExecutedContext context)
{
ExceptionTestsUtility.CheckForThrow(_throwAt, "ActionMethodAndExceptionFilter");
}
}
public class CustomContentNegotiator : System.Net.Http.Formatting.DefaultContentNegotiator
{
private string _throwAt;
public CustomContentNegotiator(string throwAt)
{
_throwAt = throwAt;
}
public override ContentNegotiationResult Negotiate(Type type, HttpRequestMessage request, IEnumerable<MediaTypeFormatter> formatters)
{
ExceptionTestsUtility.CheckForThrow(_throwAt, "ContentNegotiatorNegotiate");
return base.Negotiate(type, request, formatters);
}
}
public class CustomJsonMediaTypeFormatter : JsonMediaTypeFormatter
{
private string _throwAt;
public CustomJsonMediaTypeFormatter(string throwAt)
{
_throwAt = throwAt;
}
public override Task<object> ReadFromStreamAsync(Type type, Stream stream, HttpContentHeaders contentHeaders, IFormatterLogger formatterLogger)
{
ExceptionTestsUtility.CheckForThrow(_throwAt, "MediaTypeFormatterReadFromStreamAsync");
return base.ReadFromStreamAsync(type, stream, contentHeaders, formatterLogger);
}
public override Task WriteToStreamAsync(Type type, object value, Stream stream, HttpContentHeaders contentHeaders, TransportContext transportContext)
{
ExceptionTestsUtility.CheckForThrow(_throwAt, "MediaTypeFormatterWriteToStreamAsync");
return base.WriteToStreamAsync(type, value, stream, contentHeaders, transportContext);
}
}
public static class ExceptionTestsUtility
{
public static void CheckForThrow(string throwAt, string stage)
{
if (throwAt == stage)
{
HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new ObjectContent<string>(String.Format("Error at {0}", stage), new JsonMediaTypeFormatter())
};
throw new HttpResponseException(response);
}
}
}
}
|