/// <summary> Subtest for exception handler which tests that exception handler is invoked. </summary> private void SubtestSendAsyncExceptionHandler(bool throwException, bool handle) { var handler = new ExceptionMessageHandler { ThrowException = throwException }; var configurableHanlder = new ConfigurableMessageHandler(handler); var exceptionHandler = new ExceptionMessageHandler.ExceptionHandler { Handle = handle }; configurableHanlder.ExceptionHandlers.Add(exceptionHandler); using (var client = new HttpClient(configurableHanlder)) { var request = new HttpRequestMessage(HttpMethod.Get, "https://test-exception-handler"); try { HttpResponseMessage response = client.SendAsync(request).Result; if (throwException) { Assert.Fail("SendAsync should throw an exception"); } } catch (AggregateException ae) { Assert.That(ae.InnerException.Message, Is.EqualTo(ExceptionMessageHandler.ExceptionMessage)); } // if exception is thrown, check if it's handles. if so, there should be num tries calls, otherwise // only 1 if (throwException) { Assert.That(exceptionHandler.Calls, Is.EqualTo(handle ? configurableHanlder.NumTries : 1)); } // exception wasn't supposed to be thrown, so no call to exception handler should be made else { Assert.That(exceptionHandler.Calls, Is.EqualTo(0)); } Assert.That(handler.Calls, Is.EqualTo(throwException & handle ? configurableHanlder.NumTries : 1)); } }
/// <summary>Subtest for exception handler which tests that exception handler is invoked.</summary> private void SubtestSendAsyncExceptionHandler(bool throwException, bool handle) { var handler = new ExceptionMessageHandler { ThrowException = throwException }; var configurableHanlder = new ConfigurableMessageHandler(handler); var exceptionHandler = new ExceptionMessageHandler.ExceptionHandler { Handle = handle }; configurableHanlder.AddExceptionHandler(exceptionHandler); using (var client = new HttpClient(configurableHanlder)) { var request = new HttpRequestMessage(HttpMethod.Get, "https://test-exception-handler"); try { HttpResponseMessage response = client.SendAsync(request).Result; if (throwException) { Assert.Fail("SendAsync should throw an exception"); } } catch (AggregateException ae) { Assert.That(ae.InnerException.Message, Is.EqualTo(ExceptionMessageHandler.ExceptionMessage)); } // if exception is thrown, check if it's handles. if so, there should be num tries calls, otherwise // only 1 if (throwException) { Assert.That(exceptionHandler.Calls, Is.EqualTo(handle ? configurableHanlder.NumTries : 1)); } // exception wasn't supposed to be thrown, so no call to exception handler should be made else { Assert.That(exceptionHandler.Calls, Is.EqualTo(0)); } Assert.That(handler.Calls, Is.EqualTo(throwException & handle ? configurableHanlder.NumTries : 1)); } }