This commit is contained in:
Trofimov Ivan Andreevich 2018-08-16 22:37:47 +03:00
Родитель cc39a81d01
Коммит 08a202b95f
4 изменённых файлов: 57 добавлений и 0 удалений

Просмотреть файл

@ -73,7 +73,9 @@ namespace System.Web.Http.Cors
catch (Exception exception)
{
if (_rethrowExceptions)
{
throw;
}
return HandleException(request, exception);
}

Просмотреть файл

@ -0,0 +1,11 @@
namespace System.Web.Http.Cors
{
[EnableCors("*", "*", "*")]
public class ThrowingController : ApiController
{
public string Get()
{
throw new Exception();
}
}
}

Просмотреть файл

@ -7,6 +7,7 @@ using System.Net.Http;
using System.Threading;
using System.Threading.Tasks;
using System.Web.Cors;
using System.Web.Http.ExceptionHandling;
using System.Web.Http.Hosting;
using Microsoft.TestCommon;
@ -14,6 +15,14 @@ namespace System.Web.Http.Cors
{
public class CorsMessageHandlerTest
{
private class PassthroughExceptionHandler : IExceptionHandler
{
public Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
{
throw context.Exception;
}
}
[Fact]
public void Constructor_NullConfig_Throws()
{
@ -180,6 +189,40 @@ namespace System.Web.Http.Cors
Assert.Equal(HttpStatusCode.MethodNotAllowed, response.StatusCode);
}
[Fact]
public async Task SendAsync_Preflight_RethrowsExceptions_WhenRethrowFlagIsTrue()
{
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute("default", "{controller}");
HttpServer server = new HttpServer(config);
CorsMessageHandler corsHandler = new CorsMessageHandler(config, true);
corsHandler.InnerHandler = server;
HttpMessageInvoker invoker = new HttpMessageInvoker(corsHandler);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Options, "http://localhost/sample");
request.SetConfiguration(config);
request.Headers.Add(CorsConstants.Origin, "http://localhost");
request.Headers.Add(CorsConstants.AccessControlRequestMethod, "RandomMethod");
await Assert.ThrowsAsync<HttpResponseException>(() => invoker.SendAsync(request, CancellationToken.None));
}
[Fact]
public async Task SendAsync_RethrowsExceptions_WhenRethrowFlagIsTrue()
{
HttpConfiguration config = new HttpConfiguration();
config.Routes.MapHttpRoute("default", "{controller}");
config.Services.Replace(typeof(IExceptionHandler), new PassthroughExceptionHandler());
HttpServer server = new HttpServer(config);
CorsMessageHandler corsHandler = new CorsMessageHandler(config, true);
corsHandler.InnerHandler = server;
HttpMessageInvoker invoker = new HttpMessageInvoker(corsHandler);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/throwing");
request.SetConfiguration(config);
request.Headers.Add(CorsConstants.Origin, "http://localhost");
await Assert.ThrowsAsync<Exception>(() => invoker.SendAsync(request, CancellationToken.None));
}
[Fact]
public Task HandleCorsRequestAsync_NullConfig_Throws()
{

Просмотреть файл

@ -82,6 +82,7 @@
<Compile Include="Controllers\PerControllerConfigController.cs" />
<Compile Include="Controllers\SampleController.cs" />
<Compile Include="Controllers\DefaultController.cs" />
<Compile Include="Controllers\ThrowingController.cs" />
<Compile Include="CorsMessageHandlerTest.cs" />
<Compile Include="DisableCorsAttributeTest.cs" />
<Compile Include="EnableCorsAttributeTest.cs" />