Skip to content

Fix for #147 #185

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Aug 21, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/System.Web.Http.Cors/CorsMessageHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage
catch (Exception exception)
{
if (_rethrowExceptions)
{
throw;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style: include the braces please.

}

return HandleException(request, exception);
}
Expand Down
11 changes: 11 additions & 0 deletions test/System.Web.Http.Cors.Test/Controllers/ThrowingController.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
namespace System.Web.Http.Cors
{
[EnableCors("*", "*", "*")]
public class ThrowingController : ApiController
{
public string Get()
{
throw new Exception();
}
}
}
43 changes: 43 additions & 0 deletions test/System.Web.Http.Cors.Test/CorsMessageHandlerTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,22 @@
using System.Threading;
using System.Threading.Tasks;
using System.Web.Cors;
using System.Web.Http.ExceptionHandling;
using System.Web.Http.Hosting;
using Microsoft.TestCommon;

namespace System.Web.Http.Cors
{
public class CorsMessageHandlerTest
{
private class PassthroughExceptionHandler : IExceptionHandler
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feels like it's not the best place for this class, however i have no idea where it should be placed(
Any suggestions would be appreciated

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bottom of this class is fine.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

{
public Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
{
throw context.Exception;
}
}

[Fact]
public void Constructor_NullConfig_Throws()
{
Expand Down Expand Up @@ -180,6 +189,40 @@ public async Task SendAsync_HandlesExceptions_ThrownDuringPreflight()
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());
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is needed to reproduce the exact scenario of original issue, otherwise default implementation of IExceptionHandler maps Exceptions to just 500 status code

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()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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" />
Expand Down