public void SendAsync_Traces_And_Throws_When_Inner_Throws()
        {
            // Arrange
            InvalidOperationException exception = new InvalidOperationException("test");
            TestTraceWriter traceWriter = new TestTraceWriter();
            RequestMessageHandlerTracer tracer = new RequestMessageHandlerTracer(traceWriter);

            // DelegatingHandlers require an InnerHandler to run.  We create a mock one to simulate what
            // would happen when a DelegatingHandler executing after the tracer throws.
            MockHttpMessageHandler mockInnerHandler = new MockHttpMessageHandler((rqst, cancellation) => { throw exception; });
            tracer.InnerHandler = mockInnerHandler;

            HttpRequestMessage request = new HttpRequestMessage();
            TraceRecord[] expectedTraces = new TraceRecord[]
            {
                new TraceRecord(request, TraceCategories.RequestCategory, TraceLevel.Info) { Kind = TraceKind.Begin },
                new TraceRecord(request, TraceCategories.RequestCategory, TraceLevel.Error) { Kind = TraceKind.End }
            };

            MethodInfo method = typeof(DelegatingHandler).GetMethod("SendAsync",
                                                                     BindingFlags.Public | BindingFlags.NonPublic |
                                                                     BindingFlags.Instance);

            // Act
            Exception thrown =
                Assert.Throws<TargetInvocationException>(
                    () => method.Invoke(tracer, new object[] { request, CancellationToken.None }));

            // Assert
            Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
            Assert.Same(exception, thrown.InnerException);
            Assert.Same(exception, traceWriter.Traces[1].Exception);
        }
        public void SendAsync_Traces_And_Invokes_Inner()
        {
            // Arrange
            HttpResponseMessage response = new HttpResponseMessage();
            TestTraceWriter traceWriter = new TestTraceWriter();
            RequestMessageHandlerTracer tracer = new RequestMessageHandlerTracer(traceWriter);
            MockHttpMessageHandler mockInnerHandler = new MockHttpMessageHandler((rqst, cancellation) =>
                                     TaskHelpers.FromResult<HttpResponseMessage>(response));
            tracer.InnerHandler = mockInnerHandler;

            HttpRequestMessage request = new HttpRequestMessage();
            TraceRecord[] expectedTraces = new TraceRecord[]
            {
                new TraceRecord(request, TraceCategories.RequestCategory, TraceLevel.Info) { Kind = TraceKind.Begin },
                new TraceRecord(request, TraceCategories.RequestCategory, TraceLevel.Info) { Kind = TraceKind.End }
            };

            MethodInfo method = typeof(DelegatingHandler).GetMethod("SendAsync",
                                                                     BindingFlags.Public | BindingFlags.NonPublic |
                                                                     BindingFlags.Instance);

            // Act
            Task<HttpResponseMessage> task = method.Invoke(tracer, new object[] { request, CancellationToken.None }) as Task<HttpResponseMessage>;
            HttpResponseMessage actualResponse = task.Result;

            // Assert
            Assert.Equal<TraceRecord>(expectedTraces, traceWriter.Traces, new TraceRecordComparer());
            Assert.Same(response, actualResponse);
        }
Esempio n. 3
0
        public async Task SendAsync_Traces_And_Faults_When_Inner_Faults()
        {
            // Arrange
            InvalidOperationException exception            = new InvalidOperationException("test");
            TaskCompletionSource <HttpResponseMessage> tcs =
                new TaskCompletionSource <HttpResponseMessage>();

            tcs.TrySetException(exception);
            TestTraceWriter             traceWriter = new TestTraceWriter();
            RequestMessageHandlerTracer tracer      = new RequestMessageHandlerTracer(traceWriter);

            // DelegatingHandlers require an InnerHandler to run.  We create a mock one to simulate what
            // would happen when a DelegatingHandler executing after the tracer returns a Task that throws.
            MockHttpMessageHandler mockInnerHandler = new MockHttpMessageHandler(
                (rqst, cancellation) =>
            {
                return(tcs.Task);
            }
                );

            tracer.InnerHandler = mockInnerHandler;

            HttpRequestMessage request = new HttpRequestMessage();

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

            MethodInfo method = typeof(DelegatingHandler).GetMethod(
                "SendAsync",
                BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
                );

            // Act
            Task <HttpResponseMessage> task =
                method.Invoke(tracer, new object[] { request, CancellationToken.None })
                as Task <HttpResponseMessage>;

            // Assert
            Exception thrown = await Assert.ThrowsAsync <InvalidOperationException>(() => task);

            Assert.Equal <TraceRecord>(
                expectedTraces,
                traceWriter.Traces,
                new TraceRecordComparer()
                );
            Assert.Same(exception, thrown);
            Assert.Same(exception, traceWriter.Traces[1].Exception);
        }
