Example #1
0
        public void SendAsync_IfRequestCancelled_DoesNotCallExceptionServices()
        {
            // Arrange
            Exception expectedException = new OperationCanceledException();

            HttpMessageHandler dispatcher = CreateFaultingMessageHandler(expectedException);

            Mock <IExceptionLogger> exceptionLoggerMock = new Mock <IExceptionLogger>(MockBehavior.Strict);
            IExceptionLogger        exceptionLogger     = exceptionLoggerMock.Object;

            Mock <IExceptionHandler> exceptionHandlerMock = new Mock <IExceptionHandler>(MockBehavior.Strict);
            IExceptionHandler        exceptionHandler     = exceptionHandlerMock.Object;

            using (HttpRequestMessage expectedRequest = CreateRequest())
                using (HttpConfiguration configuration = CreateConfiguration())
                    using (HttpServer product = CreateProductUnderTest(configuration, dispatcher, exceptionLogger,
                                                                       exceptionHandler))
                    {
                        CancellationToken cancellationToken = CreateCancellationToken();

                        Task <HttpResponseMessage> task = product.SendAsync(expectedRequest, cancellationToken);

                        // Act
                        task.WaitUntilCompleted();

                        // Assert
                        Assert.Equal(TaskStatus.Canceled, task.Status);

                        // The mock handler and logger will throw if they are called, so this test verifies that
                        // they aren't called by construction.
                    }
        }
        public async Task SendAsync_IfExceptionHandlerSetsNullResult_PropogatesFaultedTaskException()
        {
            // Arrange
            Exception expectedException  = CreateExceptionWithCallStack();
            string    expectedStackTrace = expectedException.StackTrace;

            HttpMessageHandler dispatcher = CreateFaultingMessageHandler(expectedException);

            IExceptionLogger exceptionLogger = CreateStubExceptionLogger();

            Mock <IExceptionHandler> exceptionHandlerMock = new Mock <IExceptionHandler>(MockBehavior.Strict);

            exceptionHandlerMock
            .Setup(h => h.HandleAsync(It.IsAny <ExceptionHandlerContext>(), It.IsAny <CancellationToken>()))
            .Callback <ExceptionHandlerContext, CancellationToken>((c, i) => c.Result = null)
            .Returns(Task.FromResult(0));
            IExceptionHandler exceptionHandler = exceptionHandlerMock.Object;

            using (HttpRequestMessage request = CreateRequest())
                using (HttpConfiguration configuration = CreateConfiguration())
                    using (HttpServer product = CreateProductUnderTest(configuration, dispatcher, exceptionLogger,
                                                                       exceptionHandler))
                    {
                        CancellationToken cancellationToken = CreateCancellationToken();

                        // Act & Assert
                        var exception = await Assert.ThrowsAsync <Exception>(() => product.SendAsync(request, cancellationToken));

                        Assert.Same(expectedException, exception);
                        Assert.NotNull(exception.StackTrace);
                        Assert.StartsWith(expectedStackTrace, exception.StackTrace);
                    }
        }
        public async Task SendAsync_IfExceptionHandlerHandlesException_ReturnsResponse()
        {
            // Arrange
            HttpMessageHandler dispatcher = CreateFaultingMessageHandler(CreateException());

            IExceptionLogger exceptionLogger = CreateStubExceptionLogger();

            using (HttpResponseMessage expectedResponse = CreateResponse())
            {
                Mock <IExceptionHandler> exceptionHandlerMock = new Mock <IExceptionHandler>(MockBehavior.Strict);
                exceptionHandlerMock
                .Setup(h => h.HandleAsync(It.IsAny <ExceptionHandlerContext>(), It.IsAny <CancellationToken>()))
                .Callback <ExceptionHandlerContext, CancellationToken>((c, i) =>
                                                                       c.Result = new ResponseMessageResult(expectedResponse))
                .Returns(Task.FromResult(0));
                IExceptionHandler exceptionHandler = exceptionHandlerMock.Object;

                using (HttpRequestMessage request = CreateRequest())
                    using (HttpConfiguration configuration = new HttpConfiguration())
                        using (HttpServer product = CreateProductUnderTest(configuration, dispatcher, exceptionLogger,
                                                                           exceptionHandler))
                        {
                            CancellationToken cancellationToken = CreateCancellationToken();

                            // Act
                            HttpResponseMessage response = await product.SendAsync(request, cancellationToken);

                            // Assert
                            Assert.Same(expectedResponse, response);
                        }
            }
        }
        public void SendAsync_IfDispatcherTaskIsFaulted_CallsExceptionServices()
        {
            // Arrange
            Exception expectedException = CreateException();

            HttpMessageHandler dispatcher = CreateFaultingMessageHandler(expectedException);

            Mock <IExceptionLogger> exceptionLoggerMock = CreateStubExceptionLoggerMock();
            IExceptionLogger        exceptionLogger     = exceptionLoggerMock.Object;

            Mock <IExceptionHandler> exceptionHandlerMock = CreateStubExceptionHandlerMock();
            IExceptionHandler        exceptionHandler     = exceptionHandlerMock.Object;

            using (HttpRequestMessage expectedRequest = CreateRequest())
                using (HttpConfiguration configuration = CreateConfiguration())
                    using (HttpServer product = CreateProductUnderTest(configuration, dispatcher, exceptionLogger,
                                                                       exceptionHandler))
                    {
                        CancellationToken cancellationToken = CreateCancellationToken();

                        Task <HttpResponseMessage> task = product.SendAsync(expectedRequest, cancellationToken);

                        // Act
                        task.WaitUntilCompleted();

                        // Assert
                        Func <ExceptionContext, bool> exceptionContextMatches = (c) =>
                                                                                c != null &&
                                                                                c.Exception == expectedException &&
                                                                                c.CatchBlock == ExceptionCatchBlocks.HttpServer &&
                                                                                c.Request == expectedRequest;

                        exceptionLoggerMock.Verify(l => l.LogAsync(
                                                       It.Is <ExceptionLoggerContext>(c => c.CanBeHandled == true &&
                                                                                      exceptionContextMatches(c.ExceptionContext)),
                                                       cancellationToken), Times.Once());

                        exceptionHandlerMock.Verify(h => h.HandleAsync(
                                                        It.Is <ExceptionHandlerContext>((c) => exceptionContextMatches(c.ExceptionContext)),
                                                        cancellationToken), Times.Once());
                    }
        }
