|
7 | 7 | using System.Threading.Tasks; |
8 | 8 | using Polly; |
9 | 9 | using Polly.Timeout; |
| 10 | +using Simple.HttpClientFactory.MessageHandlers; |
10 | 11 | using WireMock.RequestBuilders; |
11 | 12 | using WireMock.ResponseBuilders; |
12 | 13 | using WireMock.Server; |
@@ -44,6 +45,22 @@ public TestException(string message) : base(message) |
44 | 45 | } |
45 | 46 | } |
46 | 47 |
|
| 48 | + [Fact] |
| 49 | + public void Exception_messege_handler_ctor_should_validate_first_param() => |
| 50 | + Assert.Throws<ArgumentNullException>(() => new ExceptionTranslatorRequestMiddleware(null, e => e)); |
| 51 | + |
| 52 | + [Fact] |
| 53 | + public void Exception_messege_handler_ctor_should_validate_second_param() => |
| 54 | + Assert.Throws<ArgumentNullException>(() => new ExceptionTranslatorRequestMiddleware(e => true, null)); |
| 55 | + |
| 56 | + [Fact] |
| 57 | + public void Exception_messege_handler_ctor_should_validate_first_param_overload() => |
| 58 | + Assert.Throws<ArgumentNullException>(() => new ExceptionTranslatorRequestMiddleware(null, e => e, new ExceptionTranslatorRequestMiddleware(e => true, e => e))); |
| 59 | + |
| 60 | + [Fact] |
| 61 | + public void Exception_messege_handler_ctor_should_validate_second_param_overload() => |
| 62 | + Assert.Throws<ArgumentNullException>(() => new ExceptionTranslatorRequestMiddleware(e => true, null, new ExceptionTranslatorRequestMiddleware(e => true, e => e))); |
| 63 | + |
47 | 64 | [Fact] |
48 | 65 | public async Task Exception_translator_can_translate_exception_types() |
49 | 66 | { |
@@ -81,6 +98,24 @@ public async Task Exception_translator_should_not_change_unhandled_exceptions() |
81 | 98 |
|
82 | 99 | } |
83 | 100 |
|
| 101 | + [Fact] |
| 102 | + public async Task Exception_translator_should_throw_original_exception_if_delegate_is_false() |
| 103 | + { |
| 104 | + var clientWithRetry = HttpClientFactory.Create() |
| 105 | + .WithMessageExceptionHandler(ex => false, ex => ex) |
| 106 | + .WithPolicy( |
| 107 | + Policy<HttpResponseMessage> |
| 108 | + .Handle<HttpRequestException>() |
| 109 | + .OrResult(result => (int)result.StatusCode >= 500 || result.StatusCode == HttpStatusCode.RequestTimeout) |
| 110 | + .WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(1))) |
| 111 | + .WithPolicy(Policy.TimeoutAsync<HttpResponseMessage>(TimeSpan.FromSeconds(4), TimeoutStrategy.Optimistic)) |
| 112 | + .Build(); |
| 113 | + |
| 114 | + await Assert.ThrowsAsync<HttpRequestException>(() => clientWithRetry.GetAsync(_server.Urls[0] + "/timeout")); |
| 115 | + Assert.Equal(4, _server.LogEntries.Count()); |
| 116 | + |
| 117 | + } |
| 118 | + |
84 | 119 | [Fact] |
85 | 120 | public async Task Exception_translator_without_errors_should_not_affect_anything() |
86 | 121 | { |
|
0 commit comments