Exemple #1
0
        public async Task ExecuteExceptionFilterAsync_Traces()
        {
            // Arrange
            HttpRequestMessage      request    = new HttpRequestMessage();
            HttpResponseMessage     response   = new HttpResponseMessage();
            Mock <IExceptionFilter> mockFilter = new Mock <IExceptionFilter>()
            {
                CallBase = true
            };

            mockFilter.Setup(f => f.ExecuteExceptionFilterAsync(It.IsAny <HttpActionExecutedContext>(), It.IsAny <CancellationToken>()))
            .Returns(TaskHelpers.Completed());
            HttpActionExecutedContext actionExecutedContext = ContextUtil.GetActionExecutedContext(request, response);
            TestTraceWriter           traceWriter           = new TestTraceWriter();
            ExceptionFilterTracer     tracer = new ExceptionFilterTracer(mockFilter.Object, traceWriter);

            TraceRecord[] expectedTraces = new TraceRecord[]
            {
                new TraceRecord(request, TraceCategories.FiltersCategory, TraceLevel.Info)
                {
                    Kind = TraceKind.Begin, Operation = "ExecuteExceptionFilterAsync"
                },
                new TraceRecord(request, TraceCategories.FiltersCategory, TraceLevel.Info)
                {
                    Kind = TraceKind.End, Operation = "ExecuteExceptionFilterAsync"
                },
            };

            // Act
            var filter = (IExceptionFilter)tracer;
            await filter.ExecuteExceptionFilterAsync(actionExecutedContext, CancellationToken.None);

            // Assert
            Assert.Equal <TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
        }
        public void ExecuteExceptionFilterAsync_Throws_And_Traces_When_Inner_OnException_Throws()
        {
            // Arrange
            HttpRequestMessage              request  = new HttpRequestMessage();
            HttpResponseMessage             response = new HttpResponseMessage();
            Mock <ExceptionFilterAttribute> mockAttr = new Mock <ExceptionFilterAttribute>()
            {
                CallBase = true
            };
            InvalidOperationException exception = new InvalidOperationException("test");

            mockAttr.Setup(a => a.OnException(It.IsAny <HttpActionExecutedContext>())).Callback(() => { throw exception; });
            Mock <HttpActionDescriptor> mockActionDescriptor = new Mock <HttpActionDescriptor>()
            {
                CallBase = true
            };

            mockActionDescriptor.Setup(a => a.ActionName).Returns("test");
            mockActionDescriptor.Setup(a => a.GetParameters()).Returns(new Collection <HttpParameterDescriptor>(new HttpParameterDescriptor[0]));
            HttpActionExecutedContext actionExecutedContext = ContextUtil.GetActionExecutedContext(request, response);
            TestTraceWriter           traceWriter           = new TestTraceWriter();
            IExceptionFilter          tracer = new ExceptionFilterAttributeTracer(mockAttr.Object, traceWriter) as IExceptionFilter;

            TraceRecord[] expectedTraces = new TraceRecord[]
            {
                new TraceRecord(request, TraceCategories.FiltersCategory, TraceLevel.Info)
                {
                    Kind = TraceKind.Begin, Operation = "OnExceptionAsync"
                },
                new TraceRecord(request, TraceCategories.FiltersCategory, TraceLevel.Error)
                {
                    Kind = TraceKind.End, Operation = "OnExceptionAsync"
                }
            };

            // Act
            Exception thrown = null;

            // We separate the task creation and the wait, to verify we don't throw at task creation
            // and that the exception flows inside the task (even if an Aggregate Exception was thrown).
            var task = tracer.ExecuteExceptionFilterAsync(actionExecutedContext, CancellationToken.None);

            try
            {
                task.Wait();
            }
            catch (AggregateException ex)
            {
                Assert.Equal(1, ex.InnerExceptions.Count);

                thrown = ex.InnerException;
            }

            // Assert
            Assert.Same(exception, thrown);
            Assert.Same(exception, traceWriter.Traces[1].Exception);
            Assert.Equal <TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
        }
