public void BindValuesAsync_Invokes_Inner_And_Traces()
        {
            // Arrange
            bool wasInvoked = false;
            Mock<HttpActionBinding> mockBinder = new Mock<HttpActionBinding>() { CallBase = true };
            mockBinder.Setup(b => b.ExecuteBindingAsync(
                It.IsAny<HttpActionContext>(),
                It.IsAny<CancellationToken>())).
                    Callback(() => wasInvoked = true).Returns(TaskHelpers.Completed());

            TestTraceWriter traceWriter = new TestTraceWriter();
            HttpActionBindingTracer tracer = new HttpActionBindingTracer(mockBinder.Object, traceWriter);

            TraceRecord[] expectedTraces = new TraceRecord[]
            {
                new TraceRecord(_actionContext.Request, TraceCategories.ModelBindingCategory, TraceLevel.Info) { Kind = TraceKind.Begin },
                new TraceRecord(_actionContext.Request, TraceCategories.ModelBindingCategory, TraceLevel.Info) { Kind = TraceKind.End }
            };

            // Act
            tracer.ExecuteBindingAsync(_actionContext, CancellationToken.None).Wait();

            // Assert
            Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
            Assert.True(wasInvoked);
        }
        public void GetBinding_DoesNotWrapHttpActionBindingTracer()
        {
            // Arrange
            Mock <HttpActionDescriptor> mockActionDescriptor = new Mock <HttpActionDescriptor>()
            {
                CallBase = true
            };
            Mock <HttpParameterDescriptor> mockParameterDescriptor = new Mock <HttpParameterDescriptor>()
            {
                CallBase = true
            };
            Mock <FormatterParameterBinding> mockParameterBinding = new Mock <FormatterParameterBinding>(mockParameterDescriptor.Object, new MediaTypeFormatterCollection(), null)
            {
                CallBase = true
            };
            HttpActionBinding actionBinding = new HttpActionBinding(mockActionDescriptor.Object, new HttpParameterBinding[] { mockParameterBinding.Object });

            ITraceWriter              traceWriter         = new TestTraceWriter();
            HttpActionBindingTracer   actionBindingTracer = new HttpActionBindingTracer(actionBinding, traceWriter);
            Mock <IActionValueBinder> mockBinder          = new Mock <IActionValueBinder>()
            {
                CallBase = true
            };

            mockBinder.Setup(b => b.GetBinding(It.IsAny <HttpActionDescriptor>())).Returns(actionBindingTracer);
            ActionValueBinderTracer tracer = new ActionValueBinderTracer(mockBinder.Object, traceWriter);

            // Act
            HttpActionBinding actualBinding = ((IActionValueBinder)tracer).GetBinding(mockActionDescriptor.Object);

            // Assert
            Assert.Same(actionBindingTracer, actualBinding);
        }
        public void ExecuteBindingAsync_Faults_And_Traces_When_Inner_Faults()
        {
            // Arrange
            InvalidOperationException exception = new InvalidOperationException();
            TaskCompletionSource<object> tcs = new TaskCompletionSource<object>();
            tcs.TrySetException(exception);
            Mock<HttpActionBinding> mockBinder = new Mock<HttpActionBinding>() { CallBase = true };
            mockBinder.Setup(b => b.ExecuteBindingAsync(
                 It.IsAny<HttpActionContext>(),
                 It.IsAny<CancellationToken>())).
                    Returns(tcs.Task);

            TestTraceWriter traceWriter = new TestTraceWriter();
            HttpActionBindingTracer tracer = new HttpActionBindingTracer(mockBinder.Object, traceWriter);

            TraceRecord[] expectedTraces = new TraceRecord[]
            {
                new TraceRecord(_actionContext.Request, TraceCategories.ModelBindingCategory, TraceLevel.Info) { Kind = TraceKind.Begin },
                new TraceRecord(_actionContext.Request, TraceCategories.ModelBindingCategory, TraceLevel.Error) { Kind = TraceKind.End }
            };

            // Act
            Task task = tracer.ExecuteBindingAsync(_actionContext, CancellationToken.None);

            // Assert
            Exception thrown = Assert.Throws<InvalidOperationException>(() => task.Wait());
            Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
            Assert.Same(exception, thrown);
            Assert.Same(exception, traceWriter.Traces[1].Exception);
        }
        public async Task BindValuesAsync_Invokes_Inner_And_Traces()
        {
            // Arrange
            bool wasInvoked = false;
            Mock <HttpActionBinding> mockBinder = new Mock <HttpActionBinding>()
            {
                CallBase = true
            };

            mockBinder.Setup(b => b.ExecuteBindingAsync(It.IsAny <HttpActionContext>(), It.IsAny <CancellationToken>()))
            .Callback(() => wasInvoked = true).Returns(TaskHelpers.Completed());

            TestTraceWriter         traceWriter = new TestTraceWriter();
            HttpActionBindingTracer tracer      = new HttpActionBindingTracer(mockBinder.Object, traceWriter);

            TraceRecord[] expectedTraces = new TraceRecord[]
            {
                new TraceRecord(_actionContext.Request, TraceCategories.ModelBindingCategory, TraceLevel.Info)
                {
                    Kind = TraceKind.Begin
                },
                new TraceRecord(_actionContext.Request, TraceCategories.ModelBindingCategory, TraceLevel.Info)
                {
                    Kind = TraceKind.End
                }
            };

            // Act
            await tracer.ExecuteBindingAsync(_actionContext, CancellationToken.None);

            // Assert
            Assert.Equal <TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
            Assert.True(wasInvoked);
        }
        public void ActionDescriptor_Uses_Inners()
        {
            // Arrange
            HttpActionBinding binding = new Mock<HttpActionBinding>() { CallBase = true }.Object;
            binding.ActionDescriptor = _mockActionDescriptor.Object;
            HttpActionBindingTracer tracer = new HttpActionBindingTracer(binding, new TestTraceWriter());

            // Assert
            Assert.Same(binding.ActionDescriptor, tracer.ActionDescriptor);
        }
        public void ParameterBindings_Uses_Inners()
        {
            // Arrange
            HttpActionBinding binding = new Mock<HttpActionBinding>() { CallBase = true }.Object;
            HttpParameterBinding[] parameterBindings = new HttpParameterBinding[0];
            binding.ParameterBindings = parameterBindings;
            HttpActionBindingTracer tracer = new HttpActionBindingTracer(binding, new TestTraceWriter());

            // Assert
            Assert.Same(parameterBindings, tracer.ParameterBindings);
        }
        public void Decorator_GetInner_On_HttpActionBindingTracer_Returns_HttpActionBinding()
        {
            // Arrange
            HttpActionBinding       expectedInner    = new HttpActionBinding();
            HttpActionBindingTracer productUnderTest = new HttpActionBindingTracer(expectedInner, new TestTraceWriter());

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

            // Assert
            Assert.Same(expectedInner, actualInner);
        }
        public void Inner_Property_On_HttpActionBindingTracer_Returns_HttpActionBinding()
        {
            // Arrange
            HttpActionBinding       expectedInner    = new HttpActionBinding();
            HttpActionBindingTracer productUnderTest = new HttpActionBindingTracer(expectedInner, new TestTraceWriter());

            // Act
            HttpActionBinding actualInner = productUnderTest.Inner;

            // Assert
            Assert.Same(expectedInner, actualInner);
        }
        public void ActionDescriptor_Uses_Inners()
        {
            // Arrange
            HttpActionBinding binding = new Mock <HttpActionBinding>()
            {
                CallBase = true
            }.Object;

            binding.ActionDescriptor = _mockActionDescriptor.Object;
            HttpActionBindingTracer tracer = new HttpActionBindingTracer(binding, new TestTraceWriter());

            // Assert
            Assert.Same(binding.ActionDescriptor, tracer.ActionDescriptor);
        }
        public void ParameterBindings_Uses_Inners()
        {
            // Arrange
            HttpActionBinding binding = new Mock <HttpActionBinding>()
            {
                CallBase = true
            }.Object;

            HttpParameterBinding[] parameterBindings = new HttpParameterBinding[0];
            binding.ParameterBindings = parameterBindings;
            HttpActionBindingTracer tracer = new HttpActionBindingTracer(binding, new TestTraceWriter());

            // Assert
            Assert.Same(parameterBindings, tracer.ParameterBindings);
        }
        public void ExecuteBindingAsync_Faults_And_Traces_When_Inner_Faults()
        {
            // Arrange
            InvalidOperationException     exception = new InvalidOperationException();
            TaskCompletionSource <object> tcs       = new TaskCompletionSource <object>();

            tcs.TrySetException(exception);
            Mock <HttpActionBinding> mockBinder = new Mock <HttpActionBinding>()
            {
                CallBase = true
            };

            mockBinder.Setup(b => b.ExecuteBindingAsync(
                                 It.IsAny <HttpActionContext>(),
                                 It.IsAny <CancellationToken>())).
            Returns(tcs.Task);

            TestTraceWriter         traceWriter = new TestTraceWriter();
            HttpActionBindingTracer tracer      = new HttpActionBindingTracer(mockBinder.Object, traceWriter);

            TraceRecord[] expectedTraces = new TraceRecord[]
            {
                new TraceRecord(_actionContext.Request, TraceCategories.ModelBindingCategory, TraceLevel.Info)
                {
                    Kind = TraceKind.Begin
                },
                new TraceRecord(_actionContext.Request, TraceCategories.ModelBindingCategory, TraceLevel.Error)
                {
                    Kind = TraceKind.End
                }
            };

            // Act
            Task task = tracer.ExecuteBindingAsync(_actionContext, CancellationToken.None);

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

            Assert.Equal <TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
            Assert.Same(exception, thrown);
            Assert.Same(exception, traceWriter.Traces[1].Exception);
        }
        public void GetBinding_DoesNotWrapHttpActionBindingTracer()
        {
            // Arrange
            Mock<HttpActionDescriptor> mockActionDescriptor = new Mock<HttpActionDescriptor>() { CallBase = true };
            Mock<HttpParameterDescriptor> mockParameterDescriptor = new Mock<HttpParameterDescriptor>() { CallBase = true };
            Mock<FormatterParameterBinding> mockParameterBinding = new Mock<FormatterParameterBinding>(mockParameterDescriptor.Object, new MediaTypeFormatterCollection(), null) { CallBase = true };
            HttpActionBinding actionBinding = new HttpActionBinding(mockActionDescriptor.Object, new HttpParameterBinding[] { mockParameterBinding.Object });

            ITraceWriter traceWriter = new TestTraceWriter();
            HttpActionBindingTracer actionBindingTracer = new HttpActionBindingTracer(actionBinding, traceWriter);
            Mock<IActionValueBinder> mockBinder = new Mock<IActionValueBinder>() { CallBase = true };
            mockBinder.Setup(b => b.GetBinding(It.IsAny<HttpActionDescriptor>())).Returns(actionBindingTracer);
            ActionValueBinderTracer tracer = new ActionValueBinderTracer(mockBinder.Object, traceWriter);

            // Act
            HttpActionBinding actualBinding = ((IActionValueBinder)tracer).GetBinding(mockActionDescriptor.Object);

            // Assert
            Assert.Same(actionBindingTracer, actualBinding);
        }
        public void Decorator_GetInner_On_HttpActionBindingTracer_Returns_HttpActionBinding()
        {
            // Arrange
            HttpActionBinding expectedInner = new HttpActionBinding();
            HttpActionBindingTracer productUnderTest = new HttpActionBindingTracer(expectedInner, new TestTraceWriter());

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

            // Assert
            Assert.Same(expectedInner, actualInner);
        }
        public void Inner_Property_On_HttpActionBindingTracer_Returns_HttpActionBinding()
        {
            // Arrange
            HttpActionBinding expectedInner = new HttpActionBinding();
            HttpActionBindingTracer productUnderTest = new HttpActionBindingTracer(expectedInner, new TestTraceWriter());

            // Act
            HttpActionBinding actualInner = productUnderTest.Inner;

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