| 
7 | 7 | using System.Threading;  | 
8 | 8 | using System.Threading.Tasks;  | 
9 | 9 | using System.Web.Cors;  | 
 | 10 | +using System.Web.Http.ExceptionHandling;  | 
10 | 11 | using System.Web.Http.Hosting;  | 
11 | 12 | using Microsoft.TestCommon;  | 
12 | 13 | 
 
  | 
13 | 14 | namespace System.Web.Http.Cors  | 
14 | 15 | {  | 
15 | 16 |     public class CorsMessageHandlerTest  | 
16 | 17 |     {  | 
 | 18 | +        private class PassthroughExceptionHandler : IExceptionHandler  | 
 | 19 | +        {  | 
 | 20 | +            public Task HandleAsync(ExceptionHandlerContext context, CancellationToken cancellationToken)  | 
 | 21 | +            {  | 
 | 22 | +                throw context.Exception;  | 
 | 23 | +            }  | 
 | 24 | +        }  | 
 | 25 | + | 
17 | 26 |         [Fact]  | 
18 | 27 |         public void Constructor_NullConfig_Throws()  | 
19 | 28 |         {  | 
@@ -180,6 +189,40 @@ public async Task SendAsync_HandlesExceptions_ThrownDuringPreflight()  | 
180 | 189 |             Assert.Equal(HttpStatusCode.MethodNotAllowed, response.StatusCode);  | 
181 | 190 |         }  | 
182 | 191 | 
 
  | 
 | 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 | + | 
183 | 226 |         [Fact]  | 
184 | 227 |         public Task HandleCorsRequestAsync_NullConfig_Throws()  | 
185 | 228 |         {  | 
 | 
0 commit comments