Esempio n. 4
0
        public void SendAsync_Traces_And_Throws_When_Inner_Throws()
        {
            // Arrange
            InvalidOperationException   exception   = new InvalidOperationException("test");
            TestTraceWriter             traceWriter = new TestTraceWriter();
            RequestMessageHandlerTracer tracer      = new RequestMessageHandlerTracer(traceWriter);

            // DelegatingHandlers require an InnerHandler to run.  We create a mock one to simulate what
            // would happen when a DelegatingHandler executing after the tracer throws.
            MockHttpMessageHandler mockInnerHandler = new MockHttpMessageHandler(
                (rqst, cancellation) =>
            {
                throw exception;
            }
                );

            tracer.InnerHandler = mockInnerHandler;

            HttpRequestMessage request = new HttpRequestMessage();

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

            MethodInfo method = typeof(DelegatingHandler).GetMethod(
                "SendAsync",
                BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
                );

            // Act
            Exception thrown = Assert.Throws <TargetInvocationException>(
                () => method.Invoke(tracer, new object[] { request, CancellationToken.None })
                );

            // Assert
            Assert.Equal <TraceRecord>(
                expectedTraces,
                traceWriter.Traces,
                new TraceRecordComparer()
                );
            Assert.Same(exception, thrown.InnerException);
            Assert.Same(exception, traceWriter.Traces[1].Exception);
        }
Esempio n. 5
0
        public async Task SendAsync_Traces_And_Invokes_Inner()
        {
            // Arrange
            HttpResponseMessage         response         = new HttpResponseMessage();
            TestTraceWriter             traceWriter      = new TestTraceWriter();
            RequestMessageHandlerTracer tracer           = new RequestMessageHandlerTracer(traceWriter);
            MockHttpMessageHandler      mockInnerHandler = new MockHttpMessageHandler(
                (rqst, cancellation) => Task.FromResult <HttpResponseMessage>(response)
                );

            tracer.InnerHandler = mockInnerHandler;

            HttpRequestMessage request = new HttpRequestMessage();

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

            MethodInfo method = typeof(DelegatingHandler).GetMethod(
                "SendAsync",
                BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance
                );

            // Act
            Task <HttpResponseMessage> task =
                method.Invoke(tracer, new object[] { request, CancellationToken.None })
                as Task <HttpResponseMessage>;
            HttpResponseMessage actualResponse = await task;

            // Assert
            Assert.Equal <TraceRecord>(
                expectedTraces,
                traceWriter.Traces,
                new TraceRecordComparer()
                );
            Assert.Same(response, actualResponse);
        }
Esempio n. 6
0
        public void Initialize_Repairs_Handler_Collection_If_RequestTracer_Inserted()
        {
            // Arrange
            HttpConfiguration config = new HttpConfiguration();
            config.Services.Replace(typeof(ITraceWriter), new TestTraceWriter());
            TestTraceWriter traceWriter = new TestTraceWriter();
            DelegatingHandler messageHandler = new Mock<DelegatingHandler>().Object;
            DelegatingHandler msgHandlerTracer = new MessageHandlerTracer(messageHandler, traceWriter);
            RequestMessageHandlerTracer requestMsgtracer = new RequestMessageHandlerTracer(traceWriter);
            List<DelegatingHandler> handlerList = new List<DelegatingHandler>() { messageHandler, msgHandlerTracer, requestMsgtracer };
            foreach (var eachHandler in handlerList)
            {
                config.MessageHandlers.Add(eachHandler);
            }
            TraceManager traceManager = new TraceManager();
            traceManager.Initialize(config);
            Collection<DelegatingHandler> expectedMessageHandlers = config.MessageHandlers;

            // Act
            config.MessageHandlers.Insert(0, requestMsgtracer);
            traceManager.Initialize(config);

            // Assert
            Collection<DelegatingHandler> actualMessageHandlers = config.MessageHandlers;
            Assert.Equal(expectedMessageHandlers, actualMessageHandlers);
            Assert.True(IsMessageHandlerCollectionValid(config.MessageHandlers));
        }