-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
more code quality refactoring, ensure Polly message handler won't cau…
…se async deadlocks, more testing
- Loading branch information
MIchael Yarichuk
committed
May 4, 2020
1 parent
b6e298c
commit 80de6a5
Showing
5 changed files
with
139 additions
and
11 deletions.
There are no files selected for viewing
110 changes: 110 additions & 0 deletions
110
Simple.HttpClientFactory.Tests/ExceptionTranslatorTests.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Net; | ||
using System.Net.Http; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
using Polly; | ||
using Polly.Timeout; | ||
using WireMock.RequestBuilders; | ||
using WireMock.ResponseBuilders; | ||
using WireMock.Server; | ||
using Xunit; | ||
|
||
namespace Simple.HttpClientFactory.Tests | ||
{ | ||
public class ExceptionTranslatorTests | ||
{ | ||
private readonly WireMockServer _server; | ||
private readonly List<string> _visitedMiddleware = new List<string>(); | ||
|
||
public ExceptionTranslatorTests() | ||
{ | ||
_server = WireMockServer.Start(); | ||
_server.Given(Request.Create().WithPath("/hello/world").UsingAnyMethod()) | ||
.RespondWith( | ||
Response.Create() | ||
.WithStatusCode(200) | ||
.WithHeader("Content-Type", "text/plain") | ||
.WithBody("Hello world!")); | ||
|
||
_server | ||
.Given(Request.Create() | ||
.WithPath("/timeout") | ||
.UsingGet()) | ||
.RespondWith(Response.Create() | ||
.WithStatusCode(408)); | ||
} | ||
|
||
public class TestException : Exception | ||
{ | ||
public TestException(string message) : base(message) | ||
{ | ||
} | ||
} | ||
|
||
[Fact] | ||
public async Task Exception_translator_can_translate_exception_types() | ||
{ | ||
var clientWithRetry = HttpClientFactory.Create() | ||
.WithMessageExceptionHandler(ex => true, ex => new TestException(ex.Message)) | ||
.WithPolicy( | ||
Policy<HttpResponseMessage> | ||
.Handle<HttpRequestException>() | ||
.OrResult(result => (int)result.StatusCode >= 500 || result.StatusCode == HttpStatusCode.RequestTimeout) | ||
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(1))) | ||
.WithPolicy(Policy.TimeoutAsync<HttpResponseMessage>(TimeSpan.FromSeconds(4), TimeoutStrategy.Optimistic)) | ||
.Build(); | ||
|
||
await Assert.ThrowsAsync<TestException>(() => clientWithRetry.GetAsync(_server.Urls[0] + "/timeout")); | ||
Assert.Equal(4, _server.LogEntries.Count()); | ||
|
||
} | ||
|
||
|
||
[Fact] | ||
public async Task Exception_translator_should_not_change_unhandled_exceptions() | ||
{ | ||
var clientWithRetry = HttpClientFactory.Create() | ||
.WithMessageExceptionHandler(ex => true, ex => ex) | ||
.WithPolicy( | ||
Policy<HttpResponseMessage> | ||
.Handle<HttpRequestException>() | ||
.OrResult(result => (int)result.StatusCode >= 500 || result.StatusCode == HttpStatusCode.RequestTimeout) | ||
.WaitAndRetryAsync(3, retryAttempt => TimeSpan.FromSeconds(1))) | ||
.WithPolicy(Policy.TimeoutAsync<HttpResponseMessage>(TimeSpan.FromSeconds(4), TimeoutStrategy.Optimistic)) | ||
.Build(); | ||
|
||
await Assert.ThrowsAsync<HttpRequestException>(() => clientWithRetry.GetAsync(_server.Urls[0] + "/timeout")); | ||
Assert.Equal(4, _server.LogEntries.Count()); | ||
|
||
} | ||
|
||
[Fact] | ||
public async Task Exception_translator_without_errors_should_not_affect_anything() | ||
{ | ||
var trafficRecorderMessageHandler = new TrafficRecorderMessageHandler(_visitedMiddleware); | ||
var eventMessageHandler = new EventMessageHandler(_visitedMiddleware); | ||
|
||
var client = HttpClientFactory.Create() | ||
.WithMessageExceptionHandler(ex => true, ex => ex) | ||
.WithMessageHandler(eventMessageHandler) | ||
.WithMessageHandler(trafficRecorderMessageHandler) | ||
.Build(); | ||
|
||
var raisedEvent = await Assert.RaisesAsync<EventMessageHandler.RequestEventArgs>( | ||
h => eventMessageHandler.Request += h, | ||
h => eventMessageHandler.Request -= h, | ||
() => client.GetAsync(_server.Urls[0] + "/hello/world")); | ||
|
||
Assert.True(raisedEvent.Arguments.Request.Headers.Contains("foobar")); | ||
Assert.Equal("foobar",raisedEvent.Arguments.Request.Headers.GetValues("foobar").FirstOrDefault()); | ||
Assert.Single(trafficRecorderMessageHandler.Traffic); | ||
|
||
Assert.Equal(HttpStatusCode.OK, trafficRecorderMessageHandler.Traffic[0].Item2.StatusCode); | ||
Assert.Equal(new [] { nameof(TrafficRecorderMessageHandler), nameof(EventMessageHandler) }, _visitedMiddleware); | ||
} | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters