Skip to content

Commit 08a202b

Browse files
author
Trofimov Ivan Andreevich
committed
review fixes; added tests
1 parent cc39a81 commit 08a202b

File tree

4 files changed

+57
-0
lines changed

4 files changed

+57
-0
lines changed

src/System.Web.Http.Cors/CorsMessageHandler.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ protected async override Task<HttpResponseMessage> SendAsync(HttpRequestMessage
7373
catch (Exception exception)
7474
{
7575
if (_rethrowExceptions)
76+
{
7677
throw;
78+
}
7779

7880
return HandleException(request, exception);
7981
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
namespace System.Web.Http.Cors
2+
{
3+
[EnableCors("*", "*", "*")]
4+
public class ThrowingController : ApiController
5+
{
6+
public string Get()
7+
{
8+
throw new Exception();
9+
}
10+
}
11+
}

test/System.Web.Http.Cors.Test/CorsMessageHandlerTest.cs

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,22 @@
77
using System.Threading;
88
using System.Threading.Tasks;
99
using System.Web.Cors;
10+
using System.Web.Http.ExceptionHandling;
1011
using System.Web.Http.Hosting;
1112
using Microsoft.TestCommon;
1213

1314
namespace System.Web.Http.Cors
1415
{
1516
public class CorsMessageHandlerTest
1617
{
18+
private class PassthroughExceptionHandler : IExceptionHandler
19+
{
20+
public Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)
21+
{
22+
throw context.Exception;
23+
}
24+
}
25+
1726
[Fact]
1827
public void Constructor_NullConfig_Throws()
1928
{
@@ -180,6 +189,40 @@ public async Task SendAsync_HandlesExceptions_ThrownDuringPreflight()
180189
Assert.Equal(HttpStatusCode.MethodNotAllowed, response.StatusCode);
181190
}
182191

192+
[Fact]
193+
public async Task SendAsync_Preflight_RethrowsExceptions_WhenRethrowFlagIsTrue()
194+
{
195+
HttpConfiguration config = new HttpConfiguration();
196+
config.Routes.MapHttpRoute("default", "{controller}");
197+
HttpServer server = new HttpServer(config);
198+
CorsMessageHandler corsHandler = new CorsMessageHandler(config, true);
199+
corsHandler.InnerHandler = server;
200+
HttpMessageInvoker invoker = new HttpMessageInvoker(corsHandler);
201+
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Options, "http://localhost/sample");
202+
request.SetConfiguration(config);
203+
request.Headers.Add(CorsConstants.Origin, "http://localhost");
204+
request.Headers.Add(CorsConstants.AccessControlRequestMethod, "RandomMethod");
205+
206+
await Assert.ThrowsAsync<HttpResponseException>(() => invoker.SendAsync(request, CancellationToken.None));
207+
}
208+
209+
[Fact]
210+
public async Task SendAsync_RethrowsExceptions_WhenRethrowFlagIsTrue()
211+
{
212+
HttpConfiguration config = new HttpConfiguration();
213+
config.Routes.MapHttpRoute("default", "{controller}");
214+
config.Services.Replace(typeof(IExceptionHandler), new PassthroughExceptionHandler());
215+
HttpServer server = new HttpServer(config);
216+
CorsMessageHandler corsHandler = new CorsMessageHandler(config, true);
217+
corsHandler.InnerHandler = server;
218+
HttpMessageInvoker invoker = new HttpMessageInvoker(corsHandler);
219+
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "http://localhost/throwing");
220+
request.SetConfiguration(config);
221+
request.Headers.Add(CorsConstants.Origin, "http://localhost");
222+
223+
await Assert.ThrowsAsync<Exception>(() => invoker.SendAsync(request, CancellationToken.None));
224+
}
225+
183226
[Fact]
184227
public Task HandleCorsRequestAsync_NullConfig_Throws()
185228
{

test/System.Web.Http.Cors.Test/System.Web.Http.Cors.Test.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
<Compile Include="Controllers\PerControllerConfigController.cs" />
8383
<Compile Include="Controllers\SampleController.cs" />
8484
<Compile Include="Controllers\DefaultController.cs" />
85+
<Compile Include="Controllers\ThrowingController.cs" />
8586
<Compile Include="CorsMessageHandlerTest.cs" />
8687
<Compile Include="DisableCorsAttributeTest.cs" />
8788
<Compile Include="EnableCorsAttributeTest.cs" />

0 commit comments

Comments
 (0)