public void SendAsync_Traces_And_Invokes_Inner() { // Arrange HttpResponseMessage response = new HttpResponseMessage(); MockDelegatingHandler mockHandler = new MockDelegatingHandler((rqst, cancellation) => TaskHelpers.FromResult<HttpResponseMessage>(response)); TestTraceWriter traceWriter = new TestTraceWriter(); MessageHandlerTracer tracer = new MessageHandlerTracer(mockHandler, 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.MessageHandlersCategory, TraceLevel.Info) { Kind = TraceKind.Begin, Operation = "SendAsync" }, new TraceRecord(request, TraceCategories.MessageHandlersCategory, TraceLevel.Info) { Kind = TraceKind.End, Operation = "SendAsync" } }; 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); }
public void SendAsync_Traces_And_Throws_When_Inner_Throws() { // Arrange InvalidOperationException exception = new InvalidOperationException("test"); MockDelegatingHandler mockHandler = new MockDelegatingHandler((rqst, cancellation) => { throw exception; }); TestTraceWriter traceWriter = new TestTraceWriter(); MessageHandlerTracer tracer = new MessageHandlerTracer(mockHandler, 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.MessageHandlersCategory, TraceLevel.Info) { Kind = TraceKind.Begin, Operation = "SendAsync" }, new TraceRecord(request, TraceCategories.MessageHandlersCategory, TraceLevel.Error) { Kind = TraceKind.End, Operation = "SendAsync" } }; 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_Throws_When_Inner_Throws() { // Arrange InvalidOperationException exception = new InvalidOperationException("test"); MockDelegatingHandler mockHandler = new MockDelegatingHandler( (rqst, cancellation) => { throw exception; } ); TestTraceWriter traceWriter = new TestTraceWriter(); MessageHandlerTracer tracer = new MessageHandlerTracer(mockHandler, 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.MessageHandlersCategory, TraceLevel.Info) { Kind = TraceKind.Begin, Operation = "SendAsync" }, new TraceRecord(request, TraceCategories.MessageHandlersCategory, TraceLevel.Error) { Kind = TraceKind.End, Operation = "SendAsync" } }; 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 Decorator_GetInner_On_MessageHandlerTracer_Returns_DelegatingHandler() { // Arrange DelegatingHandler expectedInner = new Mock <DelegatingHandler>().Object; MessageHandlerTracer productUnderTest = new MessageHandlerTracer(expectedInner, new TestTraceWriter()); // Act DelegatingHandler actualInner = Decorator.GetInner(productUnderTest as DelegatingHandler); // Assert Assert.Same(expectedInner, actualInner); }
public void Inner_Property_On_MessageHandlerTracer_Returns_DelegatingHandler() { // Arrange DelegatingHandler expectedInner = new Mock <DelegatingHandler>().Object; MessageHandlerTracer productUnderTest = new MessageHandlerTracer(expectedInner, new TestTraceWriter()); // Act DelegatingHandler actualInner = productUnderTest.Inner; // Assert Assert.Same(expectedInner, actualInner); }
public async Task SendAsync_Traces_And_Invokes_Inner() { // Arrange HttpResponseMessage response = new HttpResponseMessage(); MockDelegatingHandler mockHandler = new MockDelegatingHandler( (rqst, cancellation) => Task.FromResult <HttpResponseMessage>(response) ); TestTraceWriter traceWriter = new TestTraceWriter(); MessageHandlerTracer tracer = new MessageHandlerTracer(mockHandler, 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.MessageHandlersCategory, TraceLevel.Info) { Kind = TraceKind.Begin, Operation = "SendAsync" }, new TraceRecord(request, TraceCategories.MessageHandlersCategory, TraceLevel.Info) { Kind = TraceKind.End, Operation = "SendAsync" } }; 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); }
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); MockDelegatingHandler mockHandler = new MockDelegatingHandler((rqst, cancellation) => { return(tcs.Task); }); TestTraceWriter traceWriter = new TestTraceWriter(); MessageHandlerTracer tracer = new MessageHandlerTracer(mockHandler, 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.MessageHandlersCategory, TraceLevel.Info) { Kind = TraceKind.Begin, Operation = "SendAsync" }, new TraceRecord(request, TraceCategories.MessageHandlersCategory, TraceLevel.Error) { Kind = TraceKind.End, Operation = "SendAsync" } }; 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); }
public void Decorator_GetInner_On_MessageHandlerTracer_Returns_DelegatingHandler() { // Arrange DelegatingHandler expectedInner = new Mock<DelegatingHandler>().Object; MessageHandlerTracer productUnderTest = new MessageHandlerTracer(expectedInner, new TestTraceWriter()); // Act DelegatingHandler actualInner = Decorator.GetInner(productUnderTest as DelegatingHandler); // Assert Assert.Same(expectedInner, actualInner); }
public void Inner_Property_On_MessageHandlerTracer_Returns_DelegatingHandler() { // Arrange DelegatingHandler expectedInner = new Mock<DelegatingHandler>().Object; MessageHandlerTracer productUnderTest = new MessageHandlerTracer(expectedInner, new TestTraceWriter()); // Act DelegatingHandler actualInner = productUnderTest.Inner; // Assert Assert.Same(expectedInner, actualInner); }
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)); }
private static void CreateMessageHandlerTracers(HttpConfiguration configuration, ITraceWriter traceWriter) { // Insert a tracing handler before each existing message handler (in execution order) int handlerCount = configuration.MessageHandlers.Count; for (int i = 0; i < handlerCount * 2; i += 2) { DelegatingHandler innerHandler = configuration.MessageHandlers[i]; DelegatingHandler handlerTracer = new MessageHandlerTracer(innerHandler, traceWriter); configuration.MessageHandlers.Insert(i + 1, handlerTracer); } configuration.MessageHandlers.Add(new RequestMessageHandlerTracer(traceWriter)); }