public async Task SendAsync_IfExceptionHandlerHandlesException_ReturnsResponse() { // Arrange 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 server = CreateServer(configuration)) using (HttpBatchHandler product = new LambdaHttpBatchHandler(server, exceptionLogger, exceptionHandler, (i1, i2) => CreateFaultedTask <HttpResponseMessage>(CreateException()))) { CancellationToken cancellationToken = CreateCancellationToken(); // Act HttpResponseMessage response = await product.SendAsync(request, cancellationToken); // Assert Assert.Same(expectedResponse, response); } } }
public async Task SendAsync_IfExceptionHandlerSetsNullResult_PropogatesFaultedTaskException() { // Arrange Exception expectedException = CreateExceptionWithCallStack(); string expectedStackTrace = expectedException.StackTrace; 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 server = CreateServer(configuration)) using (HttpBatchHandler product = new LambdaHttpBatchHandler(server, exceptionLogger, exceptionHandler, (i1, i2) => CreateFaultedTask <HttpResponseMessage>(expectedException))) { CancellationToken cancellationToken = CreateCancellationToken(); // Act var exception = await Assert.ThrowsAsync <Exception>(() => product.SendAsync(request, cancellationToken)); // Assert Assert.Same(expectedException, exception); Assert.NotNull(exception.StackTrace); Assert.StartsWith(expectedStackTrace, exception.StackTrace); } }
public async Task SendAsync_IfProcessBatchAsyncTaskIsFaulted_CallsExceptionServices() { // Arrange Exception expectedException = CreateException(); 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 server = CreateServer(configuration)) using ( HttpBatchHandler product = new LambdaHttpBatchHandler( server, exceptionLogger, exceptionHandler, (i1, i2) => CreateFaultedTask <HttpResponseMessage>(expectedException) ) ) { CancellationToken cancellationToken = CreateCancellationToken(); // Act await Assert.ThrowsAsync <Exception>( () => product.SendAsync(expectedRequest, cancellationToken) ); // Assert Func <ExceptionContext, bool> exceptionContextMatches = (c) => c != null && c.Exception == expectedException && c.CatchBlock == ExceptionCatchBlocks.HttpBatchHandler && c.Request == expectedRequest; exceptionLoggerMock.Verify( l => l.LogAsync( It.Is <ExceptionLoggerContext>( c => exceptionContextMatches(c.ExceptionContext) ), cancellationToken ), Times.Once() ); exceptionHandlerMock.Verify( h => h.HandleAsync( It.Is <ExceptionHandlerContext>( (c) => exceptionContextMatches(c.ExceptionContext) ), cancellationToken ), Times.Once() ); } }
public async Task SendAsync_IfProcessBatchAsyncTaskIsCanceled_DoesNotCallExceptionServices() { // Arrange Exception expectedException = new OperationCanceledException(); 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 server = CreateServer(configuration)) using ( HttpBatchHandler product = new LambdaHttpBatchHandler( server, exceptionLogger, exceptionHandler, (i1, i2) => CreateFaultedTask <HttpResponseMessage>(expectedException) ) ) { CancellationToken cancellationToken = CreateCancellationToken(); // Act & Assert await Assert.ThrowsAsync <OperationCanceledException>( () => product.SendAsync(expectedRequest, cancellationToken) ); } }
public void SendAsync_IfProcessBatchAsyncTaskIsCanceled_DoesNotCallExceptionServices() { // Arrange Exception expectedException = new OperationCanceledException(); 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 server = CreateServer(configuration)) using (HttpBatchHandler product = new LambdaHttpBatchHandler(server, exceptionLogger, exceptionHandler, (i1, i2) => CreateFaultedTask <HttpResponseMessage>(expectedException))) { CancellationToken cancellationToken = CreateCancellationToken(); Task <HttpResponseMessage> task = product.SendAsync(expectedRequest, cancellationToken); // Act task.WaitUntilCompleted(); // Assert Assert.Equal(TaskStatus.Canceled, task.Status); } }