Exemple #3
0
        public async Task ExecuteExceptionFilterAsync_Traces()
        {
            // Arrange
            HttpRequestMessage              request  = new HttpRequestMessage();
            HttpResponseMessage             response = new HttpResponseMessage();
            Mock <ExceptionFilterAttribute> mockAttr = new Mock <ExceptionFilterAttribute>()
            {
                CallBase = true
            };
            Mock <HttpActionDescriptor> mockActionDescriptor = new Mock <HttpActionDescriptor>()
            {
                CallBase = true
            };

            mockActionDescriptor.Setup(a => a.ActionName).Returns("test");
            mockActionDescriptor
            .Setup(a => a.GetParameters())
            .Returns(new Collection <HttpParameterDescriptor>(new HttpParameterDescriptor[0]));
            HttpActionExecutedContext actionExecutedContext = ContextUtil.GetActionExecutedContext(
                request,
                response
                );
            TestTraceWriter traceWriter           = new TestTraceWriter();
            ExceptionFilterAttributeTracer tracer = new ExceptionFilterAttributeTracer(
                mockAttr.Object,
                traceWriter
                );

            TraceRecord[] expectedTraces = new TraceRecord[]
            {
                new TraceRecord(request, TraceCategories.FiltersCategory, TraceLevel.Info)
                {
                    Kind      = TraceKind.Begin,
                    Operation = "OnExceptionAsync"
                },
                new TraceRecord(request, TraceCategories.FiltersCategory, TraceLevel.Info)
                {
                    Kind      = TraceKind.End,
                    Operation = "OnExceptionAsync"
                },
            };

            // Act
            var filter = (IExceptionFilter)tracer;
            await filter.ExecuteExceptionFilterAsync(actionExecutedContext, CancellationToken.None);

            // Assert
            Assert.Equal <TraceRecord>(
                expectedTraces,
                traceWriter.Traces,
                new TraceRecordComparer()
                );
        }
        public void ExecuteExceptionFilterAsync_Throws_And_Traces_When_Inner_OnException_Throws()
        {
            // Arrange
            HttpRequestMessage              request  = new HttpRequestMessage();
            HttpResponseMessage             response = new HttpResponseMessage();
            Mock <ExceptionFilterAttribute> mockAttr = new Mock <ExceptionFilterAttribute>()
            {
                CallBase = true
            };
            InvalidOperationException exception = new InvalidOperationException("test");

            mockAttr.Setup(a => a.OnException(It.IsAny <HttpActionExecutedContext>())).Throws(exception);
            Mock <HttpActionDescriptor> mockActionDescriptor = new Mock <HttpActionDescriptor>()
            {
                CallBase = true
            };

            mockActionDescriptor.Setup(a => a.ActionName).Returns("test");
            mockActionDescriptor.Setup(a => a.GetParameters()).Returns(new Collection <HttpParameterDescriptor>(new HttpParameterDescriptor[0]));
            HttpActionExecutedContext      actionExecutedContext = ContextUtil.GetActionExecutedContext(request, response);
            TestTraceWriter                traceWriter           = new TestTraceWriter();
            ExceptionFilterAttributeTracer tracer = new ExceptionFilterAttributeTracer(mockAttr.Object, traceWriter);

            TraceRecord[] expectedTraces = new TraceRecord[]
            {
                new TraceRecord(request, TraceCategories.FiltersCategory, TraceLevel.Info)
                {
                    Kind = TraceKind.Begin, Operation = "OnException"
                },
                new TraceRecord(request, TraceCategories.FiltersCategory, TraceLevel.Error)
                {
                    Kind = TraceKind.End, Operation = "OnException"
                }
            };

            // Act
            Exception thrown =
                Assert.Throws <InvalidOperationException>(
                    () => ((IExceptionFilter)tracer).ExecuteExceptionFilterAsync(actionExecutedContext, CancellationToken.None));

            // Assert
            Assert.Same(exception, thrown);
            Assert.Same(exception, traceWriter.Traces[1].Exception);
            Assert.Equal <TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
        }
        public void ExecuteExceptionFilterAsync_Faults_And_Traces_When_Inner_Faults()
        {
            // Arrange
            HttpRequestMessage      request    = new HttpRequestMessage();
            HttpResponseMessage     response   = new HttpResponseMessage();
            Mock <IExceptionFilter> mockFilter = new Mock <IExceptionFilter>()
            {
                CallBase = true
            };
            InvalidOperationException     exception = new InvalidOperationException("test");
            TaskCompletionSource <object> tcs       = new TaskCompletionSource <object>(null);

            tcs.TrySetException(exception);
            mockFilter.Setup(a => a.ExecuteExceptionFilterAsync(It.IsAny <HttpActionExecutedContext>(), It.IsAny <CancellationToken>())).Returns(tcs.Task);
            HttpActionExecutedContext actionExecutedContext = ContextUtil.GetActionExecutedContext(request, response);
            TestTraceWriter           traceWriter           = new TestTraceWriter();
            ExceptionFilterTracer     tracer = new ExceptionFilterTracer(mockFilter.Object, traceWriter);

            TraceRecord[] expectedTraces = new TraceRecord[]
            {
                new TraceRecord(request, TraceCategories.FiltersCategory, TraceLevel.Info)
                {
                    Kind = TraceKind.Begin, Operation = "ExecuteExceptionFilterAsync"
                },
                new TraceRecord(request, TraceCategories.FiltersCategory, TraceLevel.Error)
                {
                    Kind = TraceKind.End, Operation = "ExecuteExceptionFilterAsync"
                }
            };

            // Act
            Task task = ((IExceptionFilter)tracer).ExecuteExceptionFilterAsync(actionExecutedContext, CancellationToken.None);


            // Assert
            Exception thrown = Assert.Throws <InvalidOperationException>(() => task.Wait());

            Assert.Same(exception, thrown);
            Assert.Same(exception, traceWriter.Traces[1].Exception);
            Assert.Equal <TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
        }