-
Notifications
You must be signed in to change notification settings - Fork 353
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
Fix for #147 #185
Changes from 1 commit
a890df4
1b5e6a4
83d5629
cc39a81
08a202b
e0257cd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
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. I feels like it's not the best place for this class, however i have no idea where it should be placed( 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. Bottom of this class is fine. 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. Done |
||
{ | ||
public Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken) | ||
{ | ||
throw context.Exception; | ||
} | ||
} | ||
|
||
[Fact] | ||
public void Constructor_NullConfig_Throws() | ||
{ | ||
|
@@ -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()); | ||
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() | ||
{ | ||
|
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.
style: include the braces please.