Example #5
0
        public void SendAsync_IfExceptionHandlerSetsNullResult_PropogatesFaultedTaskException()
        {
            // Arrange
            Exception expectedException  = CreateExceptionWithCallStack();
            string    expectedStackTrace = expectedException.StackTrace;

            HttpMessageHandler dispatcher = CreateFaultingMessageHandler(expectedException);

            IExceptionLogger exceptionLogger = CreateStubExceptionLogger();

            Mock <IExceptionHandler> exceptionHandlerMock = new Mock <IExceptionHandler>(MockBehavior.Strict);

            exceptionHandlerMock
            .Setup(h => h.HandleAsync(It.IsAny <ExceptionHandlerContext>(), It.IsAny <CancellationToken>()))
            .Callback <ExceptionHandlerContext, CancellationToken>((c, i) => c.Result = null)
            .Returns(Task.FromResult(0));
            IExceptionHandler exceptionHandler = exceptionHandlerMock.Object;

            using (HttpRequestMessage request = CreateRequest())
                using (HttpConfiguration configuration = CreateConfiguration())
                    using (HttpServer product = CreateProductUnderTest(configuration, dispatcher, exceptionLogger,
                                                                       exceptionHandler))
                    {
                        CancellationToken cancellationToken = CreateCancellationToken();

                        Task <HttpResponseMessage> task = product.SendAsync(request, cancellationToken);

                        // Act
                        task.WaitUntilCompleted();

                        // Assert
                        Assert.Equal(TaskStatus.Faulted, task.Status);
                        Assert.NotNull(task.Exception);
                        Exception exception = task.Exception.GetBaseException();
                        Assert.Same(expectedException, exception);
                        Assert.NotNull(exception.StackTrace);
                        Assert.True(exception.StackTrace.StartsWith(expectedStackTrace));
                    }
        }