|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Net; |
| 5 | +using System.Net.Http; |
| 6 | +using System.Text; |
| 7 | +using System.Threading.Tasks; |
| 8 | +using Polly; |
| 9 | +using Polly.Timeout; |
| 10 | +using WireMock.RequestBuilders; |
| 11 | +using WireMock.ResponseBuilders; |
| 12 | +using WireMock.Server; |
| 13 | +using Xunit; |
| 14 | + |
| 15 | +namespace Simple.HttpClientFactory.Tests |
| 16 | +{ |
| 17 | + public class ExceptionTranslatorTests |
| 18 | + { |
| 19 | + private readonly WireMockServer _server; |
| 20 | + private readonly List<string> _visitedMiddleware = new List<string>(); |
| 21 | + |
| 22 | + public ExceptionTranslatorTests() |
| 23 | + { |
| 24 | + _server = WireMockServer.Start(); |
| 25 | + _server.Given(Request.Create().WithPath("/hello/world").UsingAnyMethod()) |
| 26 | + .RespondWith( |
| 27 | + Response.Create() |
| 28 | + .WithStatusCode(200) |
| 29 | + .WithHeader("Content-Type", "text/plain") |
| 30 | + .WithBody("Hello world!")); |
| 31 | + |
| 32 | + _server |
| 33 | + .Given(Request.Create() |
| 34 | + .WithPath("/timeout") |
| 35 | + .UsingGet()) |
| 36 | + .RespondWith(Response.Create() |
| 37 | + .WithStatusCode(408)); |
| 38 | + } |
| 39 | + |
| 40 | + public class TestException : Exception |
| 41 | + { |
| 42 | + public TestException(string message) : base(message) |
| 43 | + { |
| 44 | + } |
| 45 | + } |
| 46 | + |
| 47 | + [Fact] |
| 48 | + public async Task Exception_translator_can_translate_exception_types() |
| 49 | + { |
| 50 | + var clientWithRetry = HttpClientFactory.Create() |
| 51 | + .WithMessageExceptionHandler(ex => true, ex => new TestException(ex.Message)) |
| 52 | + .WithPolicy( |
| 53 | + Policy<HttpResponseMessage> |
| 54 | + .Handle<HttpRequestException>() |
| 55 | + .OrResult(result => (int)result.StatusCode >= 500 || result.StatusCode == HttpStatusCode.RequestTimeout) |
| 56 | + .WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(1))) |
| 57 | + .WithPolicy(Policy.TimeoutAsync<HttpResponseMessage>(TimeSpan.FromSeconds(4), TimeoutStrategy.Optimistic)) |
| 58 | + .Build(); |
| 59 | + |
| 60 | + await Assert.ThrowsAsync<TestException>(() => clientWithRetry.GetAsync(_server.Urls[0] + "/timeout")); |
| 61 | + Assert.Equal(4, _server.LogEntries.Count()); |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + |
| 66 | + [Fact] |
| 67 | + public async Task Exception_translator_should_not_change_unhandled_exceptions() |
| 68 | + { |
| 69 | + var clientWithRetry = HttpClientFactory.Create() |
| 70 | + .WithMessageExceptionHandler(ex => true, ex => ex) |
| 71 | + .WithPolicy( |
| 72 | + Policy<HttpResponseMessage> |
| 73 | + .Handle<HttpRequestException>() |
| 74 | + .OrResult(result => (int)result.StatusCode >= 500 || result.StatusCode == HttpStatusCode.RequestTimeout) |
| 75 | + .WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(1))) |
| 76 | + .WithPolicy(Policy.TimeoutAsync<HttpResponseMessage>(TimeSpan.FromSeconds(4), TimeoutStrategy.Optimistic)) |
| 77 | + .Build(); |
| 78 | + |
| 79 | + await Assert.ThrowsAsync<HttpRequestException>(() => clientWithRetry.GetAsync(_server.Urls[0] + "/timeout")); |
| 80 | + Assert.Equal(4, _server.LogEntries.Count()); |
| 81 | + |
| 82 | + } |
| 83 | + |
| 84 | + [Fact] |
| 85 | + public async Task Exception_translator_without_errors_should_not_affect_anything() |
| 86 | + { |
| 87 | + var trafficRecorderMessageHandler = new TrafficRecorderMessageHandler(_visitedMiddleware); |
| 88 | + var eventMessageHandler = new EventMessageHandler(_visitedMiddleware); |
| 89 | + |
| 90 | + var client = HttpClientFactory.Create() |
| 91 | + .WithMessageExceptionHandler(ex => true, ex => ex) |
| 92 | + .WithMessageHandler(eventMessageHandler) |
| 93 | + .WithMessageHandler(trafficRecorderMessageHandler) |
| 94 | + .Build(); |
| 95 | + |
| 96 | + var raisedEvent = await Assert.RaisesAsync<EventMessageHandler.RequestEventArgs>( |
| 97 | + h => eventMessageHandler.Request += h, |
| 98 | + h => eventMessageHandler.Request -= h, |
| 99 | + () => client.GetAsync(_server.Urls[0] + "/hello/world")); |
| 100 | + |
| 101 | + Assert.True(raisedEvent.Arguments.Request.Headers.Contains("foobar")); |
| 102 | + Assert.Equal("foobar",raisedEvent.Arguments.Request.Headers.GetValues("foobar").FirstOrDefault()); |
| 103 | + Assert.Single(trafficRecorderMessageHandler.Traffic); |
| 104 | + |
| 105 | + Assert.Equal(HttpStatusCode.OK, trafficRecorderMessageHandler.Traffic[0].Item2.StatusCode); |
| 106 | + Assert.Equal(new [] { nameof(TrafficRecorderMessageHandler), nameof(EventMessageHandler) }, _visitedMiddleware); |
| 107 | + } |
| 108 | + |
| 109 | + } |
| 110 | +} |
0 commit comments