Exemplo n.º 1
0
        public void HandleAsync_IfResultIsSet_CallsResultExecuteAsync()
        {
            // Arrange
            using (HttpResponseMessage response = CreateResponse())
            {
                Mock <IHttpActionResult> mock = new Mock <IHttpActionResult>(MockBehavior.Strict);
                mock.Setup(r => r.ExecuteAsync(It.IsAny <CancellationToken>())).Returns(Task.FromResult(response));
                IHttpActionResult result  = mock.Object;
                IExceptionHandler handler = CreateResultHandler(result);

                using (CancellationTokenSource tokenSource = CreateCancellationTokenSource())
                {
                    ExceptionContext  context = CreateMinimalValidContext();
                    CancellationToken expectedCancellationToken = tokenSource.Token;

                    // Act
                    Task task = ExceptionHandlerExtensions.HandleAsync(handler, context,
                                                                       expectedCancellationToken);

                    // Assert
                    Assert.NotNull(task);
                    task.WaitUntilCompleted();
                    task.ThrowIfFaulted();

                    mock.Verify(h => h.ExecuteAsync(expectedCancellationToken), Times.Once());
                }
            }
        }
Exemplo n.º 2
0
        public void HandleAsync_IfResultIsSetButReturnsNull_ReturnsFaultedTask()
        {
            // Arrange
            Mock <IHttpActionResult> mock = new Mock <IHttpActionResult>(MockBehavior.Strict);

            mock
            .Setup(r => r.ExecuteAsync(It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult <HttpResponseMessage>(null));
            IHttpActionResult result            = mock.Object;
            IExceptionHandler handler           = CreateResultHandler(result);
            ExceptionContext  context           = CreateMinimalValidContext();
            CancellationToken cancellationToken = CancellationToken.None;

            // Act
            Task <HttpResponseMessage> task =
                ExceptionHandlerExtensions.HandleAsync(handler, context, cancellationToken);

            // Assert
            Assert.NotNull(task);
            task.WaitUntilCompleted();
            Assert.Equal(TaskStatus.Faulted, task.Status);
            Assert.NotNull(task.Exception);
            Exception exception = task.Exception.GetBaseException();

            Assert.IsType <InvalidOperationException>(exception);
            Assert.Equal("IHttpActionResult.ExecuteAsync must not return null.", exception.Message);
        }
Exemplo n.º 3
0
        public async Task HandleAsync_CallsInterfaceHandleAsync()
        {
            // Arrange
            Mock <IExceptionHandler> mock    = CreateStubHandlerMock();
            IExceptionHandler        handler = mock.Object;

            using (CancellationTokenSource tokenSource = CreateCancellationTokenSource())
            {
                ExceptionContext  expectedContext           = CreateMinimalValidContext();
                CancellationToken expectedCancellationToken = tokenSource.Token;

                // Act
                await ExceptionHandlerExtensions.HandleAsync(
                    handler,
                    expectedContext,
                    expectedCancellationToken
                    );

                // Assert
                mock.Verify(
                    h =>
                    h.HandleAsync(
                        It.Is <ExceptionHandlerContext>(
                            c => c.ExceptionContext == expectedContext
                            ),
                        expectedCancellationToken
                        ),
                    Times.Once()
                    );
            }
        }
        public void HandleAsync_IfHandlerIsNull_Throws()
        {
            // Arrange
            IExceptionHandler handler           = null;
            ExceptionContext  context           = CreateMinimalValidContext();
            CancellationToken cancellationToken = CancellationToken.None;

            // Act & Assert
            Assert.ThrowsArgumentNull(() =>
                                      ExceptionHandlerExtensions.HandleAsync(handler, context, cancellationToken), "handler");
        }
        public void HandleAsync_IfContextIsNull_Throws()
        {
            // Arrange
            IExceptionHandler handler           = CreateDummyHandler();
            ExceptionContext  context           = null;
            CancellationToken cancellationToken = CancellationToken.None;

            // Act & Assert
            Assert.ThrowsArgumentNull(() =>
                                      ExceptionHandlerExtensions.HandleAsync(handler, context, cancellationToken), "context");
        }
        public async Task HandleAsync_IfResultIsNotSet_ReturnsCompletedTaskWithNullResponse()
        {
            // Arrange
            IExceptionHandler handler           = CreateStubHandler();
            ExceptionContext  context           = CreateMinimalValidContext();
            CancellationToken cancellationToken = CancellationToken.None;

            // Act
            HttpResponseMessage response = await ExceptionHandlerExtensions.HandleAsync(handler, context, cancellationToken);

            // Assert
            Assert.Null(response);
        }
Exemplo n.º 7
0
        public void HandleAsync_IfResultIsNotSet_ReturnsCompletedTaskWithNullResponse()
        {
            // Arrange
            IExceptionHandler handler           = CreateStubHandler();
            ExceptionContext  context           = CreateMinimalValidContext();
            CancellationToken cancellationToken = CancellationToken.None;

            // Act
            Task <HttpResponseMessage> task = ExceptionHandlerExtensions.HandleAsync(handler, context,
                                                                                     cancellationToken);

            // Assert
            Assert.NotNull(task);
            task.WaitUntilCompleted();
            Assert.Equal(TaskStatus.RanToCompletion, task.Status);
            Assert.Null(task.Result);
        }
        public async Task HandleAsync_IfResultIsSetButReturnsNull_ReturnsFaultedTask()
        {
            // Arrange
            Mock <IHttpActionResult> mock = new Mock <IHttpActionResult>(MockBehavior.Strict);

            mock
            .Setup(r => r.ExecuteAsync(It.IsAny <CancellationToken>()))
            .Returns(Task.FromResult <HttpResponseMessage>(null));
            IHttpActionResult result            = mock.Object;
            IExceptionHandler handler           = CreateResultHandler(result);
            ExceptionContext  context           = CreateMinimalValidContext();
            CancellationToken cancellationToken = CancellationToken.None;

            // Act & Assert
            var exception = await Assert.ThrowsAsync <InvalidOperationException>(
                () => ExceptionHandlerExtensions.HandleAsync(handler, context, cancellationToken));

            Assert.Equal("IHttpActionResult.ExecuteAsync must not return null.", exception.Message);
        }
Exemplo n.º 9
0
        public void HandleAsync_CallsInterfaceHandleAsync()
        {
            // Arrange
            Mock <IExceptionHandler> mock    = CreateStubHandlerMock();
            IExceptionHandler        handler = mock.Object;

            using (CancellationTokenSource tokenSource = CreateCancellationTokenSource())
            {
                ExceptionContext  expectedContext           = CreateMinimalValidContext();
                CancellationToken expectedCancellationToken = tokenSource.Token;

                // Act
                Task task = ExceptionHandlerExtensions.HandleAsync(handler, expectedContext,
                                                                   expectedCancellationToken);

                // Assert
                Assert.NotNull(task);
                task.WaitUntilCompleted();
                task.ThrowIfFaulted();

                mock.Verify(h => h.HandleAsync(It.Is <ExceptionHandlerContext>(
                                                   c => c.ExceptionContext == expectedContext), expectedCancellationToken), Times.Once());
            }
        }