-
Notifications
You must be signed in to change notification settings - Fork 352
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
Fix for #147 #185
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
a890df4
general idea of fix for https://github.com/aspnet/AspNetWebStack/issu…
1b5e6a4
build fix
83d5629
passing rethrow flag through ctor, not throught CorsPolicy
cc39a81
StyleCop fixes
08a202b
review fixes; added tests
e0257cd
review fixes
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
11 changes: 11 additions & 0 deletions
11
test/System.Web.Http.Cors.Test/Controllers/ThrowingController.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,6 +7,7 @@ | |
using System.Threading; | ||
using System.Threading.Tasks; | ||
using System.Web.Cors; | ||
using System.Web.Http.ExceptionHandling; | ||
using System.Web.Http.Hosting; | ||
using Microsoft.TestCommon; | ||
|
||
|
@@ -180,6 +181,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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
{ | ||
|
@@ -238,5 +273,13 @@ public Task HandleCorsPreflightRequestAsync_NullContext_Throws() | |
() => corsHandler.HandleCorsPreflightRequestAsync(new HttpRequestMessage(), null, CancellationToken.None), | ||
"corsRequestContext"); | ||
} | ||
|
||
private class PassthroughExceptionHandler : IExceptionHandler | ||
{ | ||
public Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken) | ||
{ | ||
throw context.Exception; | ||
} | ||
} | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI we've got a meeting scheduled next Tuesday to decide whether default should be
true
i.e. whether we should break compatibility to ensureIExceptionHandler
sees what it should. Will let you know…There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Decision was to avoid breaking changes and stick with the approach you've implemented here.