Skip to content

Commit 7fb4cb2

Browse files
committed
add some tests
1 parent 8211217 commit 7fb4cb2

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

Simple.HttpClientFactory.Tests/ExceptionTranslatorTests.cs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
using System.Threading.Tasks;
88
using Polly;
99
using Polly.Timeout;
10+
using Simple.HttpClientFactory.MessageHandlers;
1011
using WireMock.RequestBuilders;
1112
using WireMock.ResponseBuilders;
1213
using WireMock.Server;
@@ -44,6 +45,22 @@ public TestException(string message) : base(message)
4445
}
4546
}
4647

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+
4764
[Fact]
4865
public async Task Exception_translator_can_translate_exception_types()
4966
{
@@ -81,6 +98,24 @@ public async Task Exception_translator_should_not_change_unhandled_exceptions()
8198

8299
}
83100

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+
84119
[Fact]
85120
public async Task Exception_translator_without_errors_should_not_affect_anything()
86121
{

0 commit comments

Comments
 (0)