예제 #1
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));
        }
예제 #2
0
        public void HttpServer_CallsCustomMessageHandlers_InTheCorrectOrder()
        {
            // Arrange
            List<int> order = new List<int>();
            order.Add(1);
            order.Add(2);
            order.Add(3);
            order.Add(4);
            List<int>.Enumerator e = order.GetEnumerator();
            OrderAwareMessageHandler messageHandler1 = new OrderAwareMessageHandler(e, 1);
            OrderAwareMessageHandler messageHandler2 = new OrderAwareMessageHandler(e, 2);
            OrderAwareMessageHandler messageHandler3 = new OrderAwareMessageHandler(e, 3);
            OrderAwareMessageHandler messageHandler4 = new OrderAwareMessageHandler(e, 4);
            HttpRequestMessage request = new HttpRequestMessage();
            TraceManager traceManager = new TraceManager();
            HttpConfiguration config = new HttpConfiguration();
            config.Services.Replace(typeof(ITraceWriter), new TestTraceWriter());
            config.MessageHandlers.Add(messageHandler1);
            config.MessageHandlers.Add(messageHandler2);
            config.MessageHandlers.Add(messageHandler3);
            config.MessageHandlers.Add(messageHandler4);
            Mock<HttpControllerDispatcher> dispatcherMock = new Mock<HttpControllerDispatcher>(config);
            dispatcherMock.Protected().Setup<Task<HttpResponseMessage>>("SendAsync", request, CancellationToken.None)
                .Returns(TaskHelpers.FromResult<HttpResponseMessage>(request.CreateResponse()));
            HttpServer server = new HttpServer(config, dispatcherMock.Object);
            HttpMessageInvoker invoker = new HttpMessageInvoker(server);

            // Act
            Task<HttpResponseMessage> response = invoker.SendAsync(request, CancellationToken.None);

            // Assert
            Assert.True(IsMessageHandlerCollectionValid(config.MessageHandlers));
        }
예제 #3
0
        public void Multiple_Initialize_DoesNotAlter_MessageHandlerCollection(List<DelegatingHandler> handlerList)
        {
            // Arrange
            HttpConfiguration config = new HttpConfiguration();
            config.Services.Replace(typeof(ITraceWriter), new TestTraceWriter());
            foreach (var eachHandler in handlerList)
            {
                config.MessageHandlers.Add(eachHandler);
            }
            TraceManager traceManager = new TraceManager();
            traceManager.Initialize(config);
            Collection<DelegatingHandler> expectedMessageHandlers = config.MessageHandlers;

            // Act
            traceManager.Initialize(config);

            // Assert
            Collection<DelegatingHandler> actualMessageHandlers = config.MessageHandlers;
            Assert.Equal(expectedMessageHandlers, actualMessageHandlers);
            Assert.True(IsMessageHandlerCollectionValid(config.MessageHandlers));
        }
예제 #4
0
        public void TraceManager_Initialize_After_HttpServer_Initialize_DoesNotAlter_MessageHandlerCollection()
        {
            // Arrange
            HttpConfiguration config = new HttpConfiguration();
            config.Services.Replace(typeof(ITraceWriter), new TestTraceWriter());
            DelegatingHandler messageHandler = new Mock<DelegatingHandler>().Object;
            config.MessageHandlers.Add(messageHandler);
            TraceManager traceManager = new TraceManager();
            Collection<DelegatingHandler> expectedMessageHandlers = config.MessageHandlers;
            HttpRequestMessage request = new HttpRequestMessage();
            Mock<HttpControllerDispatcher> dispatcherMock = new Mock<HttpControllerDispatcher>(config);
            dispatcherMock.Protected().Setup<Task<HttpResponseMessage>>("SendAsync", request, CancellationToken.None)
                .Returns(TaskHelpers.FromResult<HttpResponseMessage>(request.CreateResponse()));

            HttpServer server = new HttpServer(config, dispatcherMock.Object);
            HttpMessageInvoker invoker = new HttpMessageInvoker(server);

            // Act
            Task<HttpResponseMessage> response = invoker.SendAsync(request, CancellationToken.None);
            traceManager.Initialize(config);

            // Assert
            Collection<DelegatingHandler> actualMessageHandlers = config.MessageHandlers;
            Assert.Equal(expectedMessageHandlers, actualMessageHandlers);
            Assert.True(IsMessageHandlerCollectionValid(config.MessageHandlers));
        }
예제 #5
0
        public void Multiple_Initialize_DoesNotAlter_Num_Of_MessageHandlers_With_No_TraceWriter()
        {
            // Arrange
            HttpConfiguration config = new HttpConfiguration();
            TraceManager traceManager = new TraceManager();
            traceManager.Initialize(config);
            int expectedHandlerCount = config.MessageHandlers.Count;

            // Act
            traceManager.Initialize(config);

            // Assert
            int actualHandlerCount = config.MessageHandlers.Count;
            Assert.Equal(expectedHandlerCount, actualHandlerCount);
        }