diff --git a/src/System.Web.Http.Owin/ExceptionHandling/DefaultExceptionHandler.cs b/src/System.Web.Http.Owin/ExceptionHandling/DefaultExceptionHandler.cs index aa98e34c..a9522249 100644 --- a/src/System.Web.Http.Owin/ExceptionHandling/DefaultExceptionHandler.cs +++ b/src/System.Web.Http.Owin/ExceptionHandling/DefaultExceptionHandler.cs @@ -31,13 +31,6 @@ namespace System.Web.Http.Owin.ExceptionHandling ExceptionContext exceptionContext = context.ExceptionContext; Contract.Assert(exceptionContext != null); - Exception exception = exceptionContext.Exception; - - if (exception == null) - { - throw new ArgumentException(Error.Format(OwinResources.TypePropertyMustNotBeNull, - typeof(ExceptionContext).Name, "Exception"), "context"); - } HttpRequestMessage request = exceptionContext.Request; @@ -48,7 +41,7 @@ namespace System.Web.Http.Owin.ExceptionHandling } context.Result = new ResponseMessageResult(request.CreateErrorResponse(HttpStatusCode.InternalServerError, - exception)); + exceptionContext.Exception)); } } } diff --git a/src/System.Web.Http/ExceptionHandling/DefaultExceptionHandler.cs b/src/System.Web.Http/ExceptionHandling/DefaultExceptionHandler.cs index f6feaf0f..c72c7a78 100644 --- a/src/System.Web.Http/ExceptionHandling/DefaultExceptionHandler.cs +++ b/src/System.Web.Http/ExceptionHandling/DefaultExceptionHandler.cs @@ -38,12 +38,6 @@ namespace System.Web.Http.ExceptionHandling Contract.Assert(exceptionContext != null); Exception exception = exceptionContext.Exception; - if (exception == null) - { - throw new ArgumentException(Error.Format(SRResources.TypePropertyMustNotBeNull, - typeof(ExceptionContext).Name, "Exception"), "context"); - } - HttpRequestMessage request = exceptionContext.Request; if (request == null) diff --git a/src/System.Web.Http/ExceptionHandling/ExceptionContext.cs b/src/System.Web.Http/ExceptionHandling/ExceptionContext.cs index 9a1db3fb..12dde2b0 100644 --- a/src/System.Web.Http/ExceptionHandling/ExceptionContext.cs +++ b/src/System.Web.Http/ExceptionHandling/ExceptionContext.cs @@ -11,19 +11,10 @@ namespace System.Web.Http.ExceptionHandling public class ExceptionContext { /// Initializes a new instance of the class. - /// This constructor is for unit testing purposes only. - public ExceptionContext() - { - } - - /// - /// Initializes a new instance of the class using the values provided. - /// /// The exception caught. /// The catch block where the exception was caught. - /// The action context in which the exception occurred. - public ExceptionContext(Exception exception, ExceptionContextCatchBlock catchBlock, - HttpActionContext actionContext) + /// This constructor is for unit testing purposes only. + public ExceptionContext(Exception exception, ExceptionContextCatchBlock catchBlock) { if (exception == null) { @@ -38,7 +29,18 @@ namespace System.Web.Http.ExceptionHandling } CatchBlock = catchBlock; + } + /// + /// Initializes a new instance of the class using the values provided. + /// + /// The exception caught. + /// The catch block where the exception was caught. + /// The action context in which the exception occurred. + public ExceptionContext(Exception exception, ExceptionContextCatchBlock catchBlock, + HttpActionContext actionContext) + : this(exception, catchBlock) + { if (actionContext == null) { throw new ArgumentNullException("actionContext"); @@ -78,21 +80,8 @@ namespace System.Web.Http.ExceptionHandling /// The catch block where the exception was caught. /// The request being processed when the exception was caught. public ExceptionContext(Exception exception, ExceptionContextCatchBlock catchBlock, HttpRequestMessage request) + : this(exception, catchBlock) { - if (exception == null) - { - throw new ArgumentNullException("exception"); - } - - Exception = exception; - - if (catchBlock == null) - { - throw new ArgumentNullException("catchBlock"); - } - - CatchBlock = catchBlock; - if (request == null) { throw new ArgumentNullException("request"); @@ -110,22 +99,8 @@ namespace System.Web.Http.ExceptionHandling /// The request being processed when the exception was caught. /// The repsonse being returned when the exception was caught. public ExceptionContext(Exception exception, ExceptionContextCatchBlock catchBlock, HttpRequestMessage request, - HttpResponseMessage response) + HttpResponseMessage response) : this(exception, catchBlock) { - if (exception == null) - { - throw new ArgumentNullException("exception"); - } - - Exception = exception; - - if (catchBlock == null) - { - throw new ArgumentNullException("catchBlock"); - } - - CatchBlock = catchBlock; - if (request == null) { throw new ArgumentNullException("request"); @@ -143,12 +118,10 @@ namespace System.Web.Http.ExceptionHandling } /// Gets the exception caught. - /// The setter is for unit testing purposes only. - public Exception Exception { get; set; } + public Exception Exception { get; private set; } /// Gets the catch block in which the exception was caught. - /// The setter is for unit testing purposes only. - public ExceptionContextCatchBlock CatchBlock { get; set; } + public ExceptionContextCatchBlock CatchBlock { get; private set; } /// Gets the request being processed when the exception was caught. /// The setter is for unit testing purposes only. diff --git a/src/System.Web.Http/ExceptionHandling/ExceptionHandler.cs b/src/System.Web.Http/ExceptionHandling/ExceptionHandler.cs index cf66cbb3..c758f22d 100644 --- a/src/System.Web.Http/ExceptionHandling/ExceptionHandler.cs +++ b/src/System.Web.Http/ExceptionHandling/ExceptionHandler.cs @@ -21,12 +21,6 @@ namespace System.Web.Http.ExceptionHandling ExceptionContext exceptionContext = context.ExceptionContext; Contract.Assert(exceptionContext != null); - if (exceptionContext.Exception == null) - { - throw new ArgumentException(Error.Format(SRResources.TypePropertyMustNotBeNull, - typeof(ExceptionContext).Name, "Exception"), "context"); - } - if (!ShouldHandle(context)) { return TaskHelpers.Completed(); @@ -69,12 +63,6 @@ namespace System.Web.Http.ExceptionHandling ExceptionContextCatchBlock catchBlock = exceptionContext.CatchBlock; - if (catchBlock == null) - { - throw new ArgumentException(Error.Format(SRResources.TypePropertyMustNotBeNull, - typeof(ExceptionContext), "CatchBlock"), "context"); - } - return catchBlock.IsTopLevel; } } diff --git a/src/System.Web.Http/ExceptionHandling/ExceptionLogger.cs b/src/System.Web.Http/ExceptionHandling/ExceptionLogger.cs index 57551e09..08688e1d 100644 --- a/src/System.Web.Http/ExceptionHandling/ExceptionLogger.cs +++ b/src/System.Web.Http/ExceptionHandling/ExceptionLogger.cs @@ -25,12 +25,6 @@ namespace System.Web.Http.ExceptionHandling ExceptionContext exceptionContext = context.ExceptionContext; Contract.Assert(exceptionContext != null); - if (exceptionContext.Exception == null) - { - throw new ArgumentException(Error.Format(SRResources.TypePropertyMustNotBeNull, - typeof(ExceptionContext).Name, "Exception"), "context"); - } - if (!ShouldLog(context)) { return TaskHelpers.Completed(); @@ -72,15 +66,8 @@ namespace System.Web.Http.ExceptionHandling ExceptionContext exceptionContext = context.ExceptionContext; Contract.Assert(exceptionContext != null); - Exception exception = exceptionContext.Exception; - if (exception == null) - { - throw new ArgumentException(Error.Format(SRResources.TypePropertyMustNotBeNull, - typeof(ExceptionContext).Name, "Exception"), "context"); - } - - IDictionary data = exception.Data; + IDictionary data = exceptionContext.Exception.Data; if (data == null || data.IsReadOnly) { diff --git a/src/System.Web.Http/ExceptionHandling/ExceptionLoggerContext.cs b/src/System.Web.Http/ExceptionHandling/ExceptionLoggerContext.cs index 8b03e662..e73815df 100644 --- a/src/System.Web.Http/ExceptionHandling/ExceptionLoggerContext.cs +++ b/src/System.Web.Http/ExceptionHandling/ExceptionLoggerContext.cs @@ -80,12 +80,6 @@ namespace System.Web.Http.ExceptionHandling Contract.Assert(_exceptionContext != null); ExceptionContextCatchBlock catchBlock = _exceptionContext.CatchBlock; - if (catchBlock == null) - { - throw new InvalidOperationException(Error.Format(SRResources.TypePropertyMustNotBeNull, - typeof(ExceptionContext).Name, "CatchBlock")); - } - return catchBlock.CallsHandler; } } diff --git a/test/System.Web.Http.Owin.Test/ExceptionHandling/DefaultExceptionHandlerTests.cs b/test/System.Web.Http.Owin.Test/ExceptionHandling/DefaultExceptionHandlerTests.cs index 342b477f..cffc6121 100644 --- a/test/System.Web.Http.Owin.Test/ExceptionHandling/DefaultExceptionHandlerTests.cs +++ b/test/System.Web.Http.Owin.Test/ExceptionHandling/DefaultExceptionHandlerTests.cs @@ -29,29 +29,13 @@ namespace System.Web.Http.Owin.ExceptionHandling Assert.ThrowsArgumentNull(() => product.HandleAsync(context, cancellationToken), "context"); } - [Fact] - public void HandleAsync_IfExceptionIsNull_Throws() - { - // Arrange - IExceptionHandler product = CreateProductUnderTest(); - ExceptionHandlerContext context = CreateContext(new ExceptionContext()); - Assert.Null(context.ExceptionContext.Exception); // Guard - CancellationToken cancellationToken = CancellationToken.None; - - // Act & Assert - Assert.ThrowsArgument(() => product.HandleAsync(context, cancellationToken), "context", - "ExceptionContext.Exception must not be null."); - } - [Fact] public void HandleAsync_IfRequestIsNull_Throws() { // Arrange IExceptionHandler product = CreateProductUnderTest(); - ExceptionHandlerContext context = new ExceptionHandlerContext(new ExceptionContext - { - Exception = CreateException() - }); + ExceptionHandlerContext context = new ExceptionHandlerContext(new ExceptionContext(CreateException(), ExceptionCatchBlocks.HttpServer)); + Assert.Null(context.ExceptionContext.Request); // Guard CancellationToken cancellationToken = CancellationToken.None; @@ -135,11 +119,9 @@ namespace System.Web.Http.Owin.ExceptionHandling private static ExceptionHandlerContext CreateValidContext(HttpRequestMessage request) { - return CreateContext(new ExceptionContext - { - Exception = CreateException(), - Request = request - }); + return CreateContext(new ExceptionContext(CreateException(), + ExceptionCatchBlocks.HttpServer, + request)); } } } diff --git a/test/System.Web.Http.Owin.Test/ExceptionHandling/EmptyExceptionLoggerTests.cs b/test/System.Web.Http.Owin.Test/ExceptionHandling/EmptyExceptionLoggerTests.cs index aa24aec8..c1bda51f 100644 --- a/test/System.Web.Http.Owin.Test/ExceptionHandling/EmptyExceptionLoggerTests.cs +++ b/test/System.Web.Http.Owin.Test/ExceptionHandling/EmptyExceptionLoggerTests.cs @@ -28,7 +28,7 @@ namespace System.Web.Http.Owin.ExceptionHandling private static ExceptionLoggerContext CreateContext() { - return new ExceptionLoggerContext(new ExceptionContext()); + return new ExceptionLoggerContext(new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer)); } private static EmptyExceptionLogger CreateProductUnderTest() diff --git a/test/System.Web.Http.Owin.Test/WebApiAppBuilderExtensionsTest.cs b/test/System.Web.Http.Owin.Test/WebApiAppBuilderExtensionsTest.cs index df0c3441..b6caa4be 100644 --- a/test/System.Web.Http.Owin.Test/WebApiAppBuilderExtensionsTest.cs +++ b/test/System.Web.Http.Owin.Test/WebApiAppBuilderExtensionsTest.cs @@ -261,10 +261,7 @@ namespace System.Web.Http.Owin { Assert.NotNull(actual); - ExceptionHandlerContext context = new ExceptionHandlerContext(new ExceptionContext() - { - Exception = new Exception() - }); + ExceptionHandlerContext context = new ExceptionHandlerContext(new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer)); CancellationToken cancellationToken = CancellationToken.None; expected @@ -284,10 +281,7 @@ namespace System.Web.Http.Owin { Assert.NotNull(actual); - ExceptionLoggerContext context = new ExceptionLoggerContext(new ExceptionContext() - { - Exception = new Exception() - }); + ExceptionLoggerContext context = new ExceptionLoggerContext(new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer)); CancellationToken cancellationToken = CancellationToken.None; expected diff --git a/test/System.Web.Http.Test/ExceptionHandling/CompositeExceptionLoggerTests.cs b/test/System.Web.Http.Test/ExceptionHandling/CompositeExceptionLoggerTests.cs index 4a60a7d9..c3b6cdb9 100644 --- a/test/System.Web.Http.Test/ExceptionHandling/CompositeExceptionLoggerTests.cs +++ b/test/System.Web.Http.Test/ExceptionHandling/CompositeExceptionLoggerTests.cs @@ -94,7 +94,7 @@ namespace System.Web.Http.ExceptionHandling }; IExceptionLogger product = CreateProductUnderTest(loggers); - ExceptionLoggerContext expectedContext = CreateContext(); + ExceptionLoggerContext expectedContext = CreateMinimalValidContext(); CancellationToken expectedCancellationToken = CreateCancellationToken(); // Act @@ -115,7 +115,7 @@ namespace System.Web.Http.ExceptionHandling IEnumerable loggers = new IExceptionLogger[] { null }; IExceptionLogger product = CreateProductUnderTest(loggers); - ExceptionLoggerContext context = CreateContext(); + ExceptionLoggerContext context = CreateMinimalValidContext(); CancellationToken cancellationToken = CreateCancellationToken(); // Act & Assert @@ -130,9 +130,9 @@ namespace System.Web.Http.ExceptionHandling return source.Token; } - private static ExceptionLoggerContext CreateContext() + private static ExceptionLoggerContext CreateMinimalValidContext() { - return new ExceptionLoggerContext(new ExceptionContext()); + return new ExceptionLoggerContext(new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer)); } private static IExceptionLogger CreateDummyLogger() diff --git a/test/System.Web.Http.Test/ExceptionHandling/DefaultExceptionHandlerTests.cs b/test/System.Web.Http.Test/ExceptionHandling/DefaultExceptionHandlerTests.cs index 3ff269ce..291974be 100644 --- a/test/System.Web.Http.Test/ExceptionHandling/DefaultExceptionHandlerTests.cs +++ b/test/System.Web.Http.Test/ExceptionHandling/DefaultExceptionHandlerTests.cs @@ -28,29 +28,12 @@ namespace System.Web.Http.ExceptionHandling Assert.ThrowsArgumentNull(() => product.HandleAsync(context, cancellationToken), "context"); } - [Fact] - public void HandleAsync_IfExceptionIsNull_Throws() - { - // Arrange - IExceptionHandler product = CreateProductUnderTest(); - ExceptionHandlerContext context = CreateContext(new ExceptionContext()); - Assert.Null(context.ExceptionContext.Exception); // Guard - CancellationToken cancellationToken = CancellationToken.None; - - // Act & Assert - Assert.ThrowsArgument(() => product.HandleAsync(context, cancellationToken), "context", - "ExceptionContext.Exception must not be null."); - } - [Fact] public void HandleAsync_IfRequestIsNull_Throws() { // Arrange IExceptionHandler product = CreateProductUnderTest(); - ExceptionHandlerContext context = new ExceptionHandlerContext(new ExceptionContext - { - Exception = CreateException() - }); + ExceptionHandlerContext context = new ExceptionHandlerContext(new ExceptionContext(CreateException(), ExceptionCatchBlocks.HttpServer)); Assert.Null(context.ExceptionContext.Request); // Guard CancellationToken cancellationToken = CancellationToken.None; @@ -155,22 +138,13 @@ namespace System.Web.Http.ExceptionHandling private static ExceptionHandlerContext CreateValidContext(HttpRequestMessage request) { - return CreateContext(new ExceptionContext - { - Exception = CreateException(), - Request = request - }); + return CreateContext(new ExceptionContext(CreateException(), ExceptionCatchBlocks.HttpServer, request)); } private static ExceptionHandlerContext CreateValidContext(HttpRequestMessage request, ExceptionContextCatchBlock catchBlock) { - return CreateContext(new ExceptionContext - { - Exception = CreateException(), - Request = request, - CatchBlock = catchBlock - }); + return CreateContext(new ExceptionContext(CreateException(), catchBlock, request)); } } } diff --git a/test/System.Web.Http.Test/ExceptionHandling/EmptyExceptionHandlerTests.cs b/test/System.Web.Http.Test/ExceptionHandling/EmptyExceptionHandlerTests.cs index fc14be90..bfc4a9ca 100644 --- a/test/System.Web.Http.Test/ExceptionHandling/EmptyExceptionHandlerTests.cs +++ b/test/System.Web.Http.Test/ExceptionHandling/EmptyExceptionHandlerTests.cs @@ -27,7 +27,7 @@ namespace System.Web.Http.ExceptionHandling private static ExceptionHandlerContext CreateContext() { - return new ExceptionHandlerContext(new ExceptionContext()); + return new ExceptionHandlerContext(new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer)); } private static EmptyExceptionHandler CreateProductUnderTest() diff --git a/test/System.Web.Http.Test/ExceptionHandling/ExceptionContextTests.cs b/test/System.Web.Http.Test/ExceptionHandling/ExceptionContextTests.cs index 5a8ba264..e9a01fba 100644 --- a/test/System.Web.Http.Test/ExceptionHandling/ExceptionContextTests.cs +++ b/test/System.Web.Http.Test/ExceptionHandling/ExceptionContextTests.cs @@ -8,85 +8,6 @@ namespace System.Web.Http.ExceptionHandling { public class ExceptionContextTests { - [Fact] - public void RequestSet_UpdatesValue() - { - // Arrange - ExceptionContext product = CreateProductUnderTest(); - - using (HttpRequestMessage expectedRequest = CreateRequest()) - { - // Act - product.Request = expectedRequest; - - // Assert - HttpRequestMessage request = product.Request; - Assert.Same(expectedRequest, request); - } - } - - [Fact] - public void RequestContextSet_UpdatesValue() - { - // Arrange - ExceptionContext product = CreateProductUnderTest(); - HttpRequestContext expectedRequestContext = CreateRequestContext(); - - // Act - product.RequestContext = expectedRequestContext; - - // Assert - HttpRequestContext requestContext = product.RequestContext; - Assert.Same(expectedRequestContext, requestContext); - } - - [Fact] - public void ControllerContextSet_UpdatesValue() - { - // Arrange - ExceptionContext product = CreateProductUnderTest(); - HttpControllerContext expectedControllerContext = CreateControllerContext(); - - // Act - product.ControllerContext = expectedControllerContext; - - // Assert - HttpControllerContext controllerContext = product.ControllerContext; - Assert.Same(expectedControllerContext, controllerContext); - } - - [Fact] - public void ActionContextSet_UpdatesValue() - { - // Arrange - ExceptionContext product = CreateProductUnderTest(); - HttpActionContext expectedActionContext = CreateActionContext(); - - // Act - product.ActionContext = expectedActionContext; - - // Assert - HttpActionContext actionContext = product.ActionContext; - Assert.Same(expectedActionContext, actionContext); - } - - [Fact] - public void ResponseSet_UpdatesValue() - { - // Arrange - ExceptionContext product = CreateProductUnderTest(); - - using (HttpResponseMessage expectedResponse = CreateResponse()) - { - // Act - product.Response = expectedResponse; - - // Assert - HttpResponseMessage response = product.Response; - Assert.Same(expectedResponse, response); - } - } - [Fact] public void ConstructorWithoutArguments_SetsPropertiesToSpecifiedValues() { @@ -94,12 +15,13 @@ namespace System.Web.Http.ExceptionHandling ExceptionContext product = CreateProductUnderTest(); // Assert - Assert.Null(product.Exception); + Assert.NotNull(product.Exception); + Assert.NotNull(product.CatchBlock); + Assert.Null(product.ActionContext); Assert.Null(product.ControllerContext); Assert.Null(product.RequestContext); Assert.Null(product.Request); - Assert.Null(product.CatchBlock); Assert.Null(product.Response); } @@ -427,36 +349,6 @@ namespace System.Web.Http.ExceptionHandling } } - [Fact] - public void ExceptionSet_UpdatesValue() - { - // Arrange - ExceptionContext product = CreateProductUnderTest(); - Exception expectedException = CreateException(); - - // Act - product.Exception = expectedException; - - // Assert - Exception exception = product.Exception; - Assert.Same(expectedException, exception); - } - - [Fact] - public void CatchBlockSet_UpdatesValue() - { - // Arrange - ExceptionContext product = CreateProductUnderTest(); - ExceptionContextCatchBlock expectedCatchBlock = CreateCatchBlock(); - - // Act - product.CatchBlock = expectedCatchBlock; - - // Assert - ExceptionContextCatchBlock catchBlock = product.CatchBlock; - Assert.Same(expectedCatchBlock, catchBlock); - } - private static HttpActionContext CreateActionContext() { return new HttpActionContext(); @@ -497,7 +389,7 @@ namespace System.Web.Http.ExceptionHandling private static ExceptionContext CreateProductUnderTest() { - return new ExceptionContext(); + return new ExceptionContext(CreateException(), CreateCatchBlock()); } private static ExceptionContext CreateProductUnderTest(Exception exception, diff --git a/test/System.Web.Http.Test/ExceptionHandling/ExceptionHandlerContextTests.cs b/test/System.Web.Http.Test/ExceptionHandling/ExceptionHandlerContextTests.cs index a1006387..8dd84266 100644 --- a/test/System.Web.Http.Test/ExceptionHandling/ExceptionHandlerContextTests.cs +++ b/test/System.Web.Http.Test/ExceptionHandling/ExceptionHandlerContextTests.cs @@ -23,7 +23,7 @@ namespace System.Web.Http.ExceptionHandling public void ExceptionContextGet_ReturnsSpecifiedInstance() { // Arrange - ExceptionContext expectedContext = CreateContext(); + ExceptionContext expectedContext = CreateMinimalContext(); ExceptionHandlerContext product = CreateProductUnderTest(expectedContext); // Act @@ -37,7 +37,7 @@ namespace System.Web.Http.ExceptionHandling public void ResultSet_UpdatesValue() { // Arrange - ExceptionHandlerContext product = CreateProductUnderTest(); + ExceptionHandlerContext product = CreateProductUnderTest(CreateMinimalContext()); IHttpActionResult expectedResult = CreateDummyResult(); // Act @@ -53,10 +53,7 @@ namespace System.Web.Http.ExceptionHandling { // Arrange Exception expectedException = new InvalidOperationException(); - ExceptionContext context = new ExceptionContext - { - Exception = expectedException - }; + ExceptionContext context = new ExceptionContext(expectedException, ExceptionCatchBlocks.HttpServer); ExceptionHandlerContext product = CreateProductUnderTest(context); // Act @@ -71,10 +68,7 @@ namespace System.Web.Http.ExceptionHandling { // Arrange ExceptionContextCatchBlock expectedCatchBlock = new ExceptionContextCatchBlock("IgnoreName", false, false); - ExceptionContext context = new ExceptionContext - { - CatchBlock = expectedCatchBlock - }; + ExceptionContext context = new ExceptionContext(new Exception(), expectedCatchBlock); ExceptionHandlerContext product = CreateProductUnderTest(context); // Act @@ -90,10 +84,7 @@ namespace System.Web.Http.ExceptionHandling // Arrange using (HttpRequestMessage expectedRequest = new HttpRequestMessage()) { - ExceptionContext context = new ExceptionContext - { - Request = expectedRequest - }; + ExceptionContext context = CreateMinimalContext(expectedRequest); ExceptionHandlerContext product = CreateProductUnderTest(context); // Act @@ -109,10 +100,7 @@ namespace System.Web.Http.ExceptionHandling { // Arrange HttpRequestContext expectedRequestContext = new HttpRequestContext(); - ExceptionContext context = new ExceptionContext - { - RequestContext = expectedRequestContext - }; + ExceptionContext context = CreateMinimalContext(expectedRequestContext); ExceptionHandlerContext product = CreateProductUnderTest(context); // Act @@ -122,9 +110,17 @@ namespace System.Web.Http.ExceptionHandling Assert.Same(expectedRequestContext, requestContext); } - private static ExceptionContext CreateContext() + private static ExceptionContext CreateMinimalContext(HttpRequestContext context = null) { - return new ExceptionContext(); + return new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer) + { + RequestContext = context, + }; + } + + private static ExceptionContext CreateMinimalContext(HttpRequestMessage request) + { + return new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer, request); } private static IHttpActionResult CreateDummyResult() @@ -132,11 +128,6 @@ namespace System.Web.Http.ExceptionHandling return new Mock(MockBehavior.Strict).Object; } - private static ExceptionHandlerContext CreateProductUnderTest() - { - return CreateProductUnderTest(CreateContext()); - } - private static ExceptionHandlerContext CreateProductUnderTest(ExceptionContext exceptionContext) { return new ExceptionHandlerContext(exceptionContext); diff --git a/test/System.Web.Http.Test/ExceptionHandling/ExceptionHandlerExtensionsTests.cs b/test/System.Web.Http.Test/ExceptionHandling/ExceptionHandlerExtensionsTests.cs index c3fa5b8a..8c0fe9ff 100644 --- a/test/System.Web.Http.Test/ExceptionHandling/ExceptionHandlerExtensionsTests.cs +++ b/test/System.Web.Http.Test/ExceptionHandling/ExceptionHandlerExtensionsTests.cs @@ -23,7 +23,7 @@ namespace System.Web.Http.ExceptionHandling using (CancellationTokenSource tokenSource = CreateCancellationTokenSource()) { - ExceptionContext expectedContext = CreateContext(); + ExceptionContext expectedContext = CreateMinimalValidContext(); CancellationToken expectedCancellationToken = tokenSource.Token; // Act @@ -45,7 +45,7 @@ namespace System.Web.Http.ExceptionHandling { // Arrange IExceptionHandler handler = CreateStubHandler(); - ExceptionContext context = CreateContext(); + ExceptionContext context = CreateMinimalValidContext(); CancellationToken cancellationToken = CancellationToken.None; // Act @@ -72,7 +72,7 @@ namespace System.Web.Http.ExceptionHandling using (CancellationTokenSource tokenSource = CreateCancellationTokenSource()) { - ExceptionContext context = CreateContext(); + ExceptionContext context = CreateMinimalValidContext(); CancellationToken expectedCancellationToken = tokenSource.Token; // Act @@ -100,7 +100,7 @@ namespace System.Web.Http.ExceptionHandling IHttpActionResult result = mock.Object; IExceptionHandler handler = CreateResultHandler(result); - ExceptionContext context = CreateContext(); + ExceptionContext context = CreateMinimalValidContext(); CancellationToken cancellationToken = CancellationToken.None; // Act @@ -119,7 +119,7 @@ namespace System.Web.Http.ExceptionHandling { // Arrange IExceptionHandler handler = null; - ExceptionContext context = CreateContext(); + ExceptionContext context = CreateMinimalValidContext(); CancellationToken cancellationToken = CancellationToken.None; // Act & Assert @@ -150,7 +150,7 @@ namespace System.Web.Http.ExceptionHandling .Returns(Task.FromResult(null)); IHttpActionResult result = mock.Object; IExceptionHandler handler = CreateResultHandler(result); - ExceptionContext context = CreateContext(); + ExceptionContext context = CreateMinimalValidContext(); CancellationToken cancellationToken = CancellationToken.None; // Act @@ -172,9 +172,9 @@ namespace System.Web.Http.ExceptionHandling return new CancellationTokenSource(); } - private static ExceptionContext CreateContext() + private static ExceptionContext CreateMinimalValidContext() { - return new ExceptionContext(); + return new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer); } private static IExceptionHandler CreateDummyHandler() diff --git a/test/System.Web.Http.Test/ExceptionHandling/ExceptionHandlerTests.cs b/test/System.Web.Http.Test/ExceptionHandling/ExceptionHandlerTests.cs index 8ab9c134..39148327 100644 --- a/test/System.Web.Http.Test/ExceptionHandling/ExceptionHandlerTests.cs +++ b/test/System.Web.Http.Test/ExceptionHandling/ExceptionHandlerTests.cs @@ -23,22 +23,6 @@ namespace System.Web.Http.ExceptionHandling Assert.ThrowsArgumentNull(() => product.HandleAsync(context, cancellationToken), "context"); } - [Fact] - public void HandleAsync_IfExceptionIsNull_Throws() - { - // Arrange - Mock mock = new Mock(MockBehavior.Strict); - IExceptionHandler product = mock.Object; - - ExceptionHandlerContext context = CreateContext(CreateExceptionContext()); - Assert.Null(context.ExceptionContext.Exception); // Guard - CancellationToken cancellationToken = CancellationToken.None; - - // Act & Assert - Assert.ThrowsArgument(() => product.HandleAsync(context, cancellationToken), "context", - "ExceptionContext.Exception must not be null."); - } - [Fact] public void HandleAsync_IfShouldHandleReturnsTrue_DelegatesToHandleAsyncCore() { @@ -52,7 +36,7 @@ namespace System.Web.Http.ExceptionHandling IExceptionHandler product = mock.Object; - ExceptionHandlerContext expectedContext = CreateValidContext(); + ExceptionHandlerContext expectedContext = CreateMinimalValidHandlerContext(); using (CancellationTokenSource tokenSource = CreateTokenSource()) { @@ -78,7 +62,7 @@ namespace System.Web.Http.ExceptionHandling IExceptionHandler product = mock.Object; - ExceptionHandlerContext expectedContext = CreateValidContext(); + ExceptionHandlerContext expectedContext = CreateMinimalValidHandlerContext(); CancellationToken expectedCancellationToken = CancellationToken.None; // Act @@ -101,7 +85,7 @@ namespace System.Web.Http.ExceptionHandling mock.CallBase = true; ExceptionHandler product = mock.Object; - ExceptionHandlerContext expectedContext = CreateContext(); + ExceptionHandlerContext expectedContext = CreateMinimalValidHandlerContext(); CancellationToken cancellationToken = CancellationToken.None; // Act @@ -123,7 +107,7 @@ namespace System.Web.Http.ExceptionHandling mock.CallBase = true; ExceptionHandler product = mock.Object; - ExceptionHandlerContext context = CreateContext(); + ExceptionHandlerContext context = CreateMinimalValidHandlerContext(); // Act & Assert Assert.DoesNotThrow(() => product.Handle(context)); @@ -143,22 +127,6 @@ namespace System.Web.Http.ExceptionHandling Assert.ThrowsArgumentNull(() => product.ShouldHandle(context), "context"); } - [Fact] - public void ShouldHandle_IfCallStackIsNull_Throws() - { - // Arrange - Mock mock = new Mock(); - mock.CallBase = true; - ExceptionHandler product = mock.Object; - - ExceptionHandlerContext context = new ExceptionHandlerContext(new ExceptionContext()); - Assert.Null(context.ExceptionContext.CatchBlock); // Guard - - // Act & Assert - Assert.ThrowsArgument(() => product.ShouldHandle(context), "context", - "ExceptionContext.CatchBlock must not be null."); - } - [Theory] [InlineData(true)] [InlineData(false)] @@ -169,10 +137,8 @@ namespace System.Web.Http.ExceptionHandling mock.CallBase = true; ExceptionHandler product = mock.Object; - ExceptionHandlerContext context = CreateContext(new ExceptionContext - { - CatchBlock = new ExceptionContextCatchBlock("IgnoreCaughtAt", isTopLevelCatchBlock, callsHandler: false) - }); + ExceptionHandlerContext context = CreateContext(new ExceptionContext(new Exception(), + new ExceptionContextCatchBlock("IgnoreCaughtAt", isTopLevelCatchBlock, callsHandler: false))); // Act bool shouldHandle = product.ShouldHandle(context); @@ -187,19 +153,14 @@ namespace System.Web.Http.ExceptionHandling return source.Task; } - private static ExceptionHandlerContext CreateContext() - { - return CreateContext(CreateExceptionContext()); - } - private static ExceptionHandlerContext CreateContext(ExceptionContext exceptionContext) { return new ExceptionHandlerContext(exceptionContext); } - private static ExceptionContext CreateExceptionContext() + private static ExceptionContext CreateMinimalValidExceptionContext() { - return new ExceptionContext(); + return new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer); } private static CancellationTokenSource CreateTokenSource() @@ -207,12 +168,9 @@ namespace System.Web.Http.ExceptionHandling return new CancellationTokenSource(); } - private static ExceptionHandlerContext CreateValidContext() + private static ExceptionHandlerContext CreateMinimalValidHandlerContext() { - return new ExceptionHandlerContext(new ExceptionContext - { - Exception = new Exception() - }); + return new ExceptionHandlerContext(CreateMinimalValidExceptionContext()); } } } diff --git a/test/System.Web.Http.Test/ExceptionHandling/ExceptionLoggerContextTests.cs b/test/System.Web.Http.Test/ExceptionHandling/ExceptionLoggerContextTests.cs index ac90f71d..b668e2ed 100644 --- a/test/System.Web.Http.Test/ExceptionHandling/ExceptionLoggerContextTests.cs +++ b/test/System.Web.Http.Test/ExceptionHandling/ExceptionLoggerContextTests.cs @@ -22,7 +22,7 @@ namespace System.Web.Http.ExceptionHandling public void ExceptionContextGet_ReturnsSpecifiedInstance() { // Arrange - ExceptionContext expectedContext = CreateContext(); + ExceptionContext expectedContext = CreateMinimalValidContext(); ExceptionLoggerContext product = CreateProductUnderTest(expectedContext); // Act @@ -37,10 +37,7 @@ namespace System.Web.Http.ExceptionHandling { // Arrange Exception expectedException = new InvalidOperationException(); - ExceptionContext context = new ExceptionContext - { - Exception = expectedException - }; + ExceptionContext context = new ExceptionContext(expectedException, ExceptionCatchBlocks.HttpServer); ExceptionLoggerContext product = CreateProductUnderTest(context); // Act @@ -55,10 +52,7 @@ namespace System.Web.Http.ExceptionHandling { // Arrange ExceptionContextCatchBlock expectedCatchBlock = new ExceptionContextCatchBlock("IgnoreName", false, false); - ExceptionContext context = new ExceptionContext - { - CatchBlock = expectedCatchBlock - }; + ExceptionContext context = new ExceptionContext(new Exception(), expectedCatchBlock); ExceptionLoggerContext product = CreateProductUnderTest(context); // Act @@ -74,10 +68,8 @@ namespace System.Web.Http.ExceptionHandling // Arrange using (HttpRequestMessage expectedRequest = new HttpRequestMessage()) { - ExceptionContext context = new ExceptionContext - { - Request = expectedRequest - }; + ExceptionContext context = new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer, expectedRequest); + ExceptionLoggerContext product = CreateProductUnderTest(context); // Act @@ -93,10 +85,7 @@ namespace System.Web.Http.ExceptionHandling { // Arrange HttpRequestContext expectedRequestContext = new HttpRequestContext(); - ExceptionContext context = new ExceptionContext - { - RequestContext = expectedRequestContext - }; + ExceptionContext context = CreateMinimalValidContext(expectedRequestContext); ExceptionLoggerContext product = CreateProductUnderTest(context); // Act @@ -112,11 +101,10 @@ namespace System.Web.Http.ExceptionHandling public void CallsHandlerGet_ReturnsCatchBlockCallsHandler(bool expectedCallsHandler) { // Arrange - ExceptionContext context = new ExceptionContext - { - CatchBlock = new ExceptionContextCatchBlock("IgnoreName", isTopLevel: false, - callsHandler: expectedCallsHandler) - }; + ExceptionContext context = new ExceptionContext(new Exception(), + new ExceptionContextCatchBlock("IgnoreName", isTopLevel: false, + callsHandler: expectedCallsHandler)); + ExceptionLoggerContext product = CreateProductUnderTest(context); // Act @@ -126,22 +114,12 @@ namespace System.Web.Http.ExceptionHandling Assert.Equal(expectedCallsHandler, callsHandler); } - [Fact] - public void CallsHandlerGet_IfCatchBlockIsNull_Throws() + private static ExceptionContext CreateMinimalValidContext(HttpRequestContext context = null) { - // Arrange - ExceptionContext context = new ExceptionContext(); - Assert.Null(context.CatchBlock); // Guard - ExceptionLoggerContext product = CreateProductUnderTest(context); - - // Act & Assert - Assert.Throws(() => product.CallsHandler, - "ExceptionContext.CatchBlock must not be null."); - } - - private static ExceptionContext CreateContext() - { - return new ExceptionContext(); + return new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer) + { + RequestContext = context, + }; } private static ExceptionLoggerContext CreateProductUnderTest(ExceptionContext exceptionContext) diff --git a/test/System.Web.Http.Test/ExceptionHandling/ExceptionLoggerExtensionsTests.cs b/test/System.Web.Http.Test/ExceptionHandling/ExceptionLoggerExtensionsTests.cs index 616f8ded..1a2a5131 100644 --- a/test/System.Web.Http.Test/ExceptionHandling/ExceptionLoggerExtensionsTests.cs +++ b/test/System.Web.Http.Test/ExceptionHandling/ExceptionLoggerExtensionsTests.cs @@ -23,7 +23,7 @@ namespace System.Web.Http.ExceptionHandling using (CancellationTokenSource tokenSource = CreateCancellationTokenSource()) { - ExceptionContext expectedContext = CreateContext(); + ExceptionContext expectedContext = CreateMinimalValidContext(); CancellationToken expectedCancellationToken = tokenSource.Token; // Act @@ -43,7 +43,7 @@ namespace System.Web.Http.ExceptionHandling { // Arrange IExceptionLogger logger = null; - ExceptionContext context = CreateContext(); + ExceptionContext context = CreateMinimalValidContext(); CancellationToken cancellationToken = CancellationToken.None; // Act & Assert @@ -76,9 +76,9 @@ namespace System.Web.Http.ExceptionHandling return source.Task; } - private static ExceptionContext CreateContext() + private static ExceptionContext CreateMinimalValidContext() { - return new ExceptionContext(); + return new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer); } private static IExceptionLogger CreateDummyLogger() diff --git a/test/System.Web.Http.Test/ExceptionHandling/ExceptionLoggerTests.cs b/test/System.Web.Http.Test/ExceptionHandling/ExceptionLoggerTests.cs index ec904add..86d8f714 100644 --- a/test/System.Web.Http.Test/ExceptionHandling/ExceptionLoggerTests.cs +++ b/test/System.Web.Http.Test/ExceptionHandling/ExceptionLoggerTests.cs @@ -25,22 +25,6 @@ namespace System.Web.Http.ExceptionHandling Assert.ThrowsArgumentNull(() => product.LogAsync(context, cancellationToken), "context"); } - [Fact] - public void LogAsync_IfExceptionIsNull_Throws() - { - // Arrange - Mock mock = new Mock(MockBehavior.Strict); - IExceptionLogger product = mock.Object; - - ExceptionLoggerContext context = CreateContext(CreateExceptionContext()); - Assert.Null(context.ExceptionContext.Exception); // Guard - CancellationToken cancellationToken = CancellationToken.None; - - // Act & Assert - Assert.ThrowsArgument(() => product.LogAsync(context, cancellationToken), "context", - "ExceptionContext.Exception must not be null."); - } - [Fact] public void LogAsync_IfShouldLogReturnsTrue_DelegatesToLogAsyncCore() { @@ -54,7 +38,7 @@ namespace System.Web.Http.ExceptionHandling IExceptionLogger product = mock.Object; - ExceptionLoggerContext expectedContext = CreateValidContext(); + ExceptionLoggerContext expectedContext = CreateMinimalValidLoggerContext(); using (CancellationTokenSource tokenSource = CreateTokenSource()) { @@ -80,7 +64,7 @@ namespace System.Web.Http.ExceptionHandling IExceptionLogger product = mock.Object; - ExceptionLoggerContext expectedContext = CreateValidContext(); + ExceptionLoggerContext expectedContext = CreateMinimalValidLoggerContext(); CancellationToken expectedCancellationToken = CancellationToken.None; // Act @@ -103,7 +87,7 @@ namespace System.Web.Http.ExceptionHandling mock.CallBase = true; ExceptionLogger product = mock.Object; - ExceptionLoggerContext expectedContext = CreateContext(); + ExceptionLoggerContext expectedContext = CreateMinimalValidLoggerContext(); CancellationToken cancellationToken = CancellationToken.None; // Act @@ -125,7 +109,7 @@ namespace System.Web.Http.ExceptionHandling mock.CallBase = true; ExceptionLogger product = mock.Object; - ExceptionLoggerContext context = CreateContext(); + ExceptionLoggerContext context = CreateMinimalValidLoggerContext(); // Act & Assert Assert.DoesNotThrow(() => product.Log(context)); @@ -145,22 +129,6 @@ namespace System.Web.Http.ExceptionHandling Assert.ThrowsArgumentNull(() => product.ShouldLog(context), "context"); } - [Fact] - public void ShouldLog_IfExceptionIsNull_Throws() - { - // Arrange - Mock mock = new Mock(); - mock.CallBase = true; - ExceptionLogger product = mock.Object; - - ExceptionLoggerContext context = CreateContext(CreateExceptionContext()); - Assert.Null(context.ExceptionContext.Exception); // Guard - - // Act & Assert - Assert.ThrowsArgument(() => product.ShouldLog(context), "context", - "ExceptionContext.Exception must not be null."); - } - [Fact] public void ShouldLog_IfExceptionDataIsNull_ReturnsTrue() { @@ -170,7 +138,7 @@ namespace System.Web.Http.ExceptionHandling ExceptionLogger product = mock.Object; Exception exception = CreateException(data: null); - ExceptionLoggerContext context = CreateValidContext(exception); + ExceptionLoggerContext context = CreateMinimalValidLoggerContext(exception); // Act bool shouldLog = product.ShouldLog(context); @@ -198,7 +166,7 @@ namespace System.Web.Http.ExceptionHandling dataMock.Setup(d => d.IsReadOnly).Returns(true); IDictionary data = dataMock.Object; Exception exception = CreateException(data); - ExceptionLoggerContext context = CreateValidContext(exception); + ExceptionLoggerContext context = CreateMinimalValidLoggerContext(exception); // Act bool shouldLog = product.ShouldLog(context); @@ -217,7 +185,7 @@ namespace System.Web.Http.ExceptionHandling IDictionary data = new Dictionary(); Exception exception = CreateException(data); - ExceptionLoggerContext context = CreateValidContext(exception); + ExceptionLoggerContext context = CreateMinimalValidLoggerContext(exception); // Act bool shouldLog = product.ShouldLog(context); @@ -242,7 +210,7 @@ namespace System.Web.Http.ExceptionHandling IDictionary data = new Dictionary(); data.Add(ExceptionLogger.LoggedByKey, loggedBy); Exception exception = CreateException(data); - ExceptionLoggerContext context = CreateValidContext(exception); + ExceptionLoggerContext context = CreateMinimalValidLoggerContext(exception); // Act bool shouldLog = product.ShouldLog(context); @@ -267,7 +235,7 @@ namespace System.Web.Http.ExceptionHandling ICollection loggedBy = new List() { product }; data.Add(ExceptionLogger.LoggedByKey, loggedBy); Exception exception = CreateException(data); - ExceptionLoggerContext context = CreateValidContext(exception); + ExceptionLoggerContext context = CreateMinimalValidLoggerContext(exception); // Act bool shouldLog = product.ShouldLog(context); @@ -291,7 +259,7 @@ namespace System.Web.Http.ExceptionHandling IDictionary data = new Dictionary(); data.Add(ExceptionLogger.LoggedByKey, null); Exception exception = CreateException(data); - ExceptionLoggerContext context = CreateValidContext(exception); + ExceptionLoggerContext context = CreateMinimalValidLoggerContext(exception); // Act bool shouldLog = product.ShouldLog(context); @@ -309,11 +277,6 @@ namespace System.Web.Http.ExceptionHandling return source.Task; } - private static ExceptionLoggerContext CreateContext() - { - return CreateContext(CreateExceptionContext()); - } - private static ExceptionLoggerContext CreateContext(ExceptionContext exceptionContext) { return new ExceptionLoggerContext(exceptionContext); @@ -326,9 +289,9 @@ namespace System.Web.Http.ExceptionHandling return mock.Object; } - private static ExceptionContext CreateExceptionContext() + private static ExceptionContext CreateMinimalValidExceptionContext() { - return new ExceptionContext(); + return new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer); } private static CancellationTokenSource CreateTokenSource() @@ -336,17 +299,14 @@ namespace System.Web.Http.ExceptionHandling return new CancellationTokenSource(); } - private static ExceptionLoggerContext CreateValidContext() + private static ExceptionLoggerContext CreateMinimalValidLoggerContext() { - return CreateValidContext(new Exception()); + return CreateMinimalValidLoggerContext(new Exception()); } - private static ExceptionLoggerContext CreateValidContext(Exception exception) + private static ExceptionLoggerContext CreateMinimalValidLoggerContext(Exception exception) { - return CreateContext(new ExceptionContext - { - Exception = exception - }); + return CreateContext(new ExceptionContext(exception, ExceptionCatchBlocks.HttpServer)); } private class Dictionary : DictionaryBase diff --git a/test/System.Web.Http.Test/ExceptionHandling/LastChanceExceptionHandlerTests.cs b/test/System.Web.Http.Test/ExceptionHandling/LastChanceExceptionHandlerTests.cs index f9cd0a94..02e5581c 100644 --- a/test/System.Web.Http.Test/ExceptionHandling/LastChanceExceptionHandlerTests.cs +++ b/test/System.Web.Http.Test/ExceptionHandling/LastChanceExceptionHandlerTests.cs @@ -95,16 +95,16 @@ namespace System.Web.Http.ExceptionHandling MediaTypeFormatter expectedFormatter = CreateDummyFormatter(); configuration.Formatters.Add(expectedFormatter); - ExceptionHandlerContext context = new ExceptionHandlerContext(new ExceptionContext + ExceptionHandlerContext context = new ExceptionHandlerContext(new ExceptionContext( + exception: expectedException, + catchBlock: CreateTopLevelCatchBlock(), + request: expectedRequest) { - Exception = expectedException, - CatchBlock = CreateTopLevelCatchBlock(), RequestContext = new HttpRequestContext - { - Configuration = configuration, - IncludeErrorDetail = includeDetail - }, - Request = expectedRequest + { + Configuration = configuration, + IncludeErrorDetail = includeDetail + }, }); CancellationToken cancellationToken = CancellationToken.None; @@ -138,16 +138,16 @@ namespace System.Web.Http.ExceptionHandling using (HttpConfiguration configuration = CreateConfiguration()) using (HttpRequestMessage request = CreateRequest()) { - ExceptionContext context = new ExceptionContext - { - Exception = exception, - CatchBlock = CreateNonTopLevelCatchBlock(), - RequestContext = new HttpRequestContext + ExceptionContext context = new ExceptionContext( + exception, + CreateNonTopLevelCatchBlock(), + request) { - Configuration = configuration - }, - Request = request - }; + RequestContext = new HttpRequestContext + { + Configuration = configuration + }, + }; // More Arrange; then Act & Assert TestHandleAsyncLeavesResultNull(context); @@ -164,56 +164,6 @@ namespace System.Web.Http.ExceptionHandling TestHandleAsyncLeavesResultNull(context); } - [Fact] - public void HandleAsync_IfExceptionIsNull_LeavesResultNull() - { - // Arrange - Exception exception = null; - - using (HttpConfiguration configuration = CreateConfiguration()) - using (HttpRequestMessage request = CreateRequest()) - { - ExceptionContext context = new ExceptionContext - { - Exception = exception, - CatchBlock = CreateTopLevelCatchBlock(), - RequestContext = new HttpRequestContext - { - Configuration = configuration - }, - Request = request - }; - - // More Arrange; then Act & Assert - TestHandleAsyncLeavesResultNull(context); - } - } - - [Fact] - public void HandleAsync_IfCatchBlockIsNull_LeavesResultNull() - { - // Arrange - Exception exception = CreateDummyException(); - - using (HttpConfiguration configuration = CreateConfiguration()) - using (HttpRequestMessage request = CreateRequest()) - { - ExceptionContext context = new ExceptionContext - { - Exception = exception, - CatchBlock = null, - RequestContext = new HttpRequestContext - { - Configuration = configuration - }, - Request = request - }; - - // More Arrange; then Act & Assert - TestHandleAsyncLeavesResultNull(context); - } - } - [Fact] public void HandleAsync_IfRequestIsNull_LeavesResultNull() { @@ -222,10 +172,10 @@ namespace System.Web.Http.ExceptionHandling using (HttpConfiguration configuration = CreateConfiguration()) { - ExceptionContext context = new ExceptionContext + ExceptionContext context = new ExceptionContext( + exception, + CreateTopLevelCatchBlock()) { - Exception = exception, - CatchBlock = CreateTopLevelCatchBlock(), RequestContext = new HttpRequestContext { Configuration = configuration @@ -246,10 +196,10 @@ namespace System.Web.Http.ExceptionHandling using (HttpRequestMessage request = CreateRequest()) { - ExceptionContext context = new ExceptionContext + ExceptionContext context = new ExceptionContext( + exception, + CreateTopLevelCatchBlock()) { - Exception = exception, - CatchBlock = CreateTopLevelCatchBlock(), RequestContext = null, Request = request }; @@ -267,10 +217,10 @@ namespace System.Web.Http.ExceptionHandling using (HttpRequestMessage request = CreateRequest()) { - ExceptionContext context = new ExceptionContext + ExceptionContext context = new ExceptionContext( + exception, + CreateTopLevelCatchBlock()) { - Exception = exception, - CatchBlock = CreateTopLevelCatchBlock(), RequestContext = new HttpRequestContext { Configuration = null @@ -294,10 +244,10 @@ namespace System.Web.Http.ExceptionHandling { configuration.Services.Clear(typeof(IContentNegotiator)); - ExceptionContext context = new ExceptionContext + ExceptionContext context = new ExceptionContext( + exception, + CreateTopLevelCatchBlock()) { - Exception = exception, - CatchBlock = CreateTopLevelCatchBlock(), RequestContext = new HttpRequestContext { Configuration = configuration @@ -362,7 +312,12 @@ namespace System.Web.Http.ExceptionHandling private static ExceptionHandlerContext CreateContext() { - return new ExceptionHandlerContext(new ExceptionContext()); + return new ExceptionHandlerContext(CreateMinimalValidExceptionContext()); + } + + private static ExceptionContext CreateMinimalValidExceptionContext() + { + return new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer); } private static ExceptionHandlerContext CreateContext(ExceptionContext exceptionContext) diff --git a/test/System.Web.Http.WebHost.Test/WebHostExceptionHandlerTests.cs b/test/System.Web.Http.WebHost.Test/WebHostExceptionHandlerTests.cs index ccd7279d..81d6f948 100644 --- a/test/System.Web.Http.WebHost.Test/WebHostExceptionHandlerTests.cs +++ b/test/System.Web.Http.WebHost.Test/WebHostExceptionHandlerTests.cs @@ -246,8 +246,9 @@ namespace System.Web.Http.WebHost // Arrange using (HttpResponseMessage response = CreateResponse()) { - ExceptionHandlerContext context = CreateValidContext(null, - WebHostExceptionCatchBlocks.HttpControllerHandlerBufferContent); + ExceptionHandlerContext context = CreateContext( + CreateMinimalValidExceptionContext(WebHostExceptionCatchBlocks.HttpControllerHandlerBufferContent)); + Assert.Null(context.ExceptionContext.Request); // Guard CancellationToken cancellationToken = CancellationToken.None; @@ -364,7 +365,7 @@ namespace System.Web.Http.WebHost private static ExceptionHandlerContext CreateContext() { - return CreateContext(new ExceptionContext()); + return CreateContext(new ExceptionContext(new Exception(), ExceptionCatchBlocks.HttpServer)); } private static ExceptionHandlerContext CreateContext(ExceptionContext exceptionContext) @@ -406,12 +407,15 @@ namespace System.Web.Http.WebHost private static ExceptionHandlerContext CreateValidContext(HttpRequestMessage request, ExceptionContextCatchBlock catchBlock) { - return CreateContext(new ExceptionContext - { - Exception = new InvalidOperationException(), - CatchBlock = catchBlock, - Request = request - }); + return CreateContext(CreateMinimalValidExceptionContext(catchBlock, request)); + } + + private static ExceptionContext CreateMinimalValidExceptionContext(ExceptionContextCatchBlock catchBlock, HttpRequestMessage request = null) + { + return new ExceptionContext(new InvalidOperationException(), catchBlock) + { + Request = request, + }; } } }