public void SelectAction_DoesNotWrapHttpActionDescriptorTracer()
        {
            // Arrange
            TestTraceWriter traceWriter = new TestTraceWriter();
            Mock<IHttpActionSelector> mockSelector = new Mock<IHttpActionSelector>();

            HttpActionDescriptorTracer actionDescriptorTracer = new HttpActionDescriptorTracer(_controllerContext, _mockActionDescriptor.Object, traceWriter);
            mockSelector.Setup(s => s.SelectAction(_controllerContext)).Returns(actionDescriptorTracer);
            HttpActionSelectorTracer tracer = new HttpActionSelectorTracer(mockSelector.Object, traceWriter);

            // Act
            HttpActionDescriptor selectedActionDescriptor = ((IHttpActionSelector)tracer).SelectAction(_controllerContext);

            // Assert
            Assert.Same(actionDescriptorTracer, selectedActionDescriptor);
        }
        public void ActionName_Calls_Inner()
        {
            // Arrange
            HttpControllerDescriptor controllerDescriptor = new Mock<HttpControllerDescriptor>().Object;
            controllerDescriptor.Configuration = new HttpConfiguration();
            Mock<HttpActionDescriptor> mockDescriptor = new Mock<HttpActionDescriptor>(controllerDescriptor);
            mockDescriptor.Setup(d => d.ActionName).Returns("actionName").Verifiable();
            HttpActionDescriptor actionDescriptor = mockDescriptor.Object;
            HttpControllerContext controllerContext = new Mock<HttpControllerContext>().Object;
            controllerContext.Configuration = controllerDescriptor.Configuration;
            controllerContext.ControllerDescriptor = controllerDescriptor;
            HttpActionDescriptorTracer tracer = new HttpActionDescriptorTracer(controllerContext, actionDescriptor, new TestTraceWriter());

            // Act and Assert
            Assert.Same("actionName", tracer.ActionName);
            mockDescriptor.Verify();
        }
        public void SupportedHttpMethods_Calls_Inner()
        {
            // Arrange
            HttpControllerDescriptor controllerDescriptor = new Mock<HttpControllerDescriptor>().Object;
            controllerDescriptor.Configuration = new HttpConfiguration();
            Collection<HttpMethod> methods = new Collection<HttpMethod>() { HttpMethod.Delete };
            Mock<HttpActionDescriptor> mockDescriptor = new Mock<HttpActionDescriptor>(controllerDescriptor);
            mockDescriptor.Setup(d => d.SupportedHttpMethods).Returns(methods).Verifiable();
            HttpActionDescriptor actionDescriptor = mockDescriptor.Object;
            HttpControllerContext controllerContext = new Mock<HttpControllerContext>().Object;
            controllerContext.Configuration = controllerDescriptor.Configuration;
            controllerContext.ControllerDescriptor = controllerDescriptor;
            HttpActionDescriptorTracer tracer = new HttpActionDescriptorTracer(controllerContext, actionDescriptor, new TestTraceWriter());

            // Act and Assert
            Assert.Equal(methods, tracer.SupportedHttpMethods);
            mockDescriptor.Verify();
        }
        public void GetFilters_Returns_Wrapped_IFilters()
        {
            // Arrange
            Mock<IFilter> mockFilter = new Mock<IFilter>();
            Collection<IFilter> filters = new Collection<IFilter>(new IFilter[] { mockFilter.Object });
            Mock<HttpActionDescriptor> mockActionDescriptor = new Mock<HttpActionDescriptor>() { CallBase = true };
            mockActionDescriptor.Setup(a => a.ActionName).Returns("test");
            mockActionDescriptor.Setup(a => a.GetFilters()).Returns(filters);
            HttpControllerContext controllerContext = ContextUtil.CreateControllerContext();
            controllerContext.ControllerDescriptor = new HttpControllerDescriptor(controllerContext.Configuration, "test", typeof(ApiController));
            HttpActionDescriptorTracer tracer = new HttpActionDescriptorTracer(controllerContext, mockActionDescriptor.Object, new TestTraceWriter());

            // Act
            IFilter[] wrappedFilters = tracer.GetFilters().ToArray();

            // Assert
            Assert.IsType<FilterTracer>(wrappedFilters[0]);
        }
        public void ExecuteAsync_Invokes_Inner_ExecuteAsync()
        {
            // Arrange
            bool executeCalled = false;
            Mock<HttpActionDescriptor> mockActionDescriptor = new Mock<HttpActionDescriptor>() { CallBase = true };
            mockActionDescriptor.Setup(a => a.ActionName).Returns("test");
            mockActionDescriptor.Setup(
                a => a.ExecuteAsync(It.IsAny<HttpControllerContext>(), It.IsAny<IDictionary<string, object>>())).Callback(() => executeCalled = true);
            HttpControllerContext controllerContext = ContextUtil.CreateControllerContext();
            controllerContext.ControllerDescriptor = new HttpControllerDescriptor(controllerContext.Configuration, "test", typeof(ApiController));
            IDictionary<string, object> arguments = new Dictionary<string, object>();
            HttpActionDescriptorTracer tracer = new HttpActionDescriptorTracer(controllerContext, mockActionDescriptor.Object, new TestTraceWriter());

            // Act
            tracer.ExecuteAsync(controllerContext, arguments);

            // Assert
            Assert.True(executeCalled);
        }
        public void GetFilterPipeline_Returns_Wrapped_Filters()
        {
            // Arrange
            Mock<IFilter> mockFilter = new Mock<IFilter>();
            FilterInfo filter = new FilterInfo(mockFilter.Object, FilterScope.Global);
            Collection<FilterInfo> filterCollection = new Collection<FilterInfo>(new FilterInfo[] { filter });
            Mock<HttpActionDescriptor> mockActionDescriptor = new Mock<HttpActionDescriptor>() { CallBase = true };
            mockActionDescriptor.Setup(a => a.ActionName).Returns("test");
            mockActionDescriptor.Setup(a => a.GetFilterPipeline()).Returns(filterCollection);
            HttpControllerContext controllerContext = ContextUtil.CreateControllerContext();
            controllerContext.ControllerDescriptor = new HttpControllerDescriptor(controllerContext.Configuration, "test", typeof(ApiController));
            HttpActionDescriptorTracer tracer = new HttpActionDescriptorTracer(controllerContext, mockActionDescriptor.Object, new TestTraceWriter());

            // Act
            Collection<FilterInfo> wrappedFilterCollection = tracer.GetFilterPipeline();

            // Assert
            Assert.IsType<FilterTracer>(wrappedFilterCollection[0].Instance);
        }
        public void ExecuteAsync_Throws_What_Inner_Throws_And_Traces()
        {
            // Arrange
            Mock<HttpActionDescriptor> mockActionDescriptor = new Mock<HttpActionDescriptor>() { CallBase = true };
            InvalidOperationException exception = new InvalidOperationException("test");
            mockActionDescriptor.Setup(
                a => a.ExecuteAsync(It.IsAny<HttpControllerContext>(), It.IsAny<IDictionary<string, object>>(), CancellationToken.None)).Throws(exception);
            mockActionDescriptor.Setup(a => a.ActionName).Returns("test");
            HttpControllerContext controllerContext = ContextUtil.CreateControllerContext();
            controllerContext.ControllerDescriptor = new HttpControllerDescriptor(controllerContext.Configuration, "test", typeof(ApiController));
            IDictionary<string, object> arguments = new Dictionary<string, object>();
            TestTraceWriter traceWriter = new TestTraceWriter();
            HttpActionDescriptorTracer tracer = new HttpActionDescriptorTracer(controllerContext, mockActionDescriptor.Object, traceWriter);
            TraceRecord[] expectedTraces = new TraceRecord[]
            {
                new TraceRecord(controllerContext.Request, TraceCategories.ActionCategory, TraceLevel.Info) { Kind = TraceKind.Begin },
                new TraceRecord(controllerContext.Request, TraceCategories.ActionCategory, TraceLevel.Error) { Kind = TraceKind.End }
            };

            // Act & Assert
            Assert.Throws<InvalidOperationException>(() => tracer.ExecuteAsync(controllerContext, arguments, CancellationToken.None));

            // Assert
            Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
            Assert.Same(exception, traceWriter.Traces[1].Exception);
        }
        public void ExecuteAsync_Traces()
        {
            // Arrange
            Mock<HttpActionDescriptor> mockActionDescriptor = new Mock<HttpActionDescriptor>() { CallBase = true };
            mockActionDescriptor.Setup(a => a.ActionName).Returns("test");
            mockActionDescriptor.Setup(a => a.ExecuteAsync(It.IsAny<HttpControllerContext>(), It.IsAny<IDictionary<string, object>>(), CancellationToken.None))
                .Returns(TaskHelpers.FromResult<object>(null));
            HttpControllerContext controllerContext = ContextUtil.CreateControllerContext();
            controllerContext.ControllerDescriptor = new HttpControllerDescriptor(controllerContext.Configuration, "test", typeof(ApiController));
            IDictionary<string, object> arguments = new Dictionary<string, object>();
            TestTraceWriter traceWriter = new TestTraceWriter();
            HttpActionDescriptorTracer tracer = new HttpActionDescriptorTracer(controllerContext, mockActionDescriptor.Object, traceWriter);
            TraceRecord[] expectedTraces = new TraceRecord[]
            {
                new TraceRecord(controllerContext.Request, TraceCategories.ActionCategory, TraceLevel.Info) { Kind = TraceKind.Begin, Operation = "ExecuteAsync" },
                new TraceRecord(controllerContext.Request, TraceCategories.ActionCategory, TraceLevel.Info) { Kind = TraceKind.End }
            };

            // Act
            var result = tracer.ExecuteAsync(controllerContext, arguments, CancellationToken.None);

            // Assert
            result.WaitUntilCompleted();
            Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
        }
        public void ExecuteAsync_Invokes_Inner_ExecuteAsync()
        {
            // Arrange
            var cts = new CancellationTokenSource();
            var mockActionDescriptor = new Mock<HttpActionDescriptor>() { CallBase = true };
            mockActionDescriptor.Setup(a => a.ActionName).Returns("test");
            var controllerContext = ContextUtil.CreateControllerContext();
            controllerContext.ControllerDescriptor = new HttpControllerDescriptor(controllerContext.Configuration, "test", typeof(ApiController));
            var arguments = new Dictionary<string, object>();
            var tracer = new HttpActionDescriptorTracer(controllerContext, mockActionDescriptor.Object, new TestTraceWriter());

            // Act
            tracer.ExecuteAsync(controllerContext, arguments, cts.Token);

            // Assert
            mockActionDescriptor.Verify(a => a.ExecuteAsync(controllerContext, arguments, cts.Token), Times.Once());
        }
        public void ControllerDescriptor_Uses_Inners()
        {
            // Assert
            HttpControllerDescriptor controllerDescriptor = new Mock<HttpControllerDescriptor>().Object;
            controllerDescriptor.Configuration = new HttpConfiguration();
            Mock<HttpActionDescriptor> mockDescriptor = new Mock<HttpActionDescriptor>(controllerDescriptor);
            HttpActionDescriptor actionDescriptor = mockDescriptor.Object;
            HttpControllerContext controllerContext = new Mock<HttpControllerContext>().Object;
            controllerContext.Configuration = controllerDescriptor.Configuration;
            controllerContext.ControllerDescriptor = controllerDescriptor;
            HttpActionDescriptorTracer tracer = new HttpActionDescriptorTracer(controllerContext, actionDescriptor, new TestTraceWriter());

            // Act and Assert
            Assert.Same(actionDescriptor.ControllerDescriptor, tracer.ControllerDescriptor);
        }
        public void GetFilterPipeline_Calls_Inner()
        {
            // Arrange
            Collection<FilterInfo> filters = new Collection<FilterInfo>();
            HttpControllerDescriptor controllerDescriptor = new Mock<HttpControllerDescriptor>().Object;
            controllerDescriptor.Configuration = new HttpConfiguration();
            Mock<HttpActionDescriptor> mockDescriptor = new Mock<HttpActionDescriptor>(controllerDescriptor);
            mockDescriptor.Setup(d => d.GetFilterPipeline()).Returns(filters).Verifiable();
            HttpActionDescriptor actionDescriptor = mockDescriptor.Object;
            HttpControllerContext controllerContext = new Mock<HttpControllerContext>().Object;
            controllerContext.Configuration = controllerDescriptor.Configuration;
            controllerContext.ControllerDescriptor = controllerDescriptor;
            HttpActionDescriptorTracer tracer = new HttpActionDescriptorTracer(controllerContext, actionDescriptor, new TestTraceWriter());

            // Act
            tracer.GetFilterPipeline();

            // Assert
            mockDescriptor.Verify();
        }
        public void GetParameters_Calls_Inner()
        {
            // Arrange
            Collection<HttpParameterDescriptor> parameters = new Collection<HttpParameterDescriptor>();
            HttpControllerDescriptor controllerDescriptor = new Mock<HttpControllerDescriptor>().Object;
            controllerDescriptor.Configuration = new HttpConfiguration();
            Mock<HttpActionDescriptor> mockDescriptor = new Mock<HttpActionDescriptor>(controllerDescriptor);
            mockDescriptor.Setup(d => d.GetParameters()).Returns(parameters).Verifiable();
            HttpActionDescriptor actionDescriptor = mockDescriptor.Object;
            HttpControllerContext controllerContext = new Mock<HttpControllerContext>().Object;
            controllerContext.Configuration = controllerDescriptor.Configuration;
            controllerContext.ControllerDescriptor = controllerDescriptor;
            HttpActionDescriptorTracer tracer = new HttpActionDescriptorTracer(controllerContext, actionDescriptor, new TestTraceWriter());

            // Act and Assert
            Assert.Same(parameters, tracer.GetParameters());
            mockDescriptor.Verify();
        }
        public void Properties_Calls_Inner()
        {
            // Arrange
            ConcurrentDictionary<object, object> properties = new ConcurrentDictionary<object, object>();
            HttpControllerDescriptor controllerDescriptor = new Mock<HttpControllerDescriptor>().Object;
            controllerDescriptor.Configuration = new HttpConfiguration();
            Mock<HttpActionDescriptor> mockDescriptor = new Mock<HttpActionDescriptor>(controllerDescriptor);
            mockDescriptor.Setup(d => d.Properties).Returns(properties).Verifiable();
            HttpActionDescriptor actionDescriptor = mockDescriptor.Object;
            HttpControllerContext controllerContext = new Mock<HttpControllerContext>().Object;
            controllerContext.Configuration = controllerDescriptor.Configuration;
            controllerContext.ControllerDescriptor = controllerDescriptor;
            HttpActionDescriptorTracer tracer = new HttpActionDescriptorTracer(controllerContext, actionDescriptor, new TestTraceWriter());

            // Act and Assert
            Assert.Same(properties, tracer.Properties);
            mockDescriptor.Verify();
        }
        public void Decorator_GetInner_On_HttpActionDescriptorTracer_Returns_HttpActionDescriptor()
        {
            // Arrange
            HttpActionDescriptor expectedInner = new Mock<HttpActionDescriptor>().Object;
            HttpControllerContext controllerContext = ContextUtil.CreateControllerContext();
            HttpActionDescriptorTracer productUnderTest = new HttpActionDescriptorTracer(controllerContext, expectedInner, new TestTraceWriter());

            // Act
            HttpActionDescriptor actualInner = Decorator.GetInner(productUnderTest as HttpActionDescriptor);

            // Assert
            Assert.Same(expectedInner, actualInner);